简体   繁体   English

无法使用documents4j将xlsx文件转换为xls

[英]Unable to convert xlsx file to xls using documents4j

I have a test case that requires downloading a xlsx Excel file, writing data to it, then saving it. 我有一个测试用例,要求下载一个xlsx Excel文件,向其中写入数据,然后保存。 Using apache poi, I was able to successfully do that. 使用apache poi,我能够成功做到这一点。 But now, I need a way to convert that file to xls and text uploading that to our application. 但是现在,我需要一种将该文件转换为xls并将其文本上传到我们的应用程序的方法。

I'm using documents4j to try to convert the file from xlsx to xls. 我正在使用documents4j尝试将文件从xlsx转换为xls。 But it's failing to create the xls file. 但是创建xls文件失败。 So when I run my test, it fails to find the file, and the script fails at that point. 因此,当我运行测试时,它找不到文件,并且脚本此时也失败了。

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();
    }
}

I have the following Maven dependencies: 我有以下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

I'm not getting any relevant errors. 我没有任何相关的错误。 The script fails when it fails to locate the xls file, and then click an "Upload" button afterwards. 当脚本无法找到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']"}

It seems, its a bug for downgrade a excel file version. 看来,这是降级excel文件版本的错误。 I checked and found we can change from XLS to XLSX, but reverse didn't work. 我检查发现,我们可以从XLS更改为XLSX,但是反向操作不起作用。 I recommend you to use apache poi for convert formats. 我建议您使用apache poi转换格式。 hope this be useful for you. 希望这对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM