Microsoft® การแปลงไฟล์ Excel via Java

บันทึก Microsoft เอกสาร Excel เป็นสเปรดชีต เว็บ รูปภาพ และรูปแบบคงที่

 

สำหรับอย่างใดอย่างหนึ่งตัวแปลง Excel แอปพลิเคชันหรือโซลูชัน Java Excel Library เร่งความเร็วการเขียนโปรแกรมสเปรดชีตและกระบวนการแปลงในขณะที่จัดการหลายรูปแบบรวมถึง XLS, XLSX, XLSM, XLSB, XLTX, XLTM, CSV, SpreadsheetML, 07619 3481 นอกจากนี้ยังอนุญาตให้ แปลงไฟล์ Excel เป็น PDF*, XPS, HTML, MHTML, Plain Text และรูปแบบรูปภาพยอดนิยม เช่น TIFF, JPG, PNG, BMP และ SVG.

การแปลงรูปแบบ Excel Microsoft ระหว่างกัน

การแปลงรูปแบบสเปรดชีตระหว่างกันต้องโหลดสเปรดชีตที่มีอินสแตนซ์เท่านั้น สมุดงาน และบันทึกกลับในรูปแบบที่ต้องการพร้อมเลือกค่าที่เหมาะสมจาก บันทึกรูปแบบ การแจงนับ

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

มีคลาสเฉพาะทางเพื่อควบคุมกระบวนการแปลงสำหรับรูปแบบเอาต์พุตเฉพาะเช่น ตัวเลือก PdfSave เพื่อแปลงไฟล์ Excel เป็น PDF, XpsSaveOptions เพื่อส่งออก Excel เป็น XPS, HtmlSaveตัวเลือก เพื่อเรนเดอร์ Excel เป็น HTML และ MarkdownSaveOptions สำหรับการแปลง Excel เป็น Markdown

Java โค้ดตัวอย่างสำหรับ Excel ถึง PDF และรูปแบบเว็บ
// 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 ได้ด้วยการสร้าง พิสัย หรือเซลล์และเรียก ส่งออก RangeToJson วิธี.

รหัส 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 ในขณะที่ใช้งาน DocxSaveOptions & PptxSaveOptions ชั้นเรียนตามที่แสดงด้านล่าง

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