Easyxls
Using easyxls makes it easy for you to operate Excel.This project aims to solve the import and export of simple (without any style) XLS files.
Jaxb is used for XML processing, so jdk1.6u14 and later versions are required. Jaxb is prone to garbled characters when processing Chinese XML. Therefore, the XML is limited to GBK encoding, and the storage format in windwos is ANSI.
Project address: http://git.oschina.net/free/EasyXls
Easyxls features
Supports XLS conversion to list <pojo> objects
Supports XLS conversion to list <map> objects
Convert list <pojo> to xls
Supports list <map> to xls
Supports xml configuration
Supports Java coding to create config configurations
XML Configuration Wizard
The project contains a simple XML generation wizard. When using this wizard, you must create the main method in the project and call the following method:
EasyXls.openGenerater();
Supported column types
- Int
- Long
- Float
- Double
- Java. Lang. Integer
- Java. Lang. Long
- Java. Lang. Float
- Java. Lang. Double
- Java. util. Date
- Java. Math. bigdecimal
Project dependency
<dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.12</version></dependency>
Configuration item description
Some attributes are valid only when they are written to XLS. These attributes are marked as "write", and some are marked as "read" Only when reading XLS. common attributes are not marked.
Overall Configuration
Cache:When using xml configuration, you can choose whether to enable the cache. After enabling the cache, the xml configuration file will not be parsed repeatedly, which will improve the efficiency of repeated use. However, if the xml configuration is modified, the new configuration does not take effect.
Sheet:Sheet label name [write]
Class:Pojo class or map corresponding to excel
Sheetnum:Read several sheet pages [read]
Startrow:Read from the first row]
Maxrow:The maximum number of read rows. If the number of rows exceeds the limit, no read will be performed]
Column Configuration
Name:Corresponding Java field name
Header:Corresponding Excel title name [write]
Type:The type of the corresponding column. If this parameter is not set, if pojo is used, the field type is automatically matched. If the map is used, use the xls Format [read]
Width:Corresponding column width (unit: pixels) [write]
Key:Columns that must contain values. If the column is empty, the system stops reading data. It mainly prevents reading blank rows. Only the first column with key = true is valid]
Example
The following code is from the test code (under the test directory)
Some XML:
<? XML version = "1.0" encoding = "GBK" standalone = "yes"?> <Excel> <cache> false </cache> <sheet> one-time fee </sheet> <class> Po. charges </class> <sheetnum> 0 </sheetnum> <startrow> 1 </startrow> <columns> <column> <Name> year </Name>
Use xml configuration to read XLS:
@Testpublic void testMap() { try { List list = EasyXls.xls2List( Xls2ListTest.class.getResource("/ChargesMap.xml").getPath(), new File(Xls2ListTest.class.getResource("2.xls").getPath())); System.out.println(list); } catch (Exception e) { e.printStackTrace(); }}
Use Java code to create a Config and read XLS:
@ Testpublic void testconfig () {try {// create a configuration excelconfig Config = new excelconfig. builder (charges. class ). sheetnum (0 ). startrow (1 ). key ("name "). addColumn ("year", "communityid", "roomno", "ownersid", "ownersname", "property "). build (); List list = easyxls.xls 2 List (config, xls2listtest. class. getresourceasstream ("2.xls"); For (INT I = 0; I <list. size (); I ++) {system. out. println (Charges) list. get (I )). getownersname () ;}} catch (exception e) {e. printstacktrace ();}}
The column processing here is very simple. The columns in the Excel file correspond to the columns in the column one by one.
Use xml configuration to read data and use config to write data to a new Excel file:
@ Testpublic void testmap2 () {inputstream is = xls2listtest. class. getresourceasstream ("2.xls"); try {string xmlpath = xls2listtest. class. getresource ("/chargesmap. XML "). getpath (); List list = easyxls.xls 2 List (xmlpath, is); map = new hashmap (); map. put ("year", 2013); map. put ("ownersname", "test owner"); list. add (MAP); easyxls. list2xls (list, xmlpath, "d:/", "testmap.xls"); excelconfig Config = new excelconfig. builder (charges. class ). sheetnum (0 ). startrow (1 ). separater (","). key ("name "). addColumn ("year, year", "communityid, residential ID", "roomno, house number", "ownersid, ownerid", "ownersname, owner name", "property, license fee "). build (); easyxls. list2xls (config, list, "d:/", "testmap2.xls");} catch (exception e) {e. printstacktrace ();}}
To facilitate the call, use the. Separater (",") method to set the separator (the default is an English comma), and then assign values in the AddColumn method, for example, "year, year.
This assignment method supports up to four parameters, corresponding to name, header, width, type, for example:
"Year, year, 200, java. Lang. Integer"
You do not need to write type when using pojo classes. When using map, you can use type to specify the type.
Quick and convenient Excel import and export tool easy-release of XLS 1.0