Apache POI:Excel读写库
1)Apache POI 简介
Apache POI是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“简洁版的模糊实现“。基本功能如下:
HSSF - 提供读写Microsoft Excel格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
OOXML:Offic Open XML,微软以XML为基础并以ZIP格式压塑的电子文件规范,支持文件,表格,备忘录,幻灯片等格式。从Microsoft Offic 2007开始,OOXML已经成为Microsoft Offic默认的文件格式。
HWPF - 提供读写Microsoft Word格式档案的功能。HSLF - 提供读写Microsoft PowerPoint格式档案的功能。HDGF - 提供读写Microsoft Visio格式档案的功能。
2) maven pom.xml配置
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15-beta2</version>
</dependency>
3)核心代码
/**
* 下载
* @param response
* @param list
* @param clazz
* @param templatePath
* @param templateName
* @throws Exception
*/
public static void download(HttpServletResponse response, List list, Class clazz, String templatePath, String templateName) throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/M/d");
OutputStream os = response.getOutputStream();
try {
XSSFWorkbook workbook = ExcelUtil.getExportWorkBook(list, clazz, templatePath);
response.setHeader("Content-Disposition", "attachment; filename='" + templateName + "'");
response.setContentType("application/vnd.ms-excel");
workbook.write(os);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
}
}
/**
* 获取导出Excel工作薄
*
* @param list 数据源
* @param clazz 类
* @param templatePath 模板路径
* @return
* @throws Exception
*/
public static XSSFWorkbook getExportWorkBook(List list, Class clazz, String templatePath) throws Exception {
//获取导入模板
InputStream in = ExcelUtil.class.getResourceAsStream(templatePath);
XSSFWorkbook wb = new XSSFWorkbook(in);
XSSFSheet sheet = wb.getSheetAt(0);
//写入数据
writeDate(sheet, clazz, list);
return wb;
}
/**
* 写入数据
*
* @param sheet 表格
* @param clazz 类
* @param list 数据源
* @throws Exception
*/
public static void writeDate( XSSFSheet sheet, Class clazz, List list) throws Exception {
int propertyRowNum = 2;
XSSFRow propertyRow = sheet.getRow(propertyRowNum);//获取属性行
int columnCount = propertyRow.getLastCellNum();//获取属性行的列数
//循环赋值
for (int i = 0; i < list.size(); i++) {
Row dataRow = sheet.createRow(propertyRowNum + 1 + i);
//循环为每列赋值
for (int j = 0; j < columnCount; j++) {
String propertyString = propertyRow.getCell(j).getStringCellValue();
if (StringUtil.isEmpty(propertyString)) {
continue;
}
Method getMethod = getGetMethod(clazz, propertyString);//使用反射来获取方法和赋值
if (getMethod != null) {
Cell cell = dataRow.createCell(j);
CellStyle cellStyle = propertyRow.getCell(j).getCellStyle();
cell.setCellStyle(cellStyle);
setCell(list.get(i), getMethod, cell);
} else {
dataRow.createCell(j).setCellValue("");
}
}
}
if(propertyRowNum == sheet.getLastRowNum()){
sheet.removeRow(propertyRow);//没有数据,清空属性行
}else {
sheet.shiftRows(propertyRowNum + 1, sheet.getLastRowNum(), -1);//有数据,清空属性行,全部数据行上移一行(该函数从起始行,到结束行,上移一行)
}
}
/**
* 根据关键词查找对应的get方法
*
* @param objectClass
* @param fieldName
* @return
*/
public static Method getGetMethod(Class objectClass, String fieldName) {
StringBuffer sb = new StringBuffer();
sb.append("get");
sb.append(fieldName.substring(0, 1).toUpperCase());
sb.append(fieldName.substring(1));
try {
return objectClass.getMethod(sb.toString());
} catch (Exception e) {
}
return null;
}
/**
* 设置单元格的值
*
* @param object
* @param method
* @param cell
* @return
* @throws Exception
*/
private static Cell setCell(Object object, Method method, Cell cell) throws Exception {
String returnType = method.getReturnType().getName();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/d");
switch (returnType) {
case "java.util.Date": {
java.util.Date cellValue = (java.util.Date) method.invoke(object);
if (cellValue != null) {
cell.setCellValue(sdf.format(cellValue));
}
}
break;
case "java.lang.Float": {
Float cellValue = (java.lang.Float) method.invoke(object);
if (cellValue != null) {
cell.setCellValue(Double.valueOf(cellValue.toString()));
}
}
break;
case "java.lang.Double": {
Double cellValue = (java.lang.Double) method.invoke(object);
if (cellValue != null) {
cell.setCellValue(cellValue);
}
}
break;
case "java.lang.String": {
String cellValue = (java.lang.String) method.invoke(object);
if (StringUtil.isNotEmpty(cellValue)) {
cell.setCellValue(cellValue);
}
}
case "java.lang.Integer":{
Integer cellValue = (java.lang.Integer) method.invoke(object);
if (cellValue != null) {
cell.setCellValue(cellValue);
}
}
default:
break;
}
return cell;
}
Apache POI:Excel读写库的更多相关文章
- Apache POI - Excel
基于模板的EXCEL报表组件ExcelUtils:http://blog.csdn.net/hanqunfeng/article/details/4834875 http://blog.csdn.ne ...
- 使用Apache POI导出Excel小结--导出XLS格式文档
使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI ...
- Apache POI 实现对 Excel 文件读写
1. Apache POI 简介 Apache POI是Apache软件基金会的开放源码函式库. 提供API给Java应用程序对Microsoft Office格式档案读和写的功能. 老外起名字总是很 ...
- Apache POI读写Excel
Apache POI是Apache软件基金会的开放源码函式库,POIAPI给Java程序对Microsoft Office格式档案读和写的功能. 官方文档 [https://poi.apache.or ...
- Apache POI 读写 Excel 文件
目录 写入 Excel 文件 读取 Excel 文件 遍历 Excel 文件 需要的 maven 依赖 完整代码 写入 Excel 文件 // 写入 Excel 文件 // ============= ...
- Apache POI - Java Excel APIs
文档来源:https://www.yiibai.com/apache_poi/ POI 什么是Apache POI? Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显 ...
- Apache POI导出excel表格
项目中我们经常用到导出功能,将数据导出以便于审查和统计等.本文主要使用Apache POI实现导出数据. POI中文文档 简介 ApachePOI是Apache软件基金会的开放源码函式库,POI提供A ...
- Apache poi简介及代码操作Excel
一.简介 在我们进行企业的系统开发时,难免会遇到网页表格和Excel之间的操作问题(POI是个不错的选择) Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序 ...
- 使用apache POI解析Excel文件
1. Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. 2. POI结构 ...
随机推荐
- react 之 ref
react提供一个refs的安全口,做到‘接触’或调用 从render()返回的组件实例的方法.DOM节点. 用法:1. ref Callback属性 ref 属性可以是一个回调函数,此函数会在这个组 ...
- java ssm 后台框架平台 项目源码 websocket IM quartz springmvc
A代码编辑器,在线模版编辑,仿开发工具编辑器,pdf在线预览,文件转换编码B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,快速开发利器)+快速表单构建器freemaker模版技术 ,0个 ...
- Oracle特性总结
最近开发项目使用了Oracle,根据总体架构一开始选择使用Mybatis,发现核心模块用Mybatis效率不够,切换到jdbc实现,效率大增.Oracle可是个庞然大物,特性多多,丝毫不能马虎,否则很 ...
- Springmvc+Spring+Mybatis整合开发(架构搭建)
Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...
- js扩展String.prototype.format字符串拼接的功能
1.题外话,有关概念理解:String.prototype 属性表示 String原型对象.所有 String 的实例都继承自 String.prototype. 任何String.prototype ...
- SHOPEX快递物流单号查询插件
本SHOPEX快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世快递.汇通快递.中通快递.天天快递等知 ...
- python中函数参数的引用方式
值传递和引用传递时C++中的概念,在python中函数参数的传递是变量指向的对象的物理内存地址!!! python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是“传对象引用”的方 ...
- python学习——面对对象进阶
一.isinstance和issubclass isinstance(obj,cls)检查是否obj是否是类 cls 的对象 class Foo: pass a = Foo() print(isins ...
- django_ORM学生管理系统
一.新建django项目准备工作 CMD新建项目命令:django-admin startproject [项目名称] pycharm的project目录里新建app命令:python manage. ...
- 用kubeadm构建k8s集群部署
一.环境 三台centos机器 二.软件及容器准备 1.安装docker环境 本例安装 docker-ce版本,repo源为docker-ce.repo文件,拷贝到 /etc/yum.repos.d下 ...