使用POI读取excel文件
来源:互联网 发布:大数据规划 编辑:程序博客网 时间:2024/04/30 15:22
以下是我自己用的一个完整的例子:在使用之前在项目中导入poi的jar包就可以了。详细说明见备注。
package com.smartsky.virtualModel.POI;
import java.io.FileInputStream;import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
public class CsvReader {
public static void main(String[] args){
HSSFWorkbook wb = null;
POIFSFileSystem fs = null;
try {
//设置要读取的文件路径
fs = new POIFSFileSystem(new FileInputStream("d:\\54000-001.xls"));
//HSSFWorkbook相当于一个excel文件,HSSFWorkbook是解析excel2007之前的版本(xls)
//之后版本使用XSSFWorkbook(xlsx)
wb = new HSSFWorkbook(fs);
//获得sheet工作簿
HSSFSheet sheet = wb.getSheetAt(0);
//获得行
HSSFRow row = sheet.getRow(1);
//获得行中的列,即单元格
HSSFCell cell = row.getCell(0);
//获得单元格中的值,这里该单元格的值为数字,所以使用getNumericCellValue,如为字符串则会报错
//如何取别的值,见print2方法
double msg = cell.getNumericCellValue();
System.out.println("main");
System.out.println(msg);
print1();
print2();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void print1() throws Exception {
System.out.println("print1");
InputStream is = new FileInputStream("d:\\54000-001.xls");
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(is));
//A text extractor for Excel files.
//Returns the textual content of the file, suitable for indexing by something like Lucene,
//but not really intended for display to the user.
//用来获得整个excel文件的内容,表示为字符串
ExcelExtractor extractor = new ExcelExtractor(wb);
//字符串所包含的类型,详见api
extractor.setIncludeSheetNames(true);
extractor.setFormulasNotResults(false);
extractor.setIncludeCellComments(true);
//获得字符串形式
String text = extractor.getText();
System.out.println(text);
}
public static void print2() throws Exception {
System.out.println("print2");
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(
"d:\\54000-001.xls"));
HSSFSheet sheet = wb.getSheetAt(0);
//迭代行
for (Iterator<Row> iter = (Iterator<Row>) sheet.rowIterator(); iter
.hasNext();) {
Row row = iter.next();
//迭代列
for (Iterator<Cell> iter2 = (Iterator<Cell>) row.cellIterator(); iter2
.hasNext();) {
Cell cell = iter2.next();
//用于测试的文件就2列,第一列为数字,第二列为字符串
//对于数字cell.getCellType的值为HSSFCell.CELL_TYPE_NUMERIC,为0
//对于字符串cell.getCellType的值为HSSFCell.CELL_TYPE_STRING,为1
//完整的类型列表请查看api
String content = cell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC?cell.getNumericCellValue()+"":cell.getStringCellValue();
System.out.println(content);
}
}
}
}
0 0
- 使用POI读取EXCEL文件
- 使用POI读取EXCEL文件
- 使用POI读取excel文件
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用POI读取excel文件内容
- 使用java poi 读取excel文件
- 线段树(小写一发)
- 如何将confluence表格的第一行或者第一列固定
- 介绍安卓API<2>之设备兼容性
- 【NOIP模拟考一】 贪心 second 排座椅(seat.cpp)
- 10条PHP编程习惯助你找工作
- 使用POI读取excel文件
- IOS基础-NSArray、NSDictionary转JSON字符串
- [转]Memcached 与 Redis 实现的对比
- caffe 中的损失函数分析以及loss function
- excl 解决导出模板中文名问题
- 框架的优缺点
- [Wondgirl]从零开始学React Native之环境搭建(一)
- 【codeforces 733D】【贪心 乱搞】D. Kostya the Sculptor【给你n个长方形,让你找出2个或1个长方体,使得他们拼接成的长方体的内接圆半径最大】
- SessionFactory的创建和Session的获得