apache POI学习(四)——apache官方读取方式试验
来源:互联网 发布:香港观塘apm有mac吗 编辑:程序博客网 时间:2024/05/01 04:09
apache POI学习(四)——apache官方读取方式试验
找到官方文档中的读取XML的方法,首先试验一下,这个是试验代码(官方提供仅仅做了一些修改)和结果。
import org.apache.poi.hssf.eventusermodel.HSSFListener;import org.apache.poi.hssf.record.BOFRecord;import org.apache.poi.hssf.record.BoundSheetRecord;import org.apache.poi.hssf.record.LabelSSTRecord;import org.apache.poi.hssf.record.NumberRecord;import org.apache.poi.hssf.record.Record;import org.apache.poi.hssf.record.RowRecord;import org.apache.poi.hssf.record.SSTRecord;/** * This example shows how to use the event API for reading a file. */public class EventExample implements HSSFListener {private SSTRecord sstrec;/** * This method listens for incoming records and handles them as required. * * @param record * The record that was found while reading. */public void processRecord(Record record) {switch (record.getSid()) {// the BOFRecord can represent either the beginning of a sheet or the// workbookcase BOFRecord.sid:BOFRecord bof = (BOFRecord) record;if (bof.getType() == bof.TYPE_WORKBOOK) {System.out.println("Encountered workbook");// assigned to the class level member} else if (bof.getType() == bof.TYPE_WORKSHEET) {System.out.println("Encountered sheet reference");}break;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("Row found, first column at " + rowrec.getFirstCol() + " last column at " + rowrec.getLastCol());break;case NumberRecord.sid:NumberRecord numrec = (NumberRecord) record;System.out.println("Cell found with value " + numrec.getValue() + " at row " + numrec.getRow()+ " and column " + numrec.getColumn());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;case LabelSSTRecord.sid:LabelSSTRecord lrec = (LabelSSTRecord) record;System.out.println("String cell found with value " + sstrec.getString(lrec.getSSTIndex()));break;}}}
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;import org.apache.poi.hssf.eventusermodel.HSSFRequest;import org.apache.poi.poifs.filesystem.POIFSFileSystem;public class Main {public static void main(String[] args){// TODO Auto-generated method stubSystem.out.println(123);// create a new file input stream with the input file specified // at the command line FileInputStream fin = null;try {fin = new FileInputStream("d:/123/workbook.xls");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} // create a new org.apache.poi.poifs.filesystem.Filesystem POIFSFileSystem poifs = null;try {poifs = new POIFSFileSystem(fin);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} // get the Workbook (excel part) stream in a InputStream InputStream din = null;try {din = poifs.createDocumentInputStream("Workbook");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} // construct out HSSFRequest object HSSFRequest req = new HSSFRequest(); // lazy listen for ALL records with the listener shown above req.addListenerForAllRecords(new EventExample()); // create our event factory HSSFEventFactory factory = new HSSFEventFactory(); // process our events based on the document input stream factory.processEvents(req, din); // once all the events are processed close our file input stream try {fin.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} // and our document input stream (don't want to leak these!) try {din.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} System.out.println("done.");}}
运行结果:
这是读取的excel文件的截图:
1 0
- apache POI学习(四)——apache官方读取方式试验
- apache POI学习——(一)试验apache官方的例子程序
- apache POI学习(五)——读取Excel文件
- apache POI学习(七)——表头样式设置
- POI (Apache POI)
- 使用 apache poi 读取 Excel
- Apache POI实现excel读取
- apache POI 学习笔记
- 一脸懵逼学习Java操作Excel之POI(Apache POI)
- apache poi读取excel(03版本之前)
- java读取excel格式的内容(APACHE POI)
- apache POI学习(二)——简单的导出excel
- apache POI学习(三)——生成带格式的excel表格
- apache POI学习(六)——大数据存储到Excel
- Apache POI组件操作Excel,制作报表(四)
- Apache POI组件操作Excel,制作报表(四)
- Java学习之Apache poi
- java通过apache poi 读取创建excel2007
- POJ 1661 - Help Jimmy
- 【每日视野】2016-08-31
- 微信公众平台jsapi开发教程(2)获取timestamp,nonceStr,signature参数
- block的存储位置和引用计数管理
- swift内购
- apache POI学习(四)——apache官方读取方式试验
- 小制作--单片机数字收音机
- 高德地图 UnsatisfiedLinkError
- debug的一些经验
- 希望能帮助一些新手 哪里不好的 大家可以讨论谢谢
- C++ 14的关键字
- Android源码阅读网站
- eclipse快捷键的设置和使用
- Java项目转换成web项目