When exposing reports through Excel files in Android, the open source library jxl is mainly used. It was first used on Java, but it can also be used on Android. Similar POIs, because there are many dependencies, can only be used for java, not Android.
Using jxl requires importing packages in Android projects. jxl can complete the basic read and write operations of Excel. The support and non-support are as follows:
1. jxl only supports Excel2003 format and does not support Excel2007 format. That is, it supports xls files, but does not support xlsx files.
2. jxl does not support direct modification of excel files, but can be modified indirectly by copying the new file overwriting the original file.
3. jxl can only recognize pictures in PNG format, but not pictures in other formats.
As can be seen above, jxl does not support Excel2007, which is very bad, especially when Excel2007 has become the mainstream format of Excel. However, there is a temporary method to implement Android reading 2007 format. If we carefully analyze the file format of xlsx, we will find that the xlsx file is actually a compressed package. There are various files in the compressed package, and the data is generally placed in "xl/" and "xl/worksheets/". Based on this, when we judge that the Excel file is in 2007 format, we can decompress it, then extract the sum from it, and then use the XML parsing tool to parse the specific data.
Below is an example of the read and write code of Excel files, which supports read and write in the 2003 format and read in the 2007 format:
import ; import ; import ; import ; import ; import ; import ; import ; import .; import .; import ; import ; import ; import ; import ; import ; import ; public class ExcelUtil { private final static String TAG = "ExcelUtil"; public static List<List<Object>> read(String file_name) { String extension = file_name.lastIndexOf(".") == -1 ? "" : file_name .substring(file_name.lastIndexOf(".") + 1); if ("xls".equals(extension)) {// 2003 (TAG, "read2003XLS, extension:" + extension); return read2003XLS(file_name); } else if ("xlsx".equals(extension)) { (TAG, "read2007XLSX, extension:" + extension); return read2007XLSX(file_name); } else { (TAG, "Unsupported file type, extension:" + extension); return null; } } public static List<List<Object>> read2003XLS(String path) { List<List<Object>> dataList = new ArrayList<List<Object>>(); try { Workbook book = (new File(path)); // (); // Get the number of sheet pages // Get the first worksheet object Sheet sheet = (0); int Rows = (); int Cols = (); (TAG, "The name of the current worksheet:" + ()); (TAG, "Total row count:" + Rows + ", Total column count:" + Cols); List<Object> objList = new ArrayList<Object>(); String val = null; for (int i = 0; i < Rows; i++) { boolean null_row = true; for (int j = 0; j < Cols; j++) { // getCell(Col,Row) gets the value of the cell. Note that getCell format is to column first and row first and then row, not a common row first and then column first. (TAG, ((j, i)).getContents() + "\t"); val = ((j, i)).getContents(); if (val == null || ("")) { val = "null"; } else { null_row = false; } (val); } (TAG, "\n"); if (null_row != true) { (objList); null_row = true; } objList = new ArrayList<Object>(); } (); } catch (Exception e) { (TAG, ()); } return dataList; } public static List<List<Object>> read2007XLSX(String path) { List<List<Object>> dataList = new ArrayList<List<Object>>(); String str_c = ""; String v = null; boolean flat = false; List<String> ls = new ArrayList<String>(); try { ZipFile xlsxFile = new ZipFile(new File(path)); ZipEntry sharedStringXML = ("xl/"); if (sharedStringXML == null) { (TAG, "Empty File:" + path); return dataList; } InputStream inputStream = (sharedStringXML); XmlPullParser xmlParser = (); (inputStream, "utf-8"); int evtType = (); while (evtType != XmlPullParser.END_DOCUMENT) { switch (evtType) { case XmlPullParser.START_TAG: String tag = (); if (("t")) { (()); } break; case XmlPullParser.END_TAG: break; default: break; } evtType = (); } ZipEntry sheetXML = ("xl/worksheets/"); InputStream inputStreamsheet = (sheetXML); XmlPullParser xmlParsersheet = (); (inputStreamsheet, "utf-8"); int evtTypesheet = (); List<Object> objList = new ArrayList<Object>(); String val = null; boolean null_row = true; while (evtTypesheet != XmlPullParser.END_DOCUMENT) { switch (evtTypesheet) { case XmlPullParser.START_TAG: String tag = (); if (("row")) { } else if (("c")) { String t = (null, "t"); if (t != null) { flat = true; // String type // (TAG, flat + "yes"); } else { // Non-string type, maybe integer type // (TAG, flat + "No"); flat = false; } } else if (("v")) { v = (); if (v != null) { if (flat) { str_c += ((v)) + " "; val = ((v)); null_row = false; } else { str_c += v + " "; val = v; } (val); } } break; case XmlPullParser.END_TAG: if (().equalsIgnoreCase("row") && v != null) { str_c += "\n"; if (null_row != true) { (objList); null_row = true; } objList = new ArrayList<Object>(); } break; } evtTypesheet = (); } (TAG, str_c); } catch (ZipException e) { (); } catch (IOException e) { (); } catch (XmlPullParserException e) { (); } if (str_c == null) { str_c = "There is a problem with the parsing file"; (TAG, str_c); } return dataList; } public static int writeExcel(String file_name, List<List<Object>> data_list) { try { WritableWorkbook book = (new File(file_name)); WritableSheet sheet1 = ("sheet1", 0); for (int i = 0; i < data_list.size(); i++) { List<Object> obj_list = data_list.get(i); for (int j = 0; j < obj_list.size(); j++) { Label label = new Label(j, i, obj_list.get(j).toString()); (label); } } (); (); } catch (Exception e) { (); return -1; } return 0; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.