poi excel
使用apache的poi包可以对excel进行操作读取和写入。
因excel分为xls的2003版和xlsx的2007版,poi在创建workbook时使用不同的类创建,因此需要注意区分xls。
Workbook workbook = null;
String fileExtension=FilenameUtils.getExtension(file.getOriginalFilename());
if(".xls".equals(fileExtension)){
workbook = new HSSFWorkbook(file.getInputStream()); //2003 xls
}else{
workbook = new XSSFWorkbook(file.getInputStream()); //2007 xlsx
}
※注意如果引入poi后找不到XSSFWorkbook,则可能没有引入poi-ooxml.jar
Sheet sheetWorkInfo = workbook.getSheet([sheetname]);
以下为读取excel内容装入到list<bean>中的实例:
/**
* ExcelUtils 读取信息
* @author DennyZhao
*
*/
public class ExcelUtils { /**
* 获取workbook
* @param file
* @return workbook
* @throws IOException
* @throws FileNotFoundException
*/
public static Workbook getWorkBook(String filepath) throws FileNotFoundException, IOException {
Workbook workbook = null;
File file = new File(filepath);
/**
* 文件是否存在,是否为可用文件
*/
if(!file.exists() || file.isDirectory()) {
System.out.println("file is not exists or is a directory...");
return null;
}
/**
* 文件是否可读
*/
if(!file.canRead()) {
System.out.println("the file can not be readed, please confirm if you have the authority to read it.");
return null;
}
/**
* 是否为excel文件
*/
if(!FilenameUtils.getExtension(filepath).contains("xls")) {
System.out.println("i'm so sorry..we just support the file of type which is excel..");
return null;
} String fileExtension=FilenameUtils.getExtension(file.getName());
if("xls".equals(fileExtension)){
workbook = new HSSFWorkbook(new FileInputStream(file)); //2003 xls
}else{
workbook = new XSSFWorkbook(new FileInputStream(file)); //2007 xlsx
}
return workbook;
} /**
* 读取数据返回对象一览
* @param filepath
* @param sheetName
* @return
* @throws IOException
* @throws FileNotFoundException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static <T> List<T> getListObj(String filepath, String sheetName, Class<T> class1) throws FileNotFoundException, IOException, IllegalAccessException, InvocationTargetException, InstantiationException{
Workbook work = getWorkBook(filepath);
Sheet sheet = work.getSheet(sheetName);
int rowCount = sheet.getPhysicalNumberOfRows();
int colCount = sheet.getRow(0).getPhysicalNumberOfCells();
List<T> listResult = new ArrayList<T>(); for(int i=1;i < rowCount; i++) {
Row row = sheet.getRow(i);
Map<String, Object> map = new HashMap<String, Object>();
// 判断是否已经读取完毕,第一格不能为空
if(row.getCell(0)== null) {
break;
} for(int j=0; j < colCount; j++) {
Cell cell = row.getCell(j);
String key = sheet.getRow(0).getCell(j).getStringCellValue(); Object cellValue = getCellValue(cell);
map.put(key, cellValue);
}
T t = class1.newInstance();
BeanUtils.copyProperties(t, map);
listResult.add(t);
}
work.close();
return listResult;
} /**
* 获取cellValue
* @param cell
*/
private static Object getCellValue(Cell cell) {
if(cell == null) {
return "";
}
CellType cellType = cell.getCellTypeEnum();
Object obj = null;
switch(cellType) {
case NUMERIC:
obj = cell.getNumericCellValue();
break;
default:
obj = cell.getStringCellValue();
}
return obj;
}
poi excel的更多相关文章
- poi 升级至4.x 的问题总结(POI Excel 单元格内容类型判断并取值)
POI Excel 单元格内容类型判断并取值 以前用 cell.getCachedFormulaResultType() 得到 type 升级到4后获取不到了 换为:cell.getCellType( ...
- POI Excel 合并数据相同的行
import java.io.Serializable; /** * POI Excel报表导出,列合并实体<br> * * @author WQ * */ public class Po ...
- poi excel导出,下载
poi.jar包 public void downExcel(HttpServletResponse response,Page<ShopApply> page) throws Excep ...
- poi excel导入
poi.jar包 import java.io.File;import java.io.FileInputStream;import java.io.IOException; import org.a ...
- java, poi, excel
工作需要用java操作Excel,现在网上搜索了一下,决定选取POI包来操作.pom内容如下: <dependency> <groupId>org.apache.poi< ...
- POI/Excel/HTML单元格公式问题
一.问题描述 使用MyBatis从数据库中获取数据,然后用POI把数据填充到Excel模板中,生成最终的xls文件.把最终的xls文件转换为html文件,并返回给前台显示在Panel中. Excel模 ...
- POI excel导出
******************************* excel表格导出,使用POI实现 ******************************* 实现导出步骤 --配置导出excel ...
- poi excel超出65536行数限制自动扩展Invalid row number (65536) outside allow
1.xls一个sheet只能装65536行,多余则报错 poi包导出或写入excel超出65536报错: java.lang.IllegalArgumentException: Invalid row ...
- poi excel 合并单元格
结论:final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip, colId, colId + c ...
- poi excel 设置边框字体行高行宽
final HSSFSheet sheet = wb.createSheet(sheetName + "_" + n); System.out.println("s ...
随机推荐
- JS之滚动条效果2
在前面一篇说的是滚动条效果,本篇继续在前面的基础上面针对滚动条进行操作.本次要实现的效果如下:拖动滚动条左右移动时,上面的图片内容也相对外层盒子做相对移动. 下面针对要实现的效果进行分析:首先是页面基 ...
- [转]截图软件分享 - Snipaste
http://chromecj.com/software/2018-10/1538.html https://zh.snipaste.com/download.html
- 1127 ZigZagging on a Tree (30 分)
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
- ros6.44版本增加了测试出两台ROS之间的速度极限是多少
原文: https://wiki.mikrotik.com/wiki/Manual:Tools/Speed_Test [admin@MikroTik]] > /tool speed-test a ...
- Jmeter(三十八)Jmeter Question 之 ‘批量执行SQL语句’
知识使我们变得玩世不恭,智慧使我们变得冷酷无情,我们思考的太多,感知太少,除了机器,我们更需要人性,除了智慧,我们需要仁慈和善良. ------出自查理卓别林的演讲 前面有提到Jmeter使用JDBC ...
- [UE4]实例化材质
在虚幻引擎 4 中,材质实例化用来更改材质的外观,而不会引起成本高昂的材质重新编译. 实例化材质官方文档
- [UE4]修改相机裁剪距离
在UE4中,相机距离一个物体太近,物体就会被裁剪,这个距离是一个全局设定,无法单个相机设置. 项目设置:
- android 将项目下的数据库拷贝到sd卡中
/** * 将项目下的数据库拷贝到sd卡中 */ public static boolean copyDbToSdCard() { FileInputStream fis = null; FileOu ...
- python文件相关
文件操作基本流程初探 f = open('chenli.txt') #打开文件 first_line = f.readline() print('first line:',first_line) #读 ...
- CS229 6.6 Neurons Networks PCA主成分分析
主成分分析(PCA)是一种经典的降维算法,基于基变换,数据原来位于标准坐标基下,将其投影到前k个最大特征值对应的特征向量所组成的基上,使得数据在新基各个维度有最大的方差,且在新基的各个维度上数据是不相 ...