Java: Convert Excel to Office Open XML and Vice Versa
Office Open XML (also referred to as OOXML) is a zipped, XML-based format for Excel, Word and Presentation documents. Sometimes, you may need to convert an Excel file to Office Open XML in order to make it readable on various applications and platforms. Likewise, you might also want to convert Office Open XML to Excel for data calculations. In this article, you will learn how to Convert Excel to Office Open XML and vice versa in Java using Spire.XLS for Java library.
Install Spire.XLS for Java
First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls</artifactId> <version>15.6.3</version> </dependency> </dependencies>
Convert Excel to Office Open XML in Java
The following are the steps to convert an Excel file to Office Open XML:
- Create an instance of Workbook class.
- Load an Excel file using Workbook.loadFromFile() method.
- Call Workbook.saveAsXml() method to save the Excel file as Office Open XML.
- Java
import com.spire.xls.Workbook; public class ExcelToOpenXML { public static void main(String []args){ //Create a Workbook instance Workbook workbook = new Workbook(); //Load an Excel file workbook.loadFromFile("Sample.xlsx"); //Save as Office Open XML file format workbook.saveAsXml("ToXML.xml"); } }
Convert Office Open XML to Excel in Java
The following are the steps to convert an Office Open XML file to Excel:
- Create an instance of Workbook class.
- Load an Office Open XML file using Workbook.loadFromXml() file.
- Call Workbook.saveToFile() method to save the Office Open XML file as Excel.
- Java
import com.spire.xls.ExcelVersion; import com.spire.xls.Workbook; public class OpenXmlToExcel { public static void main(String []args){ //Create an instance of Workbook class Workbook workbook = new Workbook(); //Load an Office Open XML file workbook.loadFromXml("ToXML.xml"); //Save as Excel XLSX file format workbook.saveToFile("ToExcel.xlsx", ExcelVersion.Version2016); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Spire.Presentation for Java 5.1.0 fixes issue that the application threw "NullPointerException" when loading a PowerPoint document
We are glad to announce the release of Spire.Presentation for Java 5.1.0. This version fixes the issue that the application threw "NullPointerException" when loading a PowerPoint document. More details are listed below.
Here is a list of changes made in this release
Category | ID | Description |
Bug | SPIREPPT-1798 | Fixes the issue that the application threw "NullPointerException" when loading a PowerPoint document. |
Spire.PDF 8.1 supports removing JavaScript of the PDF document
We are glad to announce the release of Spire.PDF 8.1. This version supports removing JavaScript of the PDF document. It also enhances the conversion from PDF to PDF/A. In addition, it fixes some known issues such as the content was incorrect after printing. More details are listed below.
Here is a list of changes made in this release
Category | ID | Description |
New feature | SPIREPDF-3345 SPIREPDF-4825 |
Supports removing JavaScript of the PDF document.
PdfDocument doc = new PdfDocument(@"input.pdf"); doc.RemoveDocumentJavaScript(); doc.SaveToFile(@"output.pdf", FileFormat.PDF); |
Bug | SPIREPDF-1245 | Fixes the issue that the text content extracted was incorrect. |
Bug | SPIREPDF-3731 | Fixes the issue that the signature was not valid when adding the same signature to each page of the PDF. |
Bug | SPIREPDF-4738 | Fixes the issue that the output was incorrect after adding PdfPolygonAnnotation. |
Bug | SPIREPDF-4784 | Fixes the issue that the application threw "IndexOutOfRangeException" when extracting text. |
Bug | SPIREPDF-4807 | Fixes the issue that the content was incorrect after printing. |
Bug | SPIREPDF-4815 | Fixes the issue that the effect was incorrect after setting the PdfPolyLineAnnotation border thickness. |
Bug | SPIREPDF-4818 | Fixes the issue that the bold font style didn't take effect when filling data in TextBox field. |
Bug | SPIREPDF-4826 | Fixes the issue that the content was incorrect after printing. |
Bug | SPIREPDF-4847 | Fixes the issue that the attachment file name extracted was incorrect. |
Bug | SPIREPDF-4854 | Fixes the issue that the application threw "System.IO.IOException" when merging documents and saving the generated document to a input file path. |
Bug | SPIREPDF-4858 | Fixes the issue that the application threw "System.NullReferenceException" when converting PDF to PDF/A. |
Spire.PDF for Java 5.1.0 supports getting PDF PieceInfo dictionary information
We are glad to announce the release of Spire.PDF for Java 5.1.0. This version supports getting PDF PieceInfo dictionary information as well as supports printing by a specified printer. Also, it enhances the conversion from PDF to Word, PDF to SVG. In addition, it fixes some known issues such as the picture of PDF file was not displayed correctly after compressing. More details are listed below.
Here is a list of changes made in this release
Category | ID | Description |
New feature | SPIREPDF-4782 | Supports printing by a specified printer.
//Specify the printer name to print PrintSettings setting = pdf.getPrintSettings(); setting.setPrinter("Adobe PDF"); //Getsystem default printer PrinterJob loPrinterJob = PrinterJob.getPrinterJob(); PrintService printer = loPrinterJob.getPrintService(); String PrinterName = printer.getName(); //set to use the current printer loPrinterJob.setPrintService(printer); |
New feature | SPIREPDF-4829 | Supports getting PDF PieceInfo dictionary information.
PdfDocument pdf = new PdfDocument(); pdf.loadFromFile(inputFile); StringBuilder sb = new StringBuilder(); PdfPieceInfo info = pdf.getDocumentPieceInfo(); ArrayList<Object> values = new ArrayList<Object>(); HashMap<String, PdfApplicationData> dic = info.getApplicationDatas(); for (Map.Entry<String, PdfApplicationData> item : dic.entrySet()) { PdfApplicationData data = item.getValue(); values.add(data.getPrivate()); String content = data.getPrivate().toString(); } PdfPageBase page = pdf.getPages().get(0); PdfPieceInfo infor2 = page.getPagePieceInfo(); HashMap<String, PdfApplicationData> dic2 = infor2.getApplicationDatas(); for (Map.Entry<String, PdfApplicationData> item2 : dic2.entrySet()) { PdfApplicationData data2 = item2.getValue(); if (data2.getPrivate() instanceof String) { String content = data2.getPrivate().toString(); } } |
Bug | SPIREPDF-4546 | Optimized the the bold font was blurred after printing PDF. |
Bug | SPIREPDF-4788 | Fixes the issue that the application threw "ArrayIndexOutOfBoundsException" when loading PDF document. |
Bug | SPIREPDF-4798 SPIREPDF-4799 SPIREPDF-4800 SPIREPDF-4812 |
Fixes the issue that the content format was incorrect when converting PDF to Word document. |
Bug | SPIREPDF-4816 | Fixes the issue that the picture of PDF file was not displayed correctly after compressing. |
Bug | SPIREPDF-4817 | Fixes the issue that the saved stream was incorrect when converting PDF to SVG. |
Bug | SPIREPDF-4841 | Fixes the issue that it needed an open password when using WPS to open the PDF that only set a permission password. |
Bug | SPIREPDF-4842 | Fixes the issue that it was unable to get the PDF used fonts. |
Java: Add Subtotals to a Data Range in Excel
Subtotal is a built-in function in Microsoft Excel that enables you to quickly calculate a range of data using a summary function, such as SUM, AVERAGE, COUNT, or MIN. This article will demonstrate how to add subtotals to a data range in Excel in Java using Spire.XLS for Java.
Install Spire.XLS for Java
First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls</artifactId> <version>15.6.3</version> </dependency> </dependencies>
Add Subtotals to a Data Range
The XlsWorksheet.subtotal() method is used to add subtotals to a data range. It accepts the following parameters:
- IXLSRange: the specific data range.
- int: the column index (zero-based) that you want to base the subtotals on.
- int[]: an array of column indexes (zero-based) on which the subtotals are calculated.
- SubtotalTypes: the function (SUM, AVERAGE etc.) used to calculate the subtotals.
- boolean: Indicates whether to replace existing subtotals.
- boolean: Indicates whether to insert page breaks between groups.
- boolean: Indicates whether to add summary rows below data.
The following are the steps to add subtotals to a data range:
- Create an instance of Workbook class.
- Load an Excel file using Workbook.loadFromFile() method.
- Get the desired worksheet using Workbook.getWorksheets().get() method.
- Access the range that you wish to subtotal using Worksheet.getCellRange() method.
- Add subtotals to the range using XlsWorksheet.subtotal() method.
- Save the result file using Workbook.saveToFile() method.
- Java
import com.spire.xls.*; public class AddSubtotalsToDataRange { public static void main(String []args){ //Create a Workbook instance Workbook workbook = new Workbook(); //Load an Excel file workbook.loadFromFile("Report.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Access the range that contains data you wish to subtotal CellRange range = sheet.getCellRange("A2:C11"); //Add subtotals to the range, the function is Sum and it will be applied to the 3rd column in the range sheet.subtotal(range, 0, new int[] { 2 }, SubtotalTypes.Sum, true, false, true); //Save the result file workbook.saveToFile("AddSubtotal.xlsx", ExcelVersion.Version2016); workbook.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Spire.PDF for Android via Java 3.12.2 fixes the issue that the encrypted PDF document failed to be opened
We are glad to announce the release of Spire.PDF for Android via Java 3.12.2. This version fixes the issue that the encrypted PDF document failed to be opened and some of the metadata obtained was incorrect after encrypting a PDF with applying license. More details are listed below.
Here is a list of changes made in this release
Category | ID | Description |
Bug | SPIREPDF-4831 | Fixes the issue that the encrypted PDF document failed to be opened and some of the metadata obtained was incorrect after encrypting a PDF with applying license |
Spire.Doc 9.12.12 fixes the number format was incorrect when filling the merged fields
We are pleased to announce the release of Spire.Doc 9.12.12. This version fixes some known issues such as the number format was incorrect when filling the merged fields. More details are listed below.
Here is a list of changes made in this release
Category | ID | Description |
Bug | SPIREDOC-6378 | Fixes the issue that the number format was incorrect when filling the merged fields. |
Bug | SPIREDOC-7131 | Fixes the issue that the application threw an error "System.ArgumentOutOfRangeException" when updating document fields in multiple threads |
Bug | SPIREDOC-7132 | Fixes the issue that the application threw an error "Unable to cast object of type 'spr┗' to type 'spr▙"when loading file. |
Bug | SPIREDOC-7132 | Fixes the issue that the application threw an error "An item with the same key has already been added"when loading file. |
Bug | SPIREDOC-7132 | Fixes the issue that the application threw an error "Byte array for GUID must be exactly 16 bytes long" when loading file. |
Bug | SPIREDOC-7132 | Fixes the issue that the application threw an error "Unable to read beyond the end of the stream" when loading file. |
Bug | SPIREDOC-7132 | Fixes the issue that the application threw an error "Non-negative number required. Parameter name: value" when loading file. |
Bug | SPIREDOC-7132 | Fixes the issue that the application threw an error "Input string was not in a correct format" when loading file. |
Bug | SPIREDOC-7132 | Fixes the issue that the application threw an error "Unknown color type encountered during WordML import - #0" when loading file. |
Bug | SPIREDOC-7132 | Fixes the issue that the application threw an error "Object reference not set to an instance of an object" when loading file. |
Spire.Presentation for Java 4.12.3 supports converting PowerPoint file to OFD
We are glad to announce the release of Spire.Presentation for Java 4.12.3. This version supports converting PowerPoint file to OFD as well as supports getting the image path of the PowerPoint file. In addition, it fixes some known issues such as the content was lost after saving a slide to image. More details are listed below.
Here is a list of changes made in this release
Category | ID | Description |
New feature | SPIREPPT-1778 | Supports getting the image path of the PowerPoint file.
ImageCollection images = ppt.getImages(); for (int i = 0; i < images.size(); i++) { IImageData imageData = images.get(i); String path = imageData.getRelativePath(); } |
New feature | SPIREPPT-1780 | Supports converting PPT file to OFD.
Presentation ppt = new Presentation(); ppt.loadFromFile(inputFile); //Save the PowerPoint to OFD file format ppt.saveToFile(outputFile, FileFormat.OFD); ppt.dispose(); |
Bug | SPIREPPT-1777 | Fixes the issue that the application threw "ArrayIndexOutOfBoundException" when getting Chart data. |
Bug | SPIREPPT-1781 | Fixes the issue that the content was lost after saving a slide to image. |
Spire.Doc for Java 4.12.13 supports setting "Allow Latin text to wrap in the middle of a word" for paragraphs
We are pleased to announce the release of Spire.Doc for Java 4.12.13. This version supports setting "Allow Latin text to wrap in the middle of a word" for paragraphs, and enhances the conversion from Word to PDF, XML to PDF. In addition, it fixes some known issues such as the text obtained were incorrect. More details are listed below.
Here is a list of changes made in this release
Category | ID | Description |
New feature | SPIREDOC-5499 | Supports setting "Allow Latin text to wrap in the middle of a word" for paragraphs.
Paragraph pa = document.getSections().get(0).getParagraphs().get(0); pa.getFormat().setWordWrap(false); pa.getFormat().getWordWrap(); |
Bug | SPIREDOC-6592 | Fixes the issue that the objects name were inconsistent while openning the saved Word document via WPS. |
Bug | SPIREDOC-6932 | Fixes the issue that the content format was incorrect when converting Word file to PDF. |
Bug | SPIREDOC-6958 | Fixes the issue that the application threw "OutOfMemoryError" when converting Word file to PDF. |
Bug | SPIREDOC-6961 SPIREDOC-7022 |
Fixes the issue that the text obtained were incorrect. |
Bug | SPIREDOC-6967 | Fixes the issue that the content was overlapped after converting Word file to PDF. |
Bug | SPIREDOC-6985 | Fixes the issue that the content was incorrect after converting xml file to PDF. |
Bug | SPIREDOC-6988 SPIREDOC-7072 |
Fixes the issue that the application hung a long time but no output file when converting Word file to PDF. |
Bug | SPIREDOC-7048 | Fixes the issue that the application threw "ArrayIndexOutOfBoundException" when converting a Word document that was inserted table data to PDF. |
Bug | SPIREDOC-7052 | Fixes the issue that the table column width setting didn't take effect after converting a Word document to PDF. |
Bug | SPIREDOC-7073 | Fixes the issue that the application threw ""IllegalArgumentException"" when loading the Word document that after doing compare. |
Bug | SPIREDOC-7075 | Fixes the issue that the picture position was wrong after converting a Word document to PDF. |
Bug | SPIREDOC-7127 | Fixes the issue that the application threw "ClassCastException" when converting the Word document which was embedded OpenType fonts to PDF. |
Bug | SPIREDOC-7128 | Fixes the issue that the text position was incorrect after accept changes. |
Bug | SPIREDOC-7145 | Fixes the issue that the application threw "Item has already been added" error when loading a Word document. |
Bug | SPIREDOC-7150 | Fixes the issue that the application threw "NullPointerException" when loading a Word document. |
Spire.Presentation 6.12.4 supports adding the setting to convert high quality images
We are glad to announce the release of Spire.Presentation 6.12.4. This version supports .NET6.0, supports adding the setting to convert high quality images, and supports getting the type and color of slide background. It also enhences the conversion from PowerPoint to PDF, PowerPoint to image. In addition, it fixes some known issues such as the obtained display color of table cell text was incorrect. More details are listed below.
Here is a list of changes made in this release
Category | ID | Description |
New feature | SPIREPPT-1699 | Supports getting the type and color of slide background.
ISlide sld = ppt.Slides[0] as ISlide; FillFormat bg = sld.DisplaySlideBackground.GetBackgroundFillFormat(sld as ActiveSlide); switch (bg.FillType) { case FillFormatType.Solid: sb.AppendLine("type:" + bg.FillType + "\tcolor:" + bg.SolidColor.Color); break; case FillFormatType.Gradient: sb.AppendLine("type:" + bg.FillType); foreach (GradientStop stop in bg.Gradient.GradientStops) { sb.AppendLine("position:" + stop.Position + "\tcolor:" + stop.Color.Color); } break; default: sb.AppendLine("type:" + bg.FillType); break; } |
New feature | SPIREPPT-1766 | Supports adding the setting to convert high quality images.
Presentation.HighQualityImage = true; |
Bug | SPIREPPT-1748 | Optimized the memory consumption when operating PowerPoint file image. |
Bug | SPIREPPT-1752 | Fixes the issue that the application threw "System.NullReferenceException" when getting audio data. |
Bug | SPIREPPT-1753 | Fixes the issue that the grid line color obtained was incorrect. |
Bug | SPIREPPT-1756 | Fixes the issue that the "Duration" time obtained was incorrect. |
Bug | SPIREPPT-1757 | Fixes the issue that the obtained display color of table cell was incorrect. |
Bug | SPIREPPT-1758 | Fixes the issue that the color of math equation was lost after saving slide as image. |
Bug | SPIREPPT-1759 | Fixes the issue that the content was incorrect after saving SmartArt to image. |
Bug | SPIREPPT-1764 | Fixes the issue that the application threw "System.IndexOutOfRangeException" when saving shape to image. |
Bug | SPIREPPT-1768 | Fixes the issue that the returned value of "HasBullet" was wrong . |
Bug | SPIREPPT-1770 | Fixes the issue that the math equation lost after converting PowerPoint to PDF. |
Bug | SPIREPPT-1771 | Fixes the issue that the application threw "System.NullReferenceException" when encrypting a PowerPoint file that contains math equations. |
Bug | SPIREPPT-1772 | Fixes the issue that the obtained display color of table cell text was incorrect. |
Bug | SPIREPPT-1775 | Fixes the issue that the obtained value of "VerticalTextType" was incorrect. |
Bug | SPIREPPT-1783 | Fixes the issue that some content was not clear after saving PowerPoint to image. |
Bug | SPIREPPT-1785 | Fixes the issue that the comments weren't able to obtained. |
Bug | SPIREPPT-1796 | Fixes the issue that the leader lines of Datalabel were incorrect when converting PowerPoint to PDF. |
Bug | SPIREPPT-1800 | Fixes the issue that the obtained PresetClassType value of animation was incorrect. |
Bug | SPIREPPT-1801 | Fixes the issue that the color of chart trend line was lost after saving PowerPoint to PDF. |