Microsoft® Excel 文件转换 via Java

将 Microsoft Excel 文档另存为电子表格、Web、图像和固定布局格式

 

对于任何Excel转换器应用程序或解决方案,Java Excel 库可加速电子表格编程和转换过程,同时处理多种格式,包括 XLS、XLSX、XLSM、XLSB、XLTX、XLTM、CSV、SpreadsheetML、0761934 81. 它还允许*将Excel文件转换为PDF**, XPS、HTML、MHTML、纯文本和流行图像格式,例如 TIFF、JPG、PNG、BMP 和 SVG。

Microsoft Excel 格式的相互转换

电子表格格式的相互转换只需要加载带有实例的电子表格 练习册 并以所需的格式保存,同时从中选择适当的值 保存格式 枚举。

Java Excel 文件格式转换示例代码
// load the source file
var wkb = new Workbook("sourceFile.xls");
// save as XLSX, ODS, SXC & FODS formats
wkb.save("xlsx-output.xlsx", SaveFormat.XLSX);
wkb.save("ods-output.ods", SaveFormat.ODS);
wkb.save("scx-output.scx", SaveFormat.SXC);
wkb.save("fods-output.fods", SaveFormat.FODS);
 

将 Excel 转换为 PDF、XPS、HTML 和 MD

专门的类可用于控制特定输出格式的转换过程,例如 Pdf保存选项 将 Excel 文件转换为 PDF, Xps保存选项 将 Excel 导出为 XPS, Html保存选项 将 Excel 呈现为 HTML 和 Markdown保存选项 用于 Excel 到 Markdown 的转换。

Java Excel 到 PDF 和 Web 格式的示例代码
// load template Excel file from disc
var bk = new Workbook("source-file.xlsx");

// convert Excel to PDF using Java
// Create PDF options
PdfSaveOptions options = new PdfSaveOptions();
options.setCompliance(PdfCompliance.PDF_A_1_A);

bk.save("excel-to-pdf.pdf", options);
// save Excel in XPS
bk.save("output.xps", new XpsSaveOptions());
// save Excel in HTML
bk.save("output.html", new HtmlSaveOptions());
// save Excel in Markdown (MD)
bk.save("output.md", new MarkdownSaveOptions());

// one can set relevant save options as of his choice before saving into relevant format
 

将 JSON 转换为 Excel,并将 Excel 转换为 JSON

JSON 数据可以导入到 Workbook 类的实例中 JSONUtility.importData 用于进一步处理或简单转换为任何支持的格式。同样,工作表数据可以通过创建一个导出为JSON 范围 或细胞并调用 导出范围ToJson 方法。

Java JSON 到 Excel 转换的代码
Workbook workbook = new Workbook(path + "source-file.xlsx");
Worksheet wks = workbook.getWorksheets().get(0);
		
// Read File
File file = new File(path + "source-data.json");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String jsonInput = "";
String tempString;
while ((tempString = bufferedReader.readLine()) != null) {
	jsonInput = jsonInput + tempString; 
}
bufferedReader.close();
							
// Set JsonLayoutOptions
JsonLayoutOptions options = new JsonLayoutOptions();
options.setIgnoreArrayTitle(true);
options.setArrayAsTable(true);

// Import JSON Data
JSONUtility.importData(jsonInput, wks.getCells(), 0, 0, options);

// Save Excel file
workbook.save(path + "excel-to-json.out.xlsx");
Java Excel 到 JSON 转换的源代码
// load XLSX file with an instance of Workbook
Workbook workbook = new Workbook("sourceFile.xlsx");
// access CellsCollection of the worksheet containing data to be converted
Cells cells = workbook.getWorksheets().get(0).getCells();
// create & set ExportRangeToJsonOptions for advanced options
ExportRangeToJsonOptions exportOptions = new ExportRangeToJsonOptions();
// create a range of cells containing data to be exported
Range range = cells.createRange(0, 0, cells.getLastCell().getRow() + 1, cells.getLastCell().getColumn() + 1);
// export range as JSON data
String jsonData = JsonUtility.exportRangeToJson(range, exportOptions);
// write data to disc in JSON format
BufferedWriter writer = new BufferedWriter(new FileWriter("output.json"));
writer.write(jsonData);
writer.close();    
 

将 Excel 工作表保存到图像

每个工作表都可以转换为不同的图像格式,包括 JPG、BMP、PNG 和 GIF,由 ImageType 属性设置。对于任何将 Excel 转换为图像案例,从链接中选择相关案例。

Java Excel 到图像转换的代码
// load template spreadsheet
var wkb = new Workbook("template.xlsx");

// Create an object for ImageOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();

// Set the image type
imgOptions.setImageType(ImageType.PNG);

// Get the first worksheet.
Worksheet sheet = wkb.getWorksheets().get(0);

// Create a SheetRender object for the target sheet
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int j = 0; j < sr.getPageCount(); j++) {
	// Generate an image for the worksheet
	sr.toImage(j, dataDir + "WToImage-out" + j + ".png");
}
 

将 Microsoft Excel 转换为 Word 和 PowerPoint

使用时可以加载任何电子表格并将其转换为 Word DOCX 和 PowerPoint PPTX 文件 Docx保存选项 & Pptx保存选项 类如下所示。

Java Excel 到 Word 的代码 & PowerPoint 转换
// load the template file
var wkb = new Workbook("template.xlsx");
// save spreadsheet as DOCX
wkb.save("output.docx", new DocxSaveOptions());
// save spreadsheet as PPTX
wkb.save("output.pptx", new PptxSaveOptions());