POI 事件模式解析xlsx

网上找了很多代码,大多忽略了空单元格,下面代码也是网上找的,自己修改了部分,使之可以输出空单元格

主要是用map保存数据,如果某列没有值则置为null,原来的是用List存储,遇到空单元格就会跳过

关于事件模式可参考https://blog.csdn.net/lipinganq/article/category/7134882,该文分析的很全

package ttt;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.xssf.model.SharedStringsTable;
import org.xml.sax.helpers.DefaultHandler;

import java.io.InputStream;   
  
import org.apache.poi.openxml4j.opc.OPCPackage;   
import org.apache.poi.xssf.eventusermodel.XSSFReader;   
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.XMLReaderFactory;   

  
public class ExcelUtil extends DefaultHandler {
       
    private SharedStringsTable sst;
    private String lastContents;
    private boolean nextIsString;

    private int sheetIndex = -1;
    private List<String> rowlist = new ArrayList<String>();
    private int curRow = 0;
    private int curCol = 0;
    private String col = "";
    private Map map = new HashedMap();
    
    
    public static void main(String[] args) throws Exception {
    	new ExcelUtil().readOneSheet("D:\\work\\workWord\\资料\\案例\\template4.xlsx");
	}
    
       
       
    /**  
     * 读取第一个工作簿的入口方法  
     * @param path  
     */  
    public void readOneSheet(String path) throws Exception {
        OPCPackage pkg = OPCPackage.open(path);
        XSSFReader r = new XSSFReader(pkg);
        SharedStringsTable sst = r.getSharedStringsTable(); 

        XMLReader parser = fetchSheetParser(sst);

        InputStream sheet = r.getSheet("rId1");

        InputSource sheetSource = new InputSource(sheet);
        parser.parse(sheetSource);

        sheet.close();
    }
       
       
    /**  
     * 读取所有工作簿的入口方法  
     * @param path  
     * @throws Exception 
       
    public void process(String path) throws Exception {
        OPCPackage pkg = OPCPackage.open(path);   
        XSSFReader r = new XSSFReader(pkg);   
        SharedStringsTable sst = r.getSharedStringsTable();   
  
        XMLReader parser = fetchSheetParser(sst);   
  
        Iterator<InputStream> sheets = r.getSheetsData();   
        while (sheets.hasNext()) {   
            curRow = 0;   
            sheetIndex++;   
            InputStream sheet = sheets.next();   
            InputSource sheetSource = new InputSource(sheet);   
            parser.parse(sheetSource);   
            sheet.close();   
        }   
    }   */
       
    /**  
     * 该方法自动被调用,每读一行调用一次,在方法中写自己的业务逻辑即可 
     * @param sheetIndex 工作簿序号 
     * @param curRow 处理到第几行 
     * @param rowList 当前数据行的数据集合 
     */  
    public void optRow(int sheetIndex, int curRow, List<String> rowList) {
        String temp = "";   
        for(String str : rowList) {
            temp += str + "_";
        }
//        System.out.println("最终值:"+temp);   
    }
       
       
    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 (name.equals("c")) {
        	col = attributes.getValue("r");
            // 如果下一个元素是 SST 的索引,则将nextIsString标记为true  
            String cellType = attributes.getValue("t");   
            if (cellType != null && cellType.equals("s")) {   
                nextIsString = true;   
            } else {
                nextIsString = false;   
            }
        }
        // 置空   
        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();
                nextIsString = false;
//                System.out.println("lastContents值为:"+lastContents);
            } catch (Exception e) {
            	e.printStackTrace();
            }
        }
  
        // v => 单元格的值,如果单元格是字符串则v标签的值为该字符串在SST中的索引 
        // 将单元格内容加入rowlist中,在这之前先去掉字符串前后的空白符  
        if (name.equals("v")) {
            String value = lastContents.trim();
            rowlist.add(curCol, value);
            curCol++;
            map.put(col, value);
        } else {
            // 如果标签名称为 row ,这说明已到行尾,调用 optRows() 方法  
            if (name.equals("row")) {
                optRow(sheetIndex, curRow, rowlist);
                outMap(map,curRow);
                rowlist.clear();
                curRow++;
                curCol = 0;
            }
        }
    }
  
    public void characters(char[] ch, int start, int length)   
            throws SAXException {
        // 得到单元格内容的值  
        lastContents += new String(ch, start, length);   
    }
    
   /**
    * 测试输出
 * @param map1
 * @param row
 */
private void outMap(Map map1,int row){
	   row = row + 1;
	   System.out.print(map1.get("A"+row)+" ");
	   System.out.print(map1.get("B"+row)+" ");
	   System.out.print(map1.get("C"+row)+" ");
	   System.out.print(map1.get("D"+row)+" ");
	   System.out.print(map1.get("E"+row)+" ");
	   System.out.print(map1.get("F"+row)+" ");
	   System.out.print(map1.get("G"+row)+" ");
	   System.out.print(map1.get("H"+row)+" ");
	   System.out.println();
	   
   }
    
    
}  


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值