使用poi读写excel文件

使用poi读写excel文件

今天一个同学需要处理一个excel文件,于是我便在网上搜了一下方法,顺便自己研究一下。刚刚参考网上资料,使用poi库测试了一下读取excel文件,效果不错,跟大家分享一下。

要读取的excel文件内容如下:

第一列是数值型,第二列是字符型,代码如下:

01package poi;
02 
03import java.io.FileInputStream;
04import java.io.InputStream;
05import java.util.Iterator;
06 
07import org.apache.poi.hssf.extractor.ExcelExtractor;
08import org.apache.poi.hssf.usermodel.HSSFCell;
09import org.apache.poi.hssf.usermodel.HSSFRow;
10import org.apache.poi.hssf.usermodel.HSSFSheet;
11import org.apache.poi.hssf.usermodel.HSSFWorkbook;
12import org.apache.poi.poifs.filesystem.POIFSFileSystem;
13import org.apache.poi.ss.usermodel.Cell;
14import org.apache.poi.ss.usermodel.Row;
15 
16/**
17 * 测试poi读取excel文件内容
18 * @author lihui
19 *
20 */
21public class TestRead {
22 
23        /**
24         * @param args
25         */
26        public static void main(String[] args){
27 
28                HSSFWorkbook wb = null;
29                POIFSFileSystem fs = null;
30                try {
31                        //设置要读取的文件路径
32                        fs = new POIFSFileSystem(new FileInputStream("d:\\book1.xls"));
33                        //HSSFWorkbook相当于一个excel文件,HSSFWorkbook是解析excel2007之前的版本(xls)
34                        //之后版本使用XSSFWorkbook(xlsx)
35                        wb = new HSSFWorkbook(fs);
36                        //获得sheet工作簿
37                        HSSFSheet sheet = wb.getSheetAt(0);
38                        //获得行
39                        HSSFRow row = sheet.getRow(3);
40                        //获得行中的列,即单元格
41                        HSSFCell cell = row.getCell(0);
42                        //获得单元格中的值,这里该单元格的值为数字,所以使用getNumericCellValue,如为字符串则会报错
43                        //如何取别的值,见print2方法
44                        double msg = cell.getNumericCellValue();
45                        System.out.println(msg);
46                        print1();
47                        print2();
48                } catch (Exception e) {
49                        e.printStackTrace();
50                }
51        }
52 
53        public static void print1() throws Exception {
54                InputStream is = new FileInputStream("d:\\book1.xls");
55                HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(is));
56                //A text extractor for Excel files.
57                //Returns the textual content of the file, suitable for indexing by something like Lucene,
58                //but not really intended for display to the user.
59                //用来获得整个excel文件的内容,表示为字符串
60                ExcelExtractor extractor = new ExcelExtractor(wb);
61                //字符串所包含的类型,详见api
62                extractor.setIncludeSheetNames(true);
63                extractor.setFormulasNotResults(false);
64                extractor.setIncludeCellComments(true);
65                //获得字符串形式
66                String text = extractor.getText();
67                System.out.println(text);
68        }
69 
70        public static void print2() throws Exception {
71                HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(
72                                "d:\\book1.xls"));
73                HSSFSheet sheet = wb.getSheetAt(0);
74                //迭代行
75                for (Iterator<Row> iter = (Iterator<Row>) sheet.rowIterator(); iter
76                                .hasNext();) {
77                        Row row = iter.next();
78                        //迭代列
79                        for (Iterator<Cell> iter2 = (Iterator<Cell>) row.cellIterator(); iter2
80                                        .hasNext();) {
81                                Cell cell = iter2.next();
82                                //用于测试的文件就2列,第一列为数字,第二列为字符串
83                                //对于数字cell.getCellType的值为HSSFCell.CELL_TYPE_NUMERIC,为0
84                                //对于字符串cell.getCellType的值为HSSFCell.CELL_TYPE_STRING,为1
85                                //完整的类型列表请查看api
86                                String content = cell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC?cell.getNumericCellValue()+"":cell.getStringCellValue();
87                                System.out.println(content);
88                        }
89                }
90        }
91 
92}

下面是创建一个excel文件

代码是参考网上自己做了些修改,加了些注释,已经写得很详细了,我就不多说了。poi库的下载地址可以去官方http://apache.freelamp.com/jakarta/poi/ 下载,我使用的是最新稳定版3.7,这里我也提供单独的jar包给大家下载http://cangzhitao.com/wp-content/uploads/2011/03/poi-3.7-20101029.jar

附cell类型说明:

This entry was posted in Java and tagged , , , , , , , . Bookmark the permalink.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值