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读写库的更多相关文章

  1. Apache POI - Excel

    基于模板的EXCEL报表组件ExcelUtils:http://blog.csdn.net/hanqunfeng/article/details/4834875 http://blog.csdn.ne ...

  2. 使用Apache POI导出Excel小结--导出XLS格式文档

    使用Apache POI导出Excel小结 关于使用Apache POI导出Excel我大概会分三篇文章去写 使用Apache POI导出Excel小结--导出XLS格式文档 使用Apache POI ...

  3. Apache POI 实现对 Excel 文件读写

    1. Apache POI 简介 Apache POI是Apache软件基金会的开放源码函式库. 提供API给Java应用程序对Microsoft Office格式档案读和写的功能. 老外起名字总是很 ...

  4. Apache POI读写Excel

    Apache POI是Apache软件基金会的开放源码函式库,POIAPI给Java程序对Microsoft Office格式档案读和写的功能. 官方文档 [https://poi.apache.or ...

  5. Apache POI 读写 Excel 文件

    目录 写入 Excel 文件 读取 Excel 文件 遍历 Excel 文件 需要的 maven 依赖 完整代码 写入 Excel 文件 // 写入 Excel 文件 // ============= ...

  6. Apache POI - Java Excel APIs

    文档来源:https://www.yiibai.com/apache_poi/ POI 什么是Apache POI? Apache POI是一种流行的API,它允许程序员使用Java程序创建,修改和显 ...

  7. Apache POI导出excel表格

    项目中我们经常用到导出功能,将数据导出以便于审查和统计等.本文主要使用Apache POI实现导出数据. POI中文文档 简介 ApachePOI是Apache软件基金会的开放源码函式库,POI提供A ...

  8. Apache poi简介及代码操作Excel

    一.简介 在我们进行企业的系统开发时,难免会遇到网页表格和Excel之间的操作问题(POI是个不错的选择) Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序 ...

  9. 使用apache POI解析Excel文件

    1. Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. 2. POI结构 ...

随机推荐

  1. Subnet Routing Examples

    Routing Table Each row in routing table contains: Destination IP address IP address of next-hop rout ...

  2. iOS 8及以后版本 如何创建UIAlertView?

    1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...

  3. Hadoop 学习之——HDFS

    HDFS是HADOOP中的核心技术之一——分布式文件存储系统.Hadoop的作者Doug Cutting 和Mike 是根据Google发布关于GFS 的研究报告所设计出的分布式文件存储系统. 一.H ...

  4. js判断值是不是全是数字

    if(isNaN(value)){ 不是数字 }else{ 全是数字 }

  5. 【译】2分钟介绍Rx

    原文地址:https://medium.com/@andrestaltz/2-minute-introduction-to-rx-24c8ca793877 翻译去掉了一些口水话(⊙o⊙) 诸位应该已经 ...

  6. ASP.Net Mvc 5 学习记录2015-9-9

    我之前一直都是学习和开发都采用ASP.Net WebForm,对MVC的一直都是一知半解,最初以为ASP.Net WebForm的N层架构就是MVC.其实N层架构设计思想是"高内聚,低耦合& ...

  7. MAC系统 输入管理员账户密码 登录不上

    mac新系统改密码~管理员 升级10.13.2后,很多不会操作了, 那天把系统管理员设置成了普通管理,就不能打开个别软件了, 贼尴尬~~~ 后来找blog才解决,现在分享下~~ http://www. ...

  8. 六个比较好用的php数组Array函数

    1. array_column 返回输入数组中某个单一列的值.2. array_filter 用回调函数过滤数组中的元素.3. array_map 将用户自定义函数作用到给定数组的每个值上,返回新的值 ...

  9. linux搭建的LNMP环境下的mysql授权远程连接

    用phpstudy搭建的lnmp环境下mysql授权远程连接 简单高效 这是因为mysql 里的优先级不是所有人(提前检查防火墙是关闭状态)1.使用phpstudy安装的mysql没有放置到可以直接调 ...

  10. C# string 转 byte[]

    string 转 byte[] /// <summary> /// string 转 byte /// </summary> /// <param name=" ...