浏览 3071 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-06-14
通过eventusermodel读取文件 package com.easyway.excel.events; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import org.apache.commons.collections.CollectionUtils; import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder.SheetRecordCollectingListener; import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener; import org.apache.poi.hssf.eventusermodel.HSSFEventFactory; import org.apache.poi.hssf.eventusermodel.HSSFListener; import org.apache.poi.hssf.eventusermodel.HSSFRequest; import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener; import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.BlankRecord; import org.apache.poi.hssf.record.BoolErrRecord; import org.apache.poi.hssf.record.BoundSheetRecord; import org.apache.poi.hssf.record.FormulaRecord; import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.NumberRecord; import org.apache.poi.hssf.record.RowRecord; import org.apache.poi.hssf.record.SSTRecord; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.poifs.filesystem.POIFSFileSystem; /** * 基于POI HSSF的eventmodel 模型的时间解析方式 * 优点:解析数据相当快。 * 缺点:1.仅仅支持97~2003版本的excel,不支持2007版本的excel。 * 2.只能读Excel中一个Sheet页面。 * @Title: * @Description: 实现TODO * @Copyright:Copyright (c) 2011 * @Company:易程科技股份有限公司 * @Date:2012-6-14 * @author longgangbai * @version 1.0 */ public class UserModelEventListener implements HSSFListener { private SSTRecord sstrec; /** Should we output the formula, or the value it has? */ private boolean outputFormulaValues = true; /** For parsing Formulas */ private SheetRecordCollectingListener workbookBuildingListener; private int sheetNo=0; private int curRowNum=0; private int ignoreRowNum=1; @Override public void processRecord(org.apache.poi.hssf.record.Record record) { switch (record.getSid()) { case BOFRecord.sid: BOFRecord bof = (BOFRecord) record; //顺序进入新的Workbook if (bof.getType() == bof.TYPE_WORKBOOK) { //System.out.println("开始解析excel 文档....."); //顺序进入新的Worksheet,因为Event API不会把Excel文件里的所有数据结构都关联起来, //所以这儿一定要记录现在进入第几个sheet了。 } else if (bof.getType() == bof.TYPE_WORKSHEET) { //读取新的一个Sheet页 sheetNo++; System.out.println("当前Sheet 页编号:" +sheetNo); } break; //开始解析Sheet的信息,记录sheet,这儿会把所有的sheet都顺序打印出来,如果有多个sheet的话,可以顺序记入到一个List里 case BoundSheetRecord.sid: BoundSheetRecord bsr = (BoundSheetRecord) record; //System.out.println("New sheet named: " + bsr.getSheetname()); break; //执行行记录事件 case RowRecord.sid: RowRecord rowrec = (RowRecord) record; //System.out.println("记录开始, first column at " // + rowrec.getFirstCol() + " last column at " // + rowrec.getLastCol()); break; // SSTRecords store a array of unique strings used in Excel. case SSTRecord.sid: sstrec = (SSTRecord) record; // for (int k = 0; k < sstrec.getNumUniqueStrings(); k++) { // System.out.println("String table value " + k + " = " // + sstrec.getString(k)); // } break; //发现数字类型的cell,因为数字和日期都是用这个格式,所以下面一定要判断是不是日期格式,另外默认的数字也会被视为日期格式,所以如果是数字的话,一定要明确指定格式!!!!!!! case NumberRecord.sid: NumberRecord nr = (NumberRecord) record; if(HSSFDateUtil.isInternalDateFormat(nr.getXFIndex())){ //System.out.println("日付:"+ // +", 行:"+nr.getRow()+ ", 列:"+nr.getColumn()+"。("+nr.getXFIndex()+")"); String timeStr=(new SimpleDateFormat("yyyy-MM-dd")).format(HSSFDateUtil.getJavaDate(nr.getValue())); addDataAndrChangeRow(nr.getRow(),nr.getColumn(),timeStr); }else{ String timeStr=getTime(nr.getValue());; // System.out.println("数字:"+nr.getValue()+", 行:"+nr.getRow()+ ", 列:"+nr.getColumn()+"。("+nr.getXFIndex()+")"); addDataAndrChangeRow(nr.getRow(),nr.getColumn(),getTime(nr.getValue())); } break; //发现字符串类型,这儿要取字符串的值的话,跟据其index去字符串表里读取 case LabelSSTRecord.sid: LabelSSTRecord lsr = (LabelSSTRecord)record; addDataAndrChangeRow(lsr.getRow(),lsr.getColumn(), sstrec.getString(lsr.getSSTIndex())); //System.out.println("文字列:"+sstrec.getString(lsr.getSSTIndex())+", 行:"+lsr.getRow()+", 列:"+lsr.getColumn()); break; case BoolErrRecord.sid: //解析boolean错误信息 BoolErrRecord ber = (BoolErrRecord)record; if(ber.isBoolean()){ addDataAndrChangeRow(ber.getRow(),ber.getColumn(), ber.getBooleanValue()); //System.out.println("Boolean:"+ber.getBooleanValue()+", 行:"+ber.getRow()+", 列:"+ber.getColumn()); } if(ber.isError()){ //addDataAndrChangeRow(ber.getRow(),ber.getColumn(), ber.getErrorValue()); //System.out.println("Error:"+ber.getErrorValue()+", 行:"+ber.getRow()+", 列:"+ber.getColumn()); } break; //空白记录的信息 case BlankRecord.sid: BlankRecord br = (BlankRecord)record; addDataAndrChangeRow(br.getRow(),br.getColumn(), ""); //System.out.println("空。 行:"+br.getRow()+", 列:"+br.getColumn()); break; case FormulaRecord.sid: //数式 FormulaRecord fr = (FormulaRecord)record; addDataAndrChangeRow(fr.getRow(),fr.getColumn(), fr.getValue()); //System.out.println("数字 。 行:"+fr.getRow()+", 列:"+fr.getColumn()); break; } } /** * 添加数据记录并检查是否换行 * @param row 实际当前行号 * @param col 实际记录当前列 * @param value 当前cell的值 */ public void addDataAndrChangeRow(int row,int col,Object value){ //当前行如果大于实际行表示改行忽略,不记录 if(curRowNum!=row){ System.out.print(row+":"+col+" "+value+"\r"); curRowNum=row; }else{ System.out.print(row+":"+col+" "+value+"\r"); } } /** * [正确地处理整数后自动加零的情况]</li> * @param sNum * @return */ private static String getTime(double daynum) { double totalSeconds=daynum*86400.0D; //总的分钟数 int seconds =(int)totalSeconds/60; //实际小时数 int hours =seconds/60; int minutes = seconds-hours*60; //剩余的实际分钟数 StringBuffer sb=new StringBuffer(); if(String.valueOf(hours).length()==1){ sb.append("0"+hours); }else{ sb.append(hours); } sb.append(":"); if(String.valueOf(minutes).length()==1){ sb.append("0"+minutes); }else{ sb.append(minutes); } return sb.toString(); } /** * Read an excel file and spit out what we find. * * @param args * Expect one argument that is the file to read. * @throws IOException * When there is an error processing the file. */ public static void main(String[] args) throws IOException { UserModelEventListener xlsEventListener=new UserModelEventListener(); MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener( xlsEventListener); FormatTrackingHSSFListener formatListener = new FormatTrackingHSSFListener(listener); //创建一个excel输入流 FileInputStream fin = new FileInputStream("C:\\station2station.xls"); //创建一个 org.apache.poi.poifs.filesystem.Filesystem POIFSFileSystem poifs = new POIFSFileSystem(fin); //将excel 2003格式POI文档输入流 InputStream din = poifs.createDocumentInputStream("Workbook"); //这儿为所有类型的Record都注册了监听器,如果需求明确的话,可以用addListener方法,并指定所需的Record类型 HSSFRequest req = new HSSFRequest(); //添加监听记录的事件 req.addListenerForAllRecords(xlsEventListener); boolean outputFormulaValues=true; if (outputFormulaValues) { req.addListenerForAllRecords(formatListener); } else { SheetRecordCollectingListener workbookBuildingListener = new SheetRecordCollectingListener( formatListener); req.addListenerForAllRecords(workbookBuildingListener); } //创建时间工厂 HSSFEventFactory factory = new HSSFEventFactory(); //处理基于时间文档流 factory.processEvents(req, din); //关闭文件流 fin.close(); //关闭基于POI文档流 din.close(); System.out.println("done."); } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |