excel文件讀取返回map(xls,xlsx)

1.xlsjava

package com.cmos.ngoccontrol.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.cmos.core.logger.Logger;
import com.cmos.core.logger.LoggerFactory;

public class XlsReaderUtil {
    
    //日誌
    private static final Logger LOGGER = LoggerFactory.getActionLog(XlsReaderUtil.class);
    //用來標識已經中斷解析
    private static boolean terminateParseFlag = false;
    /*//源表頭
      private List<String> title = new ArrayList<>();
      //源表數據
      private List<Map<String, Object>> result = new ArrayList<>();
      //源文件中缺乏字段信息的無效數據信息記錄
      private List<String> errorList = new ArrayList<String>();*/
    /** 獲取單元格的值
     * 
     * @param cell
     * @return
     */
    private static String getCellValue(Cell cell) {
        String cellValue = "";
        DataFormatter formatter = new DataFormatter();
        if (cell != null) {
            // 判斷單元格數據的類型,不一樣類型調用不一樣的方法
            switch (cell.getCellType()) {
            // 數值類型
            case Cell.CELL_TYPE_NUMERIC:
                // 進一步判斷 ,單元格格式是日期格式
                if (DateUtil.isCellDateFormatted(cell)) {
                    cellValue = formatter.formatCellValue(cell);
                } else {
                    // 數值(手機號)
                    double value = cell.getNumericCellValue();
                    DecimalFormat df = new DecimalFormat("#");
                    cellValue = df.format(value);
                }
                break;
            case Cell.CELL_TYPE_STRING:
                cellValue = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            // 判斷單元格是公式格式,須要作一種特殊處理來獲得相應的值
            case Cell.CELL_TYPE_FORMULA: {
                try {
                    cellValue = String.valueOf(cell.getNumericCellValue());
                } catch (IllegalStateException e) {
                    cellValue = String.valueOf(cell.getRichStringCellValue());
                    LOGGER.error("getCellValue() error",e);
                }
            }
                break;
            case Cell.CELL_TYPE_BLANK:
                cellValue = "";
                break;
            case Cell.CELL_TYPE_ERROR:
                cellValue = "";
                break;
            default:
                cellValue = cell.toString().trim();
                break;
            }
        }
        return cellValue.trim();
    }
    
    /**
     * 
     * @param filePath 臨時文件路徑
     * @param sourceFields 源文件字段
     * @param targetFields 目標文件字段
     * @param errorList 錯誤數據(例如:第幾行數據錯誤,錯誤緣由:字段長度與文件標題長度不一致)
     * @param dispose 處理後前臺須要展現的數據(文件總行數,實際行數(正確數據行數),錯誤行數)
     * @return
     * @throws IOException
     */
    public static Map<String, Object> readExcel(String path, String splitStr, int controlParseNum) {
        //源表頭
          List<String> title = new ArrayList<String>();
          //源表數據
          List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
          //源文件中缺乏字段信息的無效數據信息記錄
          List<String> errorList = new ArrayList<String>();
        Map<String, Object> mapR = new HashMap<String, Object>();
        FileInputStream inStream = null;
        Workbook workBook = null;
        Sheet sheet = null;
        int rowNum = 0;
        try {
            inStream = new FileInputStream(path);
            workBook = WorkbookFactory.create(inStream);
            sheet = workBook.getSheetAt(0);
            // 獲取總行數
            rowNum = sheet.getLastRowNum() + 1;
            for (int i = 0; i < rowNum; i++) {
                    if(!terminateParseFlag && controlParseNum != -1){
                        Row row = sheet.getRow(i);
                        if (i == 0) {
                            for (int j = 0; j < row.getLastCellNum(); j++) {
                                // 讀取第一行 存入標題
                                Cell cell = row.getCell(j);
                                // 獲取單元格的值
                                String str = getCellValue(cell);
                                title.add(str);
                            }
                        } else {
                            Map<String, Object> beanRow=new HashMap<String, Object>();
                            for(int k = 0; k <title.size();k++){
                                // 讀取數據行
                                Cell cell = row.getCell(k);
                                // 獲取單元格的值
                                String str = getCellValue(cell);
                                beanRow.put(title.get(k), "".equals(str) ? " " : str);
                            }
                            result.add(beanRow);
                        }
                        if(i  == controlParseNum){
                            terminateParseFlag = true;
                            /* throw new IOException();*/
                            break;
                        }

                    }
                }
        } catch (Exception e) {
            LOGGER.error("readExcelWithTitle() error",e);
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    LOGGER.error("FileInputStream close() error",e);
                }
            }
        }
        mapR.put("title", title);
        mapR.put("result", result);
        mapR.put("total", rowNum);
        mapR.put("errorList", errorList);
        return mapR;
    }
    
    /*public static void main(String[] args) throws IOException {
        Map<String, Object> map = XlsReaderUtil.readExcel("D://test.xls", ",", 2);
        System.out.println(map);
    }*/
}
View Code

2.xlsxapache

package com.cmos.ngoccontrol.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import com.cmos.core.logger.Logger;
import com.cmos.core.logger.LoggerFactory;

public class XlsxReaderUtil extends DefaultHandler {  
    //日誌
    private static final Logger LOGGER = LoggerFactory.getActionLog(XlsxReaderUtil.class);
    // 共享字符串表  
    private SharedStringsTable sst;  
    // 上一次的內容  
    private String   lastContents;  
    private boolean  nextIsString;  
    private boolean  cellNull;  
  
    private int sheetIndex = -1;  
    private List<String> rowlist = new ArrayList<String>();  
    // 當前行  
    private int curRow = 0;  
    // 當前列  
    private int curCol     = 0;  
    
    private int totalRow = 0;
    //用來控制解析多少行
      private int controlParseNum = 0;
      //用來標識已經中斷解析
      private boolean terminateParseFlag = false;
      //源表頭
    private List<String> title = new ArrayList<String>();
    //源表數據
    private List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
    //源文件中缺乏字段信息的無效數據信息記錄
    private List<String> errorList = new ArrayList<String>();
    private String path;
    
    public XlsxReaderUtil() {
        super();
    }
          
    public XlsxReaderUtil(String path, int controlParseNum) {
        super();
        this.path = path;
        this.controlParseNum = controlParseNum;
    }
  
    /** 
     * 只遍歷一個電子表格,其中sheetId爲要遍歷的sheet索引,從1開始,1-3 
     *  
     * @param filename 
     * @param sheetId 
     * @throws Exception 
     */  
    /*public void processOneSheet(String filename, int sheetId) throws Exception {  
        OPCPackage pkg = OPCPackage.open(filename);  
        XSSFReader r = new XSSFReader(pkg);  
        SharedStringsTable sst = r.getSharedStringsTable();  
        XMLReader parser = fetchSheetParser(sst);  
  
        // 根據 rId# 或 rSheet# 查找sheet  
        InputStream sheet2 = r.getSheet("rId" + sheetId);  
        sheetIndex++;  
        InputSource sheetSource = new InputSource(sheet2);  
        parser.parse(sheetSource);  
        sheet2.close();  
    }  */
  
  
    /** 
     * 遍歷工做簿中全部的電子表格 
 
     *  
     * @param filename 
     * @throws Exception 
     */  
    public Map<String, Object> process(){  
        System.out.println(System.currentTimeMillis());
        XMLReader parser = null;
        Iterator<InputStream> sheets = null;
        try {
            OPCPackage pkg = OPCPackage.open(path);  
            XSSFReader r = new XSSFReader(pkg);  
            SharedStringsTable sst = r.getSharedStringsTable();  
  
            parser = fetchSheetParser(sst);
  
            sheets = r.getSheetsData();
        } catch (InvalidFormatException e) {
            LOGGER.error("process() InvalidFormat error", e);
        } catch (IOException e) {
            LOGGER.error("process() IO error", e);
        } catch (OpenXML4JException e) {
            LOGGER.error("process() OpenXML4J error", e);
        }  catch (SAXException e) {
            LOGGER.error("process() SAXException error", e);
        }  
        if(sheets == null){
            return null;
        }
        while (sheets.hasNext()) {  
            curRow = 0;  
            sheetIndex++;  
            InputStream sheet =  null;
            sheet = sheets.next();  
            InputSource sheetSource = new InputSource(sheet);  
            try {
                parser.parse(sheetSource);
            } catch (IOException e) {
                LOGGER.error("process() IO error", e);
            } catch (SAXException e) {
                LOGGER.error("process() SAX error", e);
            } finally {
                if(sheet != null){
                    try {
                        sheet.close();
                    } catch (IOException e) {
                        LOGGER.error("process() InputStream close error", e);
                    }  
                }
            }
            
        }
        Map<String, Object> mapR = new HashMap<String, Object>();
        mapR.put("result", result);
        mapR.put("title", title);
        mapR.put("total", totalRow - sheetIndex - 1);
        mapR.put("errorList", errorList);
        return mapR;
    }  
  
    public XMLReader fetchSheetParser(SharedStringsTable sst) throws SAXException {  
        XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");  
        this.sst = sst;  
        parser.setContentHandler(this);  
        return parser;  
    }  
  
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {  
  
        // c => 單元格  
        if ("c".equals(name)) {  
            // 若是下一個元素是 SST 的索引,則將nextIsString標記爲true  
            String cellType = attributes.getValue("t");  
            if ("s".equals(cellType)) {  
                nextIsString = true;  
                cellNull = false;  
            }   
            else {  
                nextIsString = false;  
                cellNull = true;  
            }  
        }  
  
        // 置空  
        lastContents = "";  
    }  
  
    public void endElement(String uri, String localName, String name) throws SAXException {  
  
        // 根據SST的索引值的到單元格的真正要存儲的字符串  
        // 這時characters()方法可能會被調用屢次  
        if (nextIsString) {  
            try {  
                int idx = Integer.parseInt(lastContents);  
                lastContents = new XSSFRichTextString(sst.getEntryAt(idx)).toString();  
            } catch (Exception e) {  
                //LOGGER.error("endElement() error", e);
            }  
        }  
       
        if ("v".equals(name) || "t".equals(name)) {  
            String value = lastContents.trim();  
            value = "".equals(value) ? " " : value;  
            rowlist.add(curCol, value);  
            curCol++;  
            cellNull = false;  
        }else if("c".equals(name) && cellNull == true){  
            rowlist.add(curCol, "");  
            curCol++;  
            cellNull = false;  
        }  
        else {  
            // 若是標籤名稱爲 row ,這說明已到行尾,調用 optRows() 方法  
            if ("row".equals(name)) {  
                optRows(sheetIndex, curRow, rowlist);  
                rowlist.clear();  
                totalRow++;
                curRow++;  
                curCol = 0;  
            }  
        }  
  
    }  
    public void optRows(int sheetIndex, int curRow, List<String> rowList){  
        Map<String, Object> map = new HashMap<>();
            if(!terminateParseFlag && controlParseNum != -1){
                if(sheetIndex == 0 && curRow ==0){
                    for (String string : rowlist) {
                        title.add(string);
                    }
                } else if(curRow != 0){
                    if(rowlist.size() == title.size()){
                        for (int i =0; i < rowlist.size(); i++) {
                            map.put(title.get(i), rowlist.get(i));
                        }
                        result.add(map);
                    } else{
                        LOGGER.debug("第"+(sheetIndex+1)+"頁,第"+(curRow+1)+"行數據與第一行數據長度不匹配!");
                    }
                }
                /*if(totalRow - sheetIndex == controlParseNum){
                    terminateParseFlag = true;
                    try {
                        throw new SAXException();
                    } catch (SAXException e) {
                        LOGGER.info("讀取前"+controlParseNum+"數據", e);
                    }
                }*/
            }
    }
    public void characters(char[] ch, int start, int length) throws SAXException {  
        // 獲得單元格內容的值  
  
        lastContents += new String(ch, start, length);  
    }  
    
    /*public static void main(String[] args) {
        try {
            // ExcelReaderUtil.readExcel(rowReader,
            // "E://2016-07-04-011940a.xls");
            System.out.println("**********************************************");
            //ExcelReaderUtil.readExcel(rowReader, "E://test.xlsx");
            XlsxReaderUtil reader = new XlsxReaderUtil("20170731084636595455.xlsx",20);
            Map<String, Object> process = reader.process();
            System.out.println(process);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}  
View Code
相關文章
相關標籤/搜索