我有一个测试用例,要求下载一个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']"}
答案 0 :(得分:0)
似乎,这是降级excel文件版本的错误。我检查发现,我们可以从XLS更改为XLSX,但是反向操作不起作用。我建议您使用apache poi转换格式。希望对您有用。