Use JXLS Reader to analyze the steps and precautions of the Excel file
Use JXLS Reader to analyze the steps and precautions of the Excel file
JXLS is a Java library based on Apache Poi, which is used to read and write Excel files.It provides a simple way to parse the Excel file and converts the data into a Java object.Below is the steps and precautions for analyzing the Excel file using JXLS Reader.
step:
1. First, make sure that your project already contains the dependencies of the JXLS library.You can add JXLS dependencies in the project construction file (such as Maven or Gradle).
2. Create an InputStream object for reading Excel files.You can use FileInPutStream to create an INPUTSTREAM object read from the file system, or use ClassLoader.GetResourceASSTREAM method to create the InputStream object read from the class path.
InputStream inputStream = new FileInputStream("path/to/excel.xlsx");
// or
InputStream inputStream = YourClass.class.getClassLoader().getResourceAsStream("path/to/excel.xlsx");
3. Create a Context object that will save the context information during the parsing process, such as the workheet name and the Java class.
Context context = new Context();
context.putvar ("data", youdatalist); // Put the Java object to be mapped into Context
4. Create an XLSREADER object and initialize using the INPUTSTREAM objects and Context objects created above.
XLSReader xlsReader = ReaderBuilder.buildFromXML(inputStream);
xlsReader.read(context);
5. The analysis process will fill the data into the Java object according to the structure and mapping of the Java class of the Excel file.
6. Finally, access the filling Java objects after obtaining data from the context.
List<YourDataClass> filledDataList = (List<YourDataClass>) context.getVar("data");
Precautions:
1. The columns in the Excel file and the attributes in Java objects should have the same order in order to correctly map data.
2. The name of the excel file should be corresponding to the attribute name of the Java object.You can use the format of $ {PropertyName} on the column name in Excel templates, and then set the corresponding attribute value in the context.
3. In the Excel template, you can use a variety of basic Excel formulas and functions, such as Sum, if, vLookup, etc. to process data.
4. JXLS supports reading complex Excel files with grouping and additional computing. It can use Jexl expressions to achieve more complicated logic during the analysis process.
Below is an example code that uses JXLS Reader to analyze the excel file:
import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.XLSReader;
import org.jxls.common.Context;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
public class ExcelParser {
public static void main(String[] args) throws Exception {
InputStream inputStream = new FileInputStream("path/to/excel.xlsx");
Context context = new Context();
context.putVar("data", new YourDataList());
XLSReader xlsReader = ReaderBuilder.buildFromXML(inputStream);
xlsReader.read(context);
List<YourDataClass> filledDataList = (List<YourDataClass>) context.getVar("data");
for (YourDataClass data : filledDataList) {
System.out.println(data.toString());
}
}
}
This is the steps and precautions that use JXLS Reader to analyze the excel file.Remember, using JXLS can easily convert the data in the Excel file into the Java object, so as to easily proceed and operate.