简体   繁体   中英

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. Using apache poi, I was able to successfully do that. But now, I need a way to convert that file to xls and text uploading that to our application.

I'm using documents4j to try to convert the file from xlsx to xls. But it's failing to create the xls file. 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:

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.

[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. I checked and found we can change from XLS to XLSX, but reverse didn't work. I recommend you to use apache poi for convert formats. hope this be useful for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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