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. 【MongoDB】CentOS上安装MongoDB

    权限部分尚未测试完成,请勿参考. 1.去官方网站下载Mongodb for linux的包,我没找到CentOS的,随便下载了个mongodb-linux-x86_64-amazon-3.2.0.tg ...

  2. careercup-扩展性和存储限制10.4

    题目 有一个数组,里面的数在1到N之间,N最大为32000.数组中可能有重复的元素(即有的元素 存在2份),你并不知道N是多少.给你4KB的内存,你怎么把数组中重复的元素打印出来. 解答 我们有4KB ...

  3. I/O复用——各种不同的IO模型

    一.概述 我们看到上面的TCP客户同时处理两个输入:标准输入和TCP套接字.我们遇到的问题就是在客户阻塞于(标准输入上的)fgets调用期间,服务器进程会被杀死.服务器TCP虽然正确地给客户TCP发送 ...

  4. 设计一个方法injectBeforeAsyncSend,能够实现如下功能:在发起异步请求之前打印出请求的类型、URL、method、body、timestamp 等信息。

    异步请求逻辑注入 工作中我们需要对异步请求的请求信息打印日志,但是又不能耦合在业务代码中打印.请设计一个方法injectBeforeAsyncSend,能够实现如下功能:在发起异步请求之前打印出请求的 ...

  5. Mongodb使用命令总结

    使用时的Mongodb版本为:3.2 1.导出: mongoexport --host mongodb1.example.net --port 37017 --username user --pass ...

  6. IOS中Hybird实现

    现在Hybird这块,网上也有很多文章,最近研究了下,分享给大家. 什么是Hybird技术? 1.一般是指WebView和Native技术混合而成的一套技术方案 2.也可以理解成,非Native技术与 ...

  7. OOP导论系列---抽象过程

    OOP导论系列---抽象过程 所有编程语言都提供抽象机制.可以认为,人们所能解决的问题的复杂性直接取决于抽象的类型和质量.所谓“类型”是指“所抽象的是什么?”你可以抽取待求解问题的任何概念化构件,如: ...

  8. 搭建Extjs框架(一)

    搭建Extjs框架 pc端 github https://github.com/Status400/Extjs-6.2.0-demo   欢迎start 准本工作:       官方下载Extjs  ...

  9. VMware Workstation 安装Vmware tools 是 出现vmware tools unavailable

    这个问题是因为虚拟机安装的时候操作系统选择的不对,在Virtual Machine Settings中选择Options,在General中选择正确的操作系统类型 例如Guest operating ...

  10. Centos6.5 VM网络故障,可以Ping 通网关,无法上网或者访问其它网段

    首先查看cat /etc/sysconfig/network-scripts/ifcfg-eth0  配置是否正确 查看cat /etc/udev/rules.d/70-persistent-net. ...