- 浏览: 656276 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (173)
- ajax (4)
- ant (4)
- css (14)
- eclipse (1)
- exe4j (0)
- hibernate (7)
- html (29)
- http (2)
- install4j (0)
- java (35)
- javascript (31)
- jdbc (1)
- jquery (2)
- jsp (15)
- linux命令 (8)
- ooad (2)
- properties (3)
- servlet (1)
- spring (3)
- struts (6)
- swing (4)
- thread (2)
- version_control (1)
- webservice (4)
- xml (4)
- 数据库 (19)
- 正则表达式 (5)
- 其他 (16)
- 面试题 (1)
- 框架 (9)
- maven (1)
- util (29)
- 设计模式 (1)
最新评论
-
herodie4444:
看起来好像不难。
Java Struts2创建Excel文件并实现文件下载 -
心轨翼:
Java遍历Set集合方法 -
魔鬼的献祭:
...
java的web开发分页工具类 -
Nmxszxsl:
能发一个没有被墙的代码到我的邮箱吗?万分感谢。54365156 ...
Google翻译post提交无长度限制的在线翻译例子 -
782763595:
...
jar包混淆器介绍
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; 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; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.json.annotations.JSON; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.csair.smms.inituserinfo.dto.UserInsert; import com.csair.smms.inituserinfo.service.InitUserInfoService; import com.csair.smms.preventive.service.PreventiveInfoService; @ParentPackage("json-default") @Namespace("/upload") @Controller public class UploadUserAction { private static int version2003 = 2003; private static int version2007 = 2007; private static int version = version2003; private File xlsBook; private String xlsBookFileName; private String xlsBookContentType; private static Logger logger = Logger.getLogger(UploadUserAction.class); @Autowired private InitUserInfoService initUserInfoService; @Autowired private PreventiveInfoService preventiveInfoService; @JSON(serialize = false) public File getXlsBook() { return xlsBook; } public void setXlsBook(File xlsBook) { this.xlsBook = xlsBook; } @JSON(serialize = false) public String getXlsBookFileName() { return xlsBookFileName; } public void setXlsBookFileName(String xlsBookFileName) { this.xlsBookFileName = xlsBookFileName; } @JSON(serialize = false) public String getXlsBookContentType() { return xlsBookContentType; } public void setXlsBookContentType(String xlsBookContentType) { this.xlsBookContentType = xlsBookContentType; } //此方法判别Excel2003和Excel2007 public void initType() { String name = getXlsBookFileName(); if (name != null) { int index = getXlsBookFileName().indexOf("."); String suffex = name.substring(index); if ("xls".equals(suffex)) { version = version2003; } else if ("xlsx".equals(suffex)) { version = version2007; } } } @Action(value = "uploadUser", results = { @Result(type = "json") }) public String analizeExcel() { if (xlsBook != null) { initType(); InputStream is = null; List<UserInsert> userList = null; try { is = new FileInputStream(xlsBook); version = (xlsBookFileName.endsWith(".xls") ? version2003 : version2007); if (version == 2003) {// 2003 POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); userList = readUser(sheet); } else if (version == 2007) {// 2007 XSSFWorkbook xwb = new XSSFWorkbook(is); XSSFSheet sheet = xwb.getSheetAt(0); userList = readUser(sheet); } } catch (FileNotFoundException e) { } catch (IOException e) { } // 保存javabean逻辑 //自己的逻辑代码 } return com.opensymphony.xwork2.Action.SUCCESS; } //此方法为读取表格核心方法 public List<UserInsert> readUser(Sheet sheet) { List<UserInsert> userList = new ArrayList<UserInsert>(); int rowNum = sheet.getPhysicalNumberOfRows(); UserInsert ui = null; for (int i = 1; i < rowNum; i++) { Row row = sheet.getRow(i); Cell c = row.getCell(0); ui = new UserInsert(); if (c != null) { if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) { long id = (long) row.getCell(0).getNumericCellValue(); ui.setId(id + ""); } else { ui.setId(c.getStringCellValue()); } c = row.getCell(1); if (c != null) { ui.setName(row.getCell(1).getStringCellValue()); } else { ui.setName(""); } c = row.getCell(2); if (c != null) { ui.setSex(row.getCell(2).getStringCellValue()); } else { ui.setSex(""); } c = row.getCell(3); if (c != null) { ui.setBase(row.getCell(3).getStringCellValue()); } else { ui.setBase(""); } c = row.getCell(4); if (c != null) { ui.setBaseCode(row.getCell(4).getStringCellValue()); } else { ui.setBaseCode(""); } c = row.getCell(5); if (c != null) { ui.setDepartment(row.getCell(5).getStringCellValue()); } else { ui.setDepartment(""); } c = row.getCell(6); if (c != null) { ui.setPosition(row.getCell(6).getStringCellValue()); } else { ui.setPosition(""); } c = row.getCell(7); if (c != null) { ui.setRole(row.getCell(7).getStringCellValue()); } else { ui.setRole(""); } c = row.getCell(8); if (c != null) { if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) { long m = (long) row.getCell(8).getNumericCellValue(); ui.setMobile(m + ""); } else { ui.setMobile(c.getStringCellValue()); } } else { ui.setMobile(""); } c = row.getCell(9); if (c != null) { if (c.getCellType() == Cell.CELL_TYPE_NUMERIC) { long t = (long) row.getCell(9).getNumericCellValue(); ui.setTelephone(t + ""); } else { ui.setTelephone(c.getStringCellValue()); } } else { ui.setTelephone(""); } c = row.getCell(10); if (c != null) { ui.setEmail(row.getCell(10).getStringCellValue()); } else { ui.setEmail(""); } userList.add(ui); } } return userList; } public static void main(String[] args) { UploadUserAction uua = new UploadUserAction(); uua.analizeExcel(); System.out.println("end-----------------"); } }
JavaBean类:
public class UserInsert implements Serializable{ private static final long serialVersionUID = -796538816605301094L; private String id;// 员工号 private String name;// 姓名 private String sex;// 性别 private String base;// 所属基地 private String baseCode;// 基地三字码 private String department;// 部门 private String position;// 职务 private String role;// 系统角色 private String mobile;// 手机 private String telephone;// 办公电话 private String email;// 邮箱 //此处省略各属性的setter和getter方法 }
说明:
此类为Struts文件上传类,需要POI包的支持,在附件有,包含的jar包为:
poi-3.8.jar
poi-ooxml-3.8.jar
poi-ooxml-schemas-3.8.jar
xmlbeans-2.4.jar
- POI.zip (8.2 MB)
- 下载次数: 112
发表评论
-
java读取.properties配置文件的几种方法
2017-09-22 09:38 606读取.properties配置文件在实际的开发中使用的很多,总 ... -
java前后台字符串编码转换
2016-12-15 15:52 731此文为应用中遇到字符串编码转换问题网上找资料解决问题记录笔记, ... -
JavaScript replace字符串替换函数的用法
2016-10-19 15:21 812replace 语法 stringObj.replace(rg ... -
JS前端DES加密参数
2016-07-08 14:27 11644DES加密为可解密的机制,这里使用DES进行加密方便后台解密还 ... -
XSS跨站漏洞修复
2016-07-08 11:47 4874避免XSS跨站漏洞的方法之一主要是将用户所提交的内容输入输出进 ... -
JS Date函数整理
2015-01-30 10:43 1009JS Date 对象用于处理日期和时间。 创建 Date 对象 ... -
Java Struts2创建Excel文件并实现文件下载
2015-01-16 17:42 1823在做管理系统的时候,经常会用到文件的下载,特别是Excel报表 ... -
js中setInterval与setTimeout用法
2014-11-19 17:09 993下面详述js中的延时任务与周期任务的实现。 1.setTim ... -
hibernate注解配置联合主键
2014-07-17 09:35 1505有时一个实体的主键可能同时为多个,例如下面使用的字典“Dict ... -
hibernate的clob数据处理
2014-05-21 17:11 1022hibernate的clob数据处理: import or ... -
java设计模式笔记链接地址汇总
2012-12-25 12:02 1044这是iteye博客用户的iulve-rover-163-com ... -
java枚举类型(转)
2012-12-24 18:34 4925public class TestEnum { ... -
内存溢出
2012-11-21 11:07 10541.PermGen space的全称是Permanent Ge ... -
转 js中数据类型判断(转)
2012-11-05 11:07 1104转载自: http://www.cnblogs.com/yjh ... -
日期比较例子
2012-10-16 15:30 1156/** * 时间日期比较处理例子 * @autho ... -
包含日期和时间的选择器(转)
2012-09-27 16:11 1024转载自: http://www.cnblogs.com/swe ... -
基于hibernate的数据持久化处理简单实现类
2012-09-26 20:03 1650package com.forveross.plane.s ... -
Struts2的简单示例action类
2012-09-26 19:57 4646基类: package com.kit.common; ... -
jsp通用分页实现脚本
2012-09-26 17:12 1660工具脚本使用时只需替换url的问号前半部分即可。 此页面需要j ... -
Java过滤html标签
2012-09-21 09:54 1746方法一: String oldStr="< ...
相关推荐
poi读取excel2007和2003兼容工具例子.利用简单工厂方法
NULL 博文链接:https://cgs1999.iteye.com/blog/1525665
之前用jxl发现不支持excel2007,不得以就去用poi实现excel的读取了!本人亲测的一个实例,支持excel2007!
一个读取excel2003、2007、2010的类
java读取excel,并且兼容2003和2007. 附带实例 可自定义表头,行读取开始位置、结束位置,列开始位置、结束位置
兼容解析 Excel2003 Excel2007 的工具 将Excel 每一行数据封装成map集合 方便开发者读取excel中数据 用到的jar包有 xmlbeans-5.1.6.jar 和 poi-bin-3.8-20120326.zip中的包
JAVA用POI读取和创建2003和2007版本Excel完美示例 同时还有相关的类包 注意工程里面别存在冲突类包
ExcelUtil借助反射和POI对Excel读取,省略了以往读取Excel的繁琐步骤,调用ExcelUtil只需要2步,对,你没有看错,2步足以读取到Excel的内容.兼容03/07版Excel.
java使用POI实现excel文件的读取,兼容后缀名xls和xlsx
NULL 博文链接:https://chong0660.iteye.com/blog/1923204
使用POI官网上的事件驱动模式的示例方法,读取单sheet的Excel表格文件(.xlsx),Microsoft Excel和WPS Excel创建的表格文件可以正常读取数据,但是java代码创建的表格文件(不使用软件打开并保存)却读取不到数据。...
poi3.7 完整版jar包,有需要的可以下载,个人用过比较好,已经兼容了excel2007,可以方便的读取excel中的内容 POI是“Point of Interest”的缩写,可以翻译成“兴趣点”吧,每个POI包含四方面信息,名称、类别、经度...
用于office文件的处理的工具类,word文档的读取和处理。excel文件的处理. 官网上下载的免费资源
将读取和写入Excel写成了兼容的工具类。对初学者和开发朋友也许有帮助
ExcelUtil 借助反射和 POI 对 Excel 读取,省略了以往读取 Excel 的繁琐步骤,调用 ExcelUtil 只需要2步,对,你没有看错,2步足以读取到 Excel 的内容。自动赋值,传入对应Model所在路径,即可自动赋值。自动匹配...
通过java poi,对excel表格进行读取的工具类!比较使用以及做过了兼容,可以读取xls,xlsx
poi读取xlsx后缀名的excel时需要用到jar包,希望你们能用到
二、jxls对应版本是2.4,由于POI3.15版本之前读取xlsx文件的批注有bug,3.15版本才解决,所以本实例只能操作excel2003,如果诸位的poi版本在3.15或以上的话,xlsx文件同样可以操作; 三、我没有将修改后的jxls源码...