Introduction to the method of converting XML to Excel in Java

The following content introduces the method of converting an XML file into a standard Excel file through the Java back-end program code. In this conversion test, take the conversion to .xlsx and .xls file formats as an example. The code is for reference.

1. Program Test Environment

  • Test file: XML file

  • Excel test version: 2013 and 03

  • Compilation environment: IntelliJ IDEA 2018

  • JDK version: 1.8.0

  • Excel library version: spire.xls.jar 4.3.4

2. Steps and methods

(1) Create a Java project in IDEA, and save the xml file for testing in the project folder path. For example, the test path of this program is C:\Users\Administrator\IdeaProjects\Conversion_XLS; the xml file is as shown in the figure below As shown, use the idea program to open the effect:

xml.png

(2) Java code

import com.spire.xls.*;
 
 public class xmltoexcel {
     public static void main(String[] args) {
         //Load xml file
         Workbook wb = new Workbook();
         wb.loadFromXml("test.xml");
 
         //Convert to the 2013 version of Excel in xlsx format
         wb.saveToFile("xmltoExcel.xlsx",FileFormat.Version2013);
 
         //Convert to version 03 of Excel in xls format
         wb.saveToFile("xmltoExcel.xls",FileFormat.Version97to2003);
     }
 }

(3) Run the program to generate the converted result file, as shown in the figure below,

to.png

The result document path is the project folder path, and the path can be customized separately.

You can follow the official account [Office Document Development] to view more methods for operating Office documents in Java.


For reprint, please indicate the source!

(Finish)




Guess you like

Origin blog.51cto.com/miayo/2678196