Excel
apache provides Java developers with a set of tools for reading and writing Excel tables: POI. For a novice, it is not easy to write a complex reading and writing process for each reading and writing POI. Here, the editor encapsulates a set of simple and easy-to-use Excel reading and writing tools for everyone.
annotation | describe |
---|---|
@ExcelReadCell | Name Tag header name |
@ExcelTable | Used to specify the table name on the class |
@ExcelWriteAdapter | Expand the Data Set Adapter |
@ExcelWriteCell | Output file edit column name and column number information. |
@ExcelReadAggregate | Tag class member variables are used to save data without tags |
Read the excel file:
Data source (Table name: Test Table 1):
Item code | Item name | Storage location | Remark | date |
---|---|---|---|---|
TY122635 | Kitchen-Dough Separator | 0 | SDS-30S | 2021.2.1 |
TY122654 | Black and white laser printer | 0 | Brother HL-5590DN | 2021.2.2 |
TY122652 | Black and white laser printer | 0 | Brother HL-5590DN | 2021.2.3 |
TY122634 | Desktop Computer | 0 | Lenovo ThinkCentre M710t-D749 | 2021.2.4 |
Create an entity object:
@ExcelTable(sheetName = "Test Table 1") public class Table { @ExcelReadCell(name = "Storage Location") public String storageLocation; @ExcelReadCell(name = "Item Name") public String name; @ExcelReadCell(name = "Item code") public String code; //Specify this variable to save other data, and it can also not be processed @ExcelReadAggregate public String extend; }
Only three columns of data are specified here. Other data columns (notes, dates) are not specified. They will be aggregated and saved to **@ExcelReadAggregateTag the extend variable, of course, if you do not need this data, you can use it without declaring the variable.@ExcelReadAggregate** annotation.
The object marked by @ExcelReadAggregate receives a JsonArray String object.
Table { storageLocation='0', note='SDS-30S', name='Kitchen-Dough Divider', code='TY2023122635', extend= '[{"name":"date","value":"2021.2.1","index":7},{"name":"Remark","value":"SDS30S","index":8}]'
Use:
().readWith(is).doReadXLSX(new IParseListener<Table>() { @Override public void onStartParse() { } @Override public void onParse(Table test, JSONArray jsonArray) { } @Override public void onParseError(Exception e) { } @Override public void onEndParse() { } }, );
Output excel file:
@ExcelTable(sheetName = "Test Table 1") public class Table { @ExcelWriteCell(writeIndex = 2, writeName = "Storage Location") public String storageLocation; @ExcelWriteCell(writeIndex = 1, writeName = "Item Name") public String name; @ExcelWriteCell(writeIndex = 0, writeName = "Item code") public String code; //If you aggregate multiple data in a certain variable, you can use IConvertParserAdapter interface to process the data to correctly write the file @ExcelWriteAdapter(adapter = ) public String extend; }
@ExcelWriteCell
ExcelWriteCell annotation has two properties: writeIndex specifies the column to which the data belongs, writeName specifies the column name
@ExcelWriteAdapter
ExcelWriteAdapter is used to assist tools to correctly write user-defined aggregated data.
The data of extend here is as follows:
[ { "name":"date", "value":"2021.2.9", "index":3 }, { "name":"Remark", "value":"1.0", "index":4 } ]
Name represents the column name, value represents the value, and index represents the column number. The data structure here can be defined by yourself.
IConvertParserAdapter Interface
Using aggregated data, you need to implement the IConvertParserAdapter interface to parse your aggregated data and use itISheetInformation on the column name, value, column number, etc. of the interface callback data.
For the above aggregated data:
public class JsonArrayConvertAdapter implements IConvertParserAdapter { @Override public void convert(ISheet sheet, Object o) { JSONArray jsonArray = null; try { jsonArray = new JSONArray((String) o); } catch (JSONException e) { (); } for (int i = 0; i < (); i++) { JSONObject json = (JSONObject) (i); String name = (String) ("name"); Object value = ("value"); int index = (int) ("index"); (name, value, index); } } }
@ExcelWriteAdapter usage method:
@ExcelWriteAdapter(adapter = ) public String extend;
Use:
().writeWith(file).doWrite(new IWriteListener() { @Override public void onStartWrite() { (TAG, "onStartWrite: "); } @Override public void onWriteError(Exception e) { (TAG, "onWriteError: "+e); } @Override public void onEndWrite() { (TAG, "onEndWrite: "); } },data);
gitee address:/Jian-Hu/Exc…
Github address:/Andihu/Exce…
The above is the detailed content of Android's elegant reading and writing Excel. For more information about Android's reading and writing Excel, please follow my other related articles!