0

我有一个测试用例,需要下载 xlsx Excel 文件,向其中写入数据,然后保存。使用 apache poi,我能够成功地做到这一点。但是现在,我需要一种方法将该文件转换为 xls 并将其上传到我们的应用程序的文本。

我正在使用documents4j 尝试将文件从xlsx 转换为xls。但它无法创建 xls 文件。因此,当我运行测试时,它找不到文件,并且脚本在那时失败。

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class File_Converter {

    public static void convert_XLSX_To_XLS() throws InterruptedException, ExecutionException, IOException {
        File wordFile = new File("C:\\Users\\Jeff15\\Downloads\\scheduleUpload.xlsx"),
                target = new File("C:\\Users\\Jeff15\\Downloads\\scheduleUpload.xls");

        IConverter converter = LocalConverter.builder()
                .baseFolder(new File("C:\\Users\\Jeff15\\Downloads\\"))
                .workerPool(20, 25, 2, TimeUnit.SECONDS)
                .processTimeout(5, TimeUnit.SECONDS)
                .build();

        Future<Boolean> conversion = converter.convert(wordFile)
                .as(DocumentType.XLSX)
                .to(target)
                .as(DocumentType.XLS)
                // .prioritizeWith(1000) // optional
                .schedule();
    }
}

我有以下 Maven 依赖项:

documents4j-transformer-msoffice-base 1.0.3
documents4j-api 1.0.3
documents4j-util-conversion 1.0.3
documents4j-transformer-api 1.0.3
documents4j-util-all 1.0.3
documents4j-local 1.0.3
documents4j-util-ws 1.0.3
documents4j-util-standalone 1.0.3
documents4j-transformer-msoffice-excel 1.0.3
documents4j-test 1.0.3
documents4j-transformer-msoffice-test 1.0.3
documents4j-transformer-msoffice 1.0.3
documents4j-aggregation 1.0.3

我没有收到任何相关错误。脚本在找不到 xls 文件时失败,然后单击“上传”按钮。

[main] INFO com.documents4j.conversion.msoffice.MicrosoftExcelBridge - From-Microsoft-Excel-Converter was started successfully
[main] INFO com.documents4j.job.LocalConverter - The documents4j local converter has started successfully
[pool-1-thread-1] INFO com.documents4j.conversion.msoffice.MicrosoftExcelBridge - Requested conversion from C:\Users\Jeff15\Downloads\scheduleUpload.xlsx (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet) to C:\Users\Jeff15\Downloads\scheduleUpload.xls (application/vnd.ms-excel)
FAILED: Schedule_xls_File_Upload_Test
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[text()='Upload']"}
4

1 回答 1

0

看来,它是降级 excel 文件版本的错误。我检查并发现我们可以从 XLS 更改为 XLSX,但反向不起作用。我建议你使用 apache poi 来转换格式。希望这对你有用。

于 2018-08-21T09:21:28.697 回答