JavaWeb使用POI操作Excel文件实例
作者:feipeng8848 时间:2022-06-12 18:19:30
1.为项目添加POI
POI官网链接
点进去之后下载(上边的是编译好的类,下边的是源代码)
解压文件夹,把下面三个文件复制到WebComtent>WEB-INF>lib文件夹下
再把这三个文件复制到Tomcat的lib文件夹下,否则Tomcat会因为找不到类而报错(这个地方郁闷了一上午)
读取“.xls”格式使用 import org.apache.poi.hssf.usermodel.*;包的内容,例如:HSSFWorkbook
读取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:XSSFWorkbook
读取两种格式使用 import org.apache.poi.ss.usermodel.* 包的内容,例如:Workbook
由于我是读取xslx文件所以使用以上几个jar文件。
注意:
上图中的两个文件夹中也有我们需要的jar文件,具体是哪几个忘记了(当然为了保险也可以把所有的都放进WebContent>WEN-INF>lib下再BuildPath进项目),没关系,一会运行的过程中会报错,根据错误信息再去找到相关的jar文件BuildPath进去就好,注意还要再Tomcat>lib下放置一份副本。
2.读取Excel文件
官方教程:链接
类库:链接
直接看代码吧,不难懂。
//遍历一个Excel文件<br>private void getExcelData(File file) {
System.out.println("now in getExcelData" );
System.out.println("get file name:"+file.getName().toString());
XSSFWorkbook workBook= null;
try {
workBook = new XSSFWorkbook(file);
int sheetCount = workBook.getNumberOfSheets(); //Sheet的数量
System.out.println("num of sheet is : "+sheetCount);
//遍历每个sheet
for(int i=0;i<sheetCount;i++)
{
XSSFSheet sheet = workBook.getSheetAt(i);
//获取总行数
int rowCount = sheet.getPhysicalNumberOfRows();
System.out.println("num of row : "+ rowCount);
System.out.println("i now in sheet : "+ i);
//遍历每一行
for (int r = 0; r < rowCount; r++) {
XSSFRow row = sheet.getRow(r);
//获取总列数
int cellCount = row.getPhysicalNumberOfCells();
//遍历每一列
for (int c = 0; c < cellCount; c++) {
XSSFCell cell = row.getCell(c);
String cellValue = null;
switch (cell.getCellTypeEnum()) {
case STRING:
//System.out.println("celltype is string");
cellValue = cell.getStringCellValue();
break;
case NUMERIC:
//System.out.println("celltype is Number");//整数,小数,日期
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case BOOLEAN:
//System.out.println("celltype is Boolean");
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case FORMULA:
//System.out.println("celltype is Formula");//公式
cellValue = "错误,不能为公式";
break;
case BLANK:
//System.out.println("celltype is Blank");//空白
cellValue = cell.getStringCellValue();
break;
case ERROR:
//System.out.println("celltype is Error");
cellValue = "错误";
break;
default:
//System.out.println("celltype : default");
cellValue = "错误";
break;
}
System.out.println(cellValue.toString());
}
}
}
} catch (IOException e) {
System.out.println("File Error IOException : "+e.getMessage());
}
catch (Exception e) {
// TODO: handle exception
}
finally {
try {
workBook.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("workBook.close()&fileInputStream.close() Error : "+e.getMessage());
}
System.out.println("Try Catch : finally");
}
System.out.println("hi feipeng8848 getExcelData is done");
}
以上所述是小编给大家介绍的JavaWeb使用POI操作Excel文件实例网站的支持!
来源:http://www.cnblogs.com/feipeng8848/archive/2017/04/28/6781822.html
标签:javaweb,poi,excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java异常的处理机制
2023-12-03 15:33:19
![](https://img.aspxhome.com/file/2023/7/71147_0s.png)
mybatis group by substr函数传参报错的解决
2022-09-10 03:44:34
Spring Boot 2结合Spring security + JWT实现微信小程序登录
2022-07-14 08:25:54
Spring Boot集成MyBatis实现通用Mapper的配置及使用
2023-07-03 18:17:10
sqlite数据库的介绍与java操作sqlite的实例讲解
2023-05-09 03:07:40
Android实现的数字格式化用法示例
2023-09-30 09:03:28
spring boot使用logback日志级别打印控制操作
2021-08-11 07:40:03
SpringBoot整合Zookeeper详细教程
2022-07-24 11:33:09
![](https://img.aspxhome.com/file/2023/2/63392_0s.png)
在Java内存模型中测试并发程序代码
2023-11-24 20:37:55
![](https://img.aspxhome.com/file/2023/7/59967_0s.png)
spring boot打包成可执行jar包
2022-09-26 12:48:14
spring cloud zuul修改请求url的方法
2022-09-03 01:44:19
Java Socket通信之聊天室功能
2022-02-04 11:47:35
![](https://img.aspxhome.com/file/2023/3/61283_0s.jpg)
Java SpringBoot整合shiro-spring-boot-starterqi项目报错解决
2022-11-07 03:27:02
![](https://img.aspxhome.com/file/2023/0/69650_0s.png)
java 创建线程的几种方式
2023-10-29 19:40:16
![](https://img.aspxhome.com/file/2023/5/58695_0s.png)
Spring之IOC详解
2022-10-13 19:10:24
![](https://img.aspxhome.com/file/2023/8/62118_0s.png)
Java中七种排序算法总结分析
2023-03-27 19:41:44
![](https://img.aspxhome.com/file/2023/6/67616_0s.png)
java的main方法中调用spring的service方式
2023-03-29 00:12:16
Java后端学习精华之TCP通信传输协议详解
2021-08-06 03:15:42
![](https://img.aspxhome.com/file/2023/1/64221_0s.png)
Java Arrays.asList使用方法解析
2023-11-26 06:27:09
MyBatis中的properties配置(推荐)
2023-06-04 20:47:58