Java Struts2读取Excel 2003/2007/2010例子
Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版。
Apache POI 代码例子地址:http://poi.apache.org/spreadsheet/quick-guide.html
本例子可以读取Microsoft Office Excel 2003/2007/2010,具体代码及注释如下:
读取“.xls”格式使用 import org.apache.poi.hssf.usermodel.*; 包的内容,例如:HSSFWorkbook
读取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:XSSFWorkbook
读取两种格式使用 import org.apache.poi.ss.usermodel.* 包的内容,例如:Workbook
引入包如下:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.DateUtil;
/**
* 读取Excel测试,兼容 Excel 2003/2007/2010
*/
public String readExcel()
{
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
//同时支持Excel 2003、2007
File excelFile = new File("/home/zht/test.xls"); //创建文件对象
FileInputStream is = new FileInputStream(excelFile); //文件流
Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的
int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量
//遍历每个Sheet
for (int s = 0; s < sheetCount; s++) {
Sheet sheet = workbook.getSheetAt(s);
int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
//遍历每一行
for (int r = 0; r < rowCount; r++) {
Row row = sheet.getRow(r);
int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
//遍历每一列
for (int c = 0; c < cellCount; c++) {
Cell cell = row.getCell(c);
int cellType = cell.getCellType();
String cellValue = null;
switch(cellType) {
case Cell.CELL_TYPE_STRING: //文本
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC: //数字、日期
if(DateUtil.isCellDateFormatted(cell)) {
cellValue = fmt.format(cell.getDateCellValue()); //日期型
}
else {
cellValue = String.valueOf(cell.getNumericCellValue()); //数字
}
break;
case Cell.CELL_TYPE_BOOLEAN: //布尔型
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_BLANK: //空白
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_ERROR: //错误
cellValue = "错误";
break;
case Cell.CELL_TYPE_FORMULA: //公式
cellValue = "错误";
break;
default:
cellValue = "错误";
}
System.out.print(cellValue + " ");
}
System.out.println();
}
} }
catch (Exception e) {
e.printStackTrace();
} return Action.SUCCESS;
}
转自 :http://blog.csdn.net/zht666/article/details/11598711
Java Struts2读取Excel 2003/2007/2010例子的更多相关文章
- java POI读取excel 2007/2003
2003版office excel读取 import java.io.FileNotFoundException; import java.io.IOException; import java.io ...
- 【转】java 读取 excel 2003 或 excel 2007
package com.my.login; import java.io.File; import java.io.FileInputStream; import java.io.IOExceptio ...
- java的poi技术读取Excel[2003-2007,2010]
这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...
- java POI 解析excel 2003和2007 直接转为List<Map> 返回
1.POI 官网下载jar包,3.5以上 2.项目导入jar包 3.参数:String数组--对应的excel列名对应的KEY,File excel文件,sheetNumber ---excel的s ...
- java poi 读取excel内容
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import or ...
- Java POI读取Excel数据,将数据写入到Excel表格
1.准备 首先需要导入poi相应的jar包,包括: 下载地址:http://pan.baidu.com/s/1bpoxdz5 所需要的包的所在位置包括: 2.读取Excel数据代码 package S ...
- Java中读取Excel功能实现_POI
这里使用apache的poi进行读取excel 1,新建javaproject 项目:TestExcel 2,导入包 包下载地址:http://poi.apache.org/download.html ...
- java实现读取excel文件内容
package excel; import java.io.FileInputStream; import java.io.InputStream; import java.text.SimpleDa ...
- postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库
最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...
随机推荐
- hibernate中 inverse的用法(转载)
http://blog.csdn.net/xiaoxian8023/article/details/15380529 一.Inverse是hibernate双向关系中的基本概念.inverse的真正作 ...
- python decorator simple example
Why we need the decorator in python ? Let's see a example: #!/usr/bin/python def fun_1(x): return x* ...
- Atitit.java eval功能的实现 Compiler API
Atitit.java eval功能的实现 Compiler API 输出echo2 输出目录配置2 针对编译器,JDK 设计了两个接口,分别是 JavaCompiler 和JavaCompiler ...
- Atitit.javascript 实现类的方式原理大总结
Atitit.javascript 实现类的方式原理大总结 1. 实现类的式::构造方法方式:原型方式:构造方法+原型的混合方式 1 2. 原型方式(function mode)经典式..实现属性推荐 ...
- atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException
atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException #--现象 java.lang.NullPoint ...
- paip.配置ef_unified_filter() failed ext_filter_module mod_ext_filter.so apache 错误解决
paip.配置ef_unified_filter() failed ext_filter_module mod_ext_filter.so apache 错误解决 作者Attilax 艾龙, ...
- paip.log4j 日志系统 参数以及最佳实践
paip.log4j 日志系统 参数以及最佳实践 %d{yyyy-MM-dd HH:mm:ss} [thrd:%t] %5p loger:%c (%C.%M.%L) - %m%n 201 ...
- Leetcode-203 Remove Linked List Elements
#203. Remove Linked List Elements Remove all elements from a linked list of integers that have val ...
- Android Studio生成javadoc出错的解决办法
一般使用Android Studio生成javadoc会有两个问题: 空指针异常 文档乱码 解决办法如下: 第1个问题:Tools --> Generate JavaDoc -->打开对话 ...
- ubuntu 16.04 vnc server
安装方法 https://help.ubuntu.com/community/VNC/Servers#vino 系统默认自带的,通过搜索Remote Desktop来找到它 配置好之后,设定一下密码 ...