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例子的更多相关文章

  1. java POI读取excel 2007/2003

    2003版office excel读取 import java.io.FileNotFoundException; import java.io.IOException; import java.io ...

  2. 【转】java 读取 excel 2003 或 excel 2007

    package com.my.login; import java.io.File; import java.io.FileInputStream; import java.io.IOExceptio ...

  3. java的poi技术读取Excel[2003-2007,2010]

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

  4. java POI 解析excel 2003和2007 直接转为List<Map> 返回

    1.POI 官网下载jar包,3.5以上 2.项目导入jar包 3.参数:String数组--对应的excel列名对应的KEY,File  excel文件,sheetNumber ---excel的s ...

  5. java poi 读取excel内容

    import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; import or ...

  6. Java POI读取Excel数据,将数据写入到Excel表格

    1.准备 首先需要导入poi相应的jar包,包括: 下载地址:http://pan.baidu.com/s/1bpoxdz5 所需要的包的所在位置包括: 2.读取Excel数据代码 package S ...

  7. Java中读取Excel功能实现_POI

    这里使用apache的poi进行读取excel 1,新建javaproject 项目:TestExcel 2,导入包 包下载地址:http://poi.apache.org/download.html ...

  8. java实现读取excel文件内容

    package excel; import java.io.FileInputStream; import java.io.InputStream; import java.text.SimpleDa ...

  9. postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库

    最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...

随机推荐

  1. linux tar order

    http://blog.csdn.net/imyang2007/article/details/7634470 把常用的tar解压命令总结下,当作备忘: tar -c: 建立压缩档案-x:解压-t:查 ...

  2. redis 内存

    ziplist:http://blog.csdn.net/benbendy1984/article/details/7796956 redis 内部存储结构:http://www.searchtb.c ...

  3. 深入了解javascript事件流

    摘要:事件流这个东西是比较重要的,为了让自己更加理解js中的事件流,必须整理整理,梳理一下事件流的各种东西啊.本文大部分内容参考<javascript高级程序设计第三版> 先来一段书里的原 ...

  4. Inno setup 安装*.inf文件_示例

    nno setup 调用*.Inf文件的条目区段名称_示例 首先自己编写一个INF文件来供 Inno setup 进行测试: ;复制以下代码到记事本然后另存为123.inf .然后把123.inf文件 ...

  5. What is /proc/slabinfo?

    /proc/slabinfo gives information about memory usage on the slab level. Linux kernels uses slab pools ...

  6. Spark随机深林扩展—OOB错误评估和变量权重

    本文目的 当前spark(1.3版)随机森林实现,没有包括OOB错误评估和变量权重计算.而这两个功能在实际工作中比较常用.OOB错误评估可以代替交叉检验,评估模型整体结果,避免交叉检验带来的计算开销. ...

  7. HQ-SSAO (High-Quality SSAO)

    踩了前前后后无数坑,实现方式都试过了10几种,终于得到这个方案.虽说比不上2015最新的far-field AO,但至少在near/middle-field上,算是state of arts的实现了. ...

  8. jackson readTree

    String jsonstr = "{\"msg\":{\"head\":{\"version\":\"1.0\&quo ...

  9. [转]Sublime Text3注册码(可用)

    补充:2016.05 最近经过测试,3个注册码在新版3103的sublime上已经不可用了. 现补充两枚新版的license key: —– BEGIN LICENSE —– Michael Barn ...

  10. sctp和tcp的区别

    作为一个传输层协议,SCTP兼有TCP及UDP两者的特点.SCTP可以称为是TCP的改进协议,但他们之间仍然存在着较大的差别.首先SCTP和TCP之间的最大区别是SCTP的连接可以是多宿主连接的,TC ...