当前版本:23.8 (测试过最新的25.2版本,同样可以复现)
编程语言:java
异常截图:
<a class="attachment" href="/uploads/default/104960">image.jpg</a> (176.0 KB)
源文件:
<a class="attachment" href="/uploads/default/104961">入职_20250224 - 副本.zip</a> (11.0 KB)
转换代码:
package com.qiyuesuo.aspose;
import com.aspose.cells.LoadOptions;
import com.aspose.cells.MemorySetting;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class AsposeExcelTest {
public static void main(String[] args) throws Exception {
byte[] bytes = Files.readAllBytes(Paths.get("E:\\Emobile-Download\\%E5%85%A5%E8%81%8C_20250224.xlsx"));
FileOutputStream outputStream = new FileOutputStream("E:\\Emobile-Download\\test_20250224.pdf");
LoadOptions opt = new LoadOptions();
opt.setMemorySetting(MemorySetting.MEMORY_PREFERENCE);
Workbook workbook = new Workbook(new ByteArrayInputStream(bytes), opt);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
// 缩放到一个页面(如果列太多,太长)
pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
workbook.save(outputStream, pdfSaveOptions);
}
}
@ZhonghaoSun
由于你的样例文件里包含了没有设置自定义行高的行,在excel打开文件会进行行高的自动调整。请调用
AutoFitRows(bool onlyAuto)方法来自动调整没有设置自定义行高的行。请查看附件。out_java.pdf (123.0 KB)
样例代码如下:
byte[] bytes = Files.readAllBytes(Paths.get(filePath + "%E5%85%A5%E8%81%8C_20250224 - 副本.xlsx"));
FileOutputStream outputStream = new FileOutputStream(filePath + "out_java.pdf");
LoadOptions opt = new LoadOptions();
opt.setMemorySetting(MemorySetting.MEMORY_PREFERENCE);
Workbook workbook = new Workbook(new ByteArrayInputStream(bytes), opt);
//添加这行代码进行自动调整没有设置自定义行高的行
workbook.getWorksheets().get(0).autoFitRows(true);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
// 缩放到一个页面(如果列太多,太长)
pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
workbook.save(outputStream, pdfSaveOptions);
能否通过修改Excel中的行高来处理呢,可以的话如何修改
@ZhonghaoSun
你可以直接拖动一下相关的行来改变行高就可以了。请查看修改过后的样例文件和结果文件。result.zip (129.1 KB)
样例代码如下:
byte[] bytes = Files.readAllBytes(Paths.get(filePath + "%E5%85%A5%E8%81%8C_20250224 - 副本.xlsx"));
FileOutputStream outputStream = new FileOutputStream(filePath + "out_java.pdf");
LoadOptions opt = new LoadOptions();
opt.setMemorySetting(MemorySetting.MEMORY_PREFERENCE);
Workbook workbook = new Workbook(new ByteArrayInputStream(bytes), opt);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
// 缩放到一个页面(如果列太多,太长)
pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
workbook.save(outputStream, pdfSaveOptions);