当前位置 : 主页 > 编程语言 > c++ >

android读取Excel(jxl 支持xls和xlsx)

来源:互联网 收集:自由互联 发布时间:2021-07-03
ExcelUtils public class ExcelUtils { public static List readExcel(String filePath) throws Exception { return readExcel(filePath, 0); } public static List readExcel(String filePath, int sheet) throws Exception { if (filePath.trim().toLowerCa
ExcelUtils
public class ExcelUtils {  
    public static List
 
   readExcel(String filePath) throws Exception {  
        return readExcel(filePath, 0);  
    }  
  
    public static List
  
    readExcel(String filePath, int sheet) throws Exception { if (filePath.trim().toLowerCase().endsWith(".xls")) { return readXLS(new File(filePath), sheet); } else { return readXLSX(new File(filePath), sheet); } } public static List
   
     readXLS(File file, int sheetIndex) throws Exception { List
    
      list = new ArrayList
     
      (); Workbook workbook = null; try { workbook = Workbook.getWorkbook(file); Sheet sheet = workbook.getSheet(sheetIndex); int columnCount = sheet.getColumns(); int rowCount = sheet.getRows(); for (int i = 0; i < rowCount; i++) { String[] item = new String[columnCount]; for (int j = 0; j < columnCount; j++) { Cell cell = sheet.getCell(j, i); String str = ""; if (cell.getType() == CellType.NUMBER) { str = ((NumberCell) cell).getValue() + ""; } else if (cell.getType() == CellType.DATE) { str = "" + ((DateCell) cell).getDate(); } else { str = "" + cell.getContents(); } item[j] = str; } list.add(item); } } finally { if (workbook != null) workbook.close(); } return list; } public static List
      
        readXLSX(File file, int sheet) throws Exception { List
       
         list = new ArrayList
        
         (); ArrayList
         
           item = new ArrayList
          
           (); String v = null; boolean flat = false; List
           
             ls = new ArrayList
            
             (); ZipFile xlsxFile = new ZipFile(file); ZipEntry sharedStringXML = xlsxFile .getEntry("xl/sharedStrings.xml"); InputStream inputStream = xlsxFile.getInputStream(sharedStringXML); XmlPullParser xmlParser = Xml.newPullParser(); xmlParser.setInput(inputStream, "utf-8"); int evtType = xmlParser.getEventType(); while (evtType != XmlPullParser.END_DOCUMENT) { switch (evtType) { case XmlPullParser.START_TAG: String tag = xmlParser.getName(); if (tag.equalsIgnoreCase("t")) { ls.add(xmlParser.nextText()); } break; case XmlPullParser.END_TAG: break; default: break; } evtType = xmlParser.next(); } ZipEntry sheetXML = xlsxFile.getEntry("xl/worksheets/sheet" + (sheet + 1) + ".xml"); InputStream inputStreamsheet = xlsxFile.getInputStream(sheetXML); XmlPullParser xmlParsersheet = Xml.newPullParser(); xmlParsersheet.setInput(inputStreamsheet, "utf-8"); int evtTypesheet = xmlParsersheet.getEventType(); while (evtTypesheet != XmlPullParser.END_DOCUMENT) { switch (evtTypesheet) { case XmlPullParser.START_TAG: String tag = xmlParsersheet.getName(); if (tag.equalsIgnoreCase("row")) { } else if (tag.equalsIgnoreCase("c")) { String t = xmlParsersheet.getAttributeValue(null, "t"); if (t != null) { flat = true; System.out.println(flat + "有"); } else { System.out.println(flat + "没有"); flat = false; } } else if (tag.equalsIgnoreCase("v")) { v = xmlParsersheet.nextText(); if (v != null) { if (flat) { item.add(ls.get(Integer.parseInt(v))); } else { item.add(v); } } } break; case XmlPullParser.END_TAG: if (xmlParsersheet.getName().equalsIgnoreCase("row") && v != null) { list.add(item.toArray(new String[item.size()])); item = new ArrayList
             
              (); } break; } evtTypesheet = xmlParsersheet.next(); } return list; } public static int writeExcel(List
              
               > data_list, OutputStream os){ try { WritableWorkbook book = Workbook.createWorkbook(os); WritableSheet sheet1 = book.createSheet("sheet1", 0); for (int i = 0; i < data_list.size(); i++) { List
                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()); sheet1.addCell(label); } } book.write(); book.close(); } catch (Exception e) { e.printStackTrace(); return -1; } return 0; } public static int writeExcel(String file_name, List
                
                 > data_list) { try { WritableWorkbook book = Workbook.createWorkbook(new File(file_name)); WritableSheet sheet1 = book.createSheet("sheet1", 0); for (int i = 0; i < data_list.size(); i++) { List
                  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()); sheet1.addCell(label); } } book.write(); book.close(); } catch (Exception e) { e.printStackTrace(); return -1; } return 0; } } 
                
              
             
            
           
          
         
        
       
      
     
    
   
  
 
网友评论