poi导出word、excel
在实际的项目开发中,经常会有一些涉及到导入导出的文档的功能。apache开源项目之一poi对此有很好的支持,对之前的使用做一些简要的总结。
1,导入jar
为了保证对格式的兼容性,在项目的pom.xml添加这三个jar:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
2,生成文件、下载
一般我们对导出的数据变化不会太大,所以考虑到重复使用,我们可以在服务端生成一个文件,在用户再次导出时直接取这个文件下载即可,相当于一个缓存,当然如果服务端文件数过多需要定时任务或者shell去清理.. 不多啰嗦,下面开始贴代码
/**
* 生成考勤信息文档
* @param date 导出日期
* @param type 1考勤统计总数word,2考勤详情excle
* @return
* @throws IOException
*/
@RequestMapping(value="/export_message",method=RequestMethod.GET)
public @ResponseBody CommonResponse export(@RequestParam String date,
@RequestParam Integer type,
@RequestParam Integer orgId,
@RequestParam String orgName,
HttpServletResponse response,
HttpServletRequest request) throws IOException{
CommonResponse rsp = null;
if (!StringUtil.isEmpty(date)) {
String fileName = "";
File file;
if (type == 1) {
fileName = date.trim()+ ".docx";
file = new File(WORD+ fileName);
}else{
fileName = date.trim() + orgId + ".xls";
file = new File(EXCEL + fileName);
} if (file.exists()) {
rsp = new CommonResponse(1, "exists",fileName);
}else{
//文件不存在,用poi生成
int rs = messageService.exportMessage(DateUtil.str2Date(date),type,orgId,orgName);
if (rs == 1){
rsp = new CommonResponse(0, "ok",fileName);
}else{
rsp = new CommonResponse(-1, "error");
}
}
}else{
//参数错误
rsp = new CommonResponse(-1,"error");
logger.error("Fail to convert the parameters for export_message,date:{}", date);
}
return rsp;
}
/**
* 下载考勤信息文档
* @param response
* @param request
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
@RequestMapping(value="/down_info_file",method = RequestMethod.GET)
public @ResponseBody void downFile(HttpServletResponse response,HttpServletRequest request) throws FileNotFoundException, UnsupportedEncodingException { String filePath,newFileName;
String fileName = request.getParameter("fileName"); //兼容性配置
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0){
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");//firefox
}else if(request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0){
fileName = URLEncoder.encode(fileName, "UTF-8");//IE
} if (fileName.endsWith(".docx"))
filePath = WORD;
else
filePath = EXCEL; // 读到流中
InputStream inStream = new FileInputStream(filePath + fileName); if (fileName.endsWith(".docx"))
newFileName = fileName.substring(0, 10) + "勤务上报数与排名统计.docx";
else
newFileName = fileName.substring(0, 10) + "勤务详情统计.xls"; // 设置输出的格式
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(newFileName, "UTF-8")+ "\"");
// 循环取出流中的数据
byte[] b = new byte[1024];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
logger.error("Download the document is failed,message:{}", e.getMessage());
}
}
@Override
public int exportMessage(Date date,Integer type,Integer orgId,String orgName) { int rs = 0; File wordfile = new File(WORD);
File excelfile = new File(EXCEL);
if (!wordfile.exists() || !excelfile.exists()) {
wordfile.mkdirs();// 目录不存在的情况下,创建目录。
excelfile.mkdirs();
} String exportExcel = EXCEL + DateUtil.date2Str(date) + orgId +".xls";
String exportWord = WORD + DateUtil.date2Str(date) +".docx";
if (type == 1) {
try {
List<MessageCount> listMc = new ArrayList<>();
List<MessageTopUser> listMt = null;
List<Organization> list = organizationService.findLessFour();
for (int i = 0; i < list.size(); i++) {
Integer _orgId = list.get(i).getOrgId();
if (_orgId != 1) {
MessageCount mc = messageDao.findAllCountByOrgId(date, _orgId);
listMt = messageDao.findTopThree(date);
listMc.add(mc);
}
} rs = exportWord(date, exportWord, listMc, listMt);
} catch (Exception e) {
e.printStackTrace();
}
}else{
List<Message> mes = messageDao.exportMessage(date,orgId);
rs = exportExcle(date, mes, exportExcel, orgName);
} return rs;
}
3,word文档流生成
public int exportWord(Date date,String exportPath, List<MessageCount> listMc, List<MessageTopUser> listMt) throws Exception{
int result = 1;
logger.debug("export excle start!,which date is : {}" + date); XWPFDocument doc = new XWPFDocument();//新建一个文档 XWPFParagraph nav = doc.createParagraph();
nav.setStyle("1"); //1级大纲
XWPFRun runNav = nav.createRun();
runNav.setBold(true); //加粗
runNav.setText("【塔式智能系统每日监测情况通报】"); String text = DateUtil.date2Str(date) + ",";
int alarm = 0,news = 0,danger = 0,duty = 0,other = 0,police = 0,trail = 0; String allOutText = "";
for (int i = 0; i < listMc.size(); i++) {
MessageCount list = listMc.get(i);
int getalarm = list.getAlarm().intValue();//一键报警
int getnews = list.getNews().intValue();//动态
int getdanger = list.getDanger().intValue();//隐患
int getduty = list.getDuty().intValue();//勤务记录
int getother = list.getOther().intValue();//其他
int getpolice = list.getPolice().intValue();//警情
int gettrail = list.getTrail().intValue();//线索 alarm += getalarm;//一键报警
news += getnews;//动态
danger += getdanger;//隐患
duty += getduty;//勤务记录
other += getother;//其他
police += getpolice;//警情
trail += gettrail;//线索 int count = getalarm + getnews + getdanger + getduty + getother + getpolice + gettrail;
/*if (count != 0) {}*/
String name = organizationService.findByOrgId(list.getOrgId()).getName().replace("中队", "所");
allOutText += i+1 +"." + name + count + "条。";
}
int allCount = alarm + news + danger + duty + other + police + trail;
text += "发布信息总数" + allCount + "条。";
text += "其中动态" + news + "条,";
text += "警情" + police + "条,";
text += "线索" + trail + "条,";
text += "隐患" + danger + "条,";
text += "勤务记录" + duty + "条,";
text += "一键报警" + alarm + "条,";
text += "其他" + other + "条。"; XWPFParagraph para = doc.createParagraph();
XWPFRun run = para.createRun();
run.setText(text);
run.setFontSize(8);
run.setFontFamily("微软雅黑"); XWPFParagraph everyOrg = doc.createParagraph();
XWPFRun ev = everyOrg.createRun();
ev.setText(allOutText);
ev.setFontSize(8);
ev.setFontFamily("微软雅黑"); String topThree = "";
for (int j = 0; j < listMt.size(); j++) {
int total = listMt.get(j).getTotal().intValue(); NumberFormat nt = NumberFormat.getPercentInstance();
nt.setMinimumFractionDigits(2);//百分数保留两位
double divisor = (double)total/allCount;
String percent = nt.format(divisor);
logger.debug("The export word data for percent is "+percent);
/*DecimalFormat df2 = new DecimalFormat("###.00");//保留两位小数
System.out.println("小数"+df2.format(p));*/
topThree += j+1 + "." + listMt.get(j).getOrgName().replace("中队", "所") + listMt.get(j).getUname() + ",发布数" + total + "条,占比" + percent + "; ";
} XWPFParagraph topThreeP = doc.createParagraph();
XWPFRun evp = topThreeP.createRun();
evp.setText(topThree);
evp.setFontSize(8);
evp.setFontFamily("微软雅黑"); try {
FileOutputStream fout = new FileOutputStream(exportPath);
logger.debug("export excle end!"); doc.write(fout); //把doc输出到输出流 fout.close();
}
catch (Exception e){
result = 0;
logger.error("export excle error!,{}",e.getLocalizedMessage());
} return result;
}
4,excel文档流生成
@SuppressWarnings("deprecation")
public int exportExcle(Date date,List<Message> mes,String exportPath,String orgName){
int result = 1;
logger.debug("Start export excle,which date is : {}" + date);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(DateUtil.date2Str(date));
HSSFRow rowMerging = sheet.createRow((short) 0);
HSSFRow row = sheet.createRow((short) 1); //标题样式
HSSFCellStyle titleStyle = wb.createCellStyle();
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
titleStyle.setBorderBottom((short)100);// 下边框
titleStyle.setBorderTop((short)100);// 上边框
HSSFFont titleFont = wb.createFont();
titleFont.setFontHeightInPoints((short) 20);//设置字体大小
titleFont.setFontName("宋体");
titleFont.setColor(HSSFColor.RED.index);//红字
titleStyle.setFont(titleFont); //导航行样式
HSSFCellStyle navStyle = wb.createCellStyle();
navStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
navStyle.setBorderBottom((short)50);// 下边框
navStyle.setBorderTop((short)50);// 上边框
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 14);
navStyle.setFont(font); sheet.addMergedRegion(new Region(0, (short) (0), 0,(short) (11))); //设置跨行, 四个参数分别是:起始行,起始列,结束行,结束列
//设置列宽,第一个参数代表列id(从0开始),第2个参数代表宽度值
sheet.setColumnWidth(0, 6500);
sheet.setColumnWidth(1, 4000);
sheet.setColumnWidth(2, 8000);
sheet.setColumnWidth(3, 5000);
sheet.setColumnWidth(4, 4000);
sheet.setColumnWidth(5, 4000);
sheet.setColumnWidth(6, 4000);
sheet.setColumnWidth(7, 4000);
sheet.setColumnWidth(8, 4000);
sheet.setColumnWidth(9, 4000);
sheet.setColumnWidth(10, 4000); HSSFCell merging = rowMerging.createCell((short) 0);
merging.setCellValue(DateUtil.date2Str(date) + orgName + "群防群治数据统计表");
merging.setCellStyle(titleStyle); HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("时间");
cell.setCellStyle(navStyle);
cell = row.createCell((short) 1);
cell.setCellValue("主题");
cell.setCellStyle(navStyle);
cell = row.createCell((short) 2);
cell.setCellValue("描述");
cell.setCellStyle(navStyle);
cell = row.createCell((short) 3);
cell.setCellValue("发送人");
cell.setCellStyle(navStyle);
cell = row.createCell((short) 4);
cell.setCellValue("机构ID");
cell.setCellStyle(navStyle);
cell = row.createCell((short) 5);
cell.setCellValue("机构名称");
cell.setCellStyle(navStyle);
cell = row.createCell((short) 6);
cell.setCellValue("派出所ID");
cell.setCellStyle(navStyle);
cell = row.createCell((short) 7);
cell.setCellValue("所属派出所");
cell.setCellStyle(navStyle);
cell = row.createCell((short) 8);
cell.setCellValue("发布数");
cell.setCellStyle(navStyle);
cell = row.createCell((short) 9);
cell.setCellValue("发布率");
cell.setCellStyle(navStyle);
cell = row.createCell((short) 10);
cell.setCellValue("重复数");
cell.setCellStyle(navStyle);
cell = row.createCell((short) 11);
cell.setCellValue("不规范数");
cell.setCellStyle(navStyle); //内容样式
HSSFCellStyle contentStyle = wb.createCellStyle();
contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle.setBorderBottom((short)50);// 下边框
titleStyle.setBorderTop((short)50);// 上边框
HSSFFont fontcontent = wb.createFont();
fontcontent.setFontName("宋体");
fontcontent.setFontHeightInPoints((short) 12);
navStyle.setFont(fontcontent); for (int i = 1; i <= mes.size(); i++)
{
row = sheet.createRow((int) i + 1);
Message rs = mes.get(i - 1); HSSFCell cellContent = row.createCell((short) 0);
cellContent.setCellValue(DateUtil.toTimeString(rs.getSendTime()));
cellContent.setCellStyle(contentStyle);
cellContent = row.createCell((short) 1);
cellContent.setCellValue(rs.getMsgTypeName());
cellContent.setCellStyle(contentStyle);
cellContent = row.createCell((short) 2);
cellContent.setCellValue(StringUtil.isNull(rs.getContent())?"":rs.getContent());
cellContent.setCellStyle(contentStyle);
cellContent = row.createCell((short) 3);
cellContent.setCellValue(rs.getUname());
cellContent.setCellStyle(contentStyle);
cellContent = row.createCell((short) 4);
cellContent.setCellValue(rs.getOrgId());
cellContent.setCellStyle(contentStyle);
cellContent = row.createCell((short) 5);
cellContent.setCellValue(rs.getOrgName());
cellContent.setCellStyle(contentStyle);
cellContent = row.createCell((short) 6);
int ancestors = rs.getAncestors().equals("") ? rs.getOrgId() : Integer.parseInt(rs.getAncestors());
cellContent.setCellValue(ancestors);
cellContent.setCellStyle(contentStyle);
} try
{
FileOutputStream fout = new FileOutputStream(exportPath);
logger.debug("export excle end!"); wb.write(fout); fout.close();
}
catch (Exception e)
{
result = 0;
logger.error("export excle error!,{}",e.getLocalizedMessage());
}
return result;
}
5,总结
其实导出是很简单的,贴上代码后基本上意思就出来了,无非就是通过使用XWPFDocument、HSSFWorkbook这两个类,set相关属性值,数据通过sql从数据库里读取数据做一些相关的循环逻辑,再通过文档流的形式反馈给客户端。可能相对来说导入会比较麻烦,首先是从服务端下载一个模板,然后再根据模板填入数据上传到服务器,这个时候需要做大量的验证处理,再反馈给客户有一个预览界面等等,思路大致是这样,后续再整理excel导入。
poi相关属性参考:http://poi.apache.org/
各jar包作用参考:http://www.07net01.com/2014/09/63956.html
poi导出word、excel的更多相关文章
- java工具类POI导出word
1.新建一个word,里面填写内容,如: 2.导出wordjava类 /** * POI导出word测试 * @throws Exception */ @RequestMapping(value=&q ...
- 【MVC】 非常简单的页面导出 WORD, EXCEL方法
[MVC] 页面导出 WORD, EXCEL 前端 js function output() { var para = new Object(); para.html = getHtml(" ...
- poi导出word
最近做了个poi导出word的功能 下面是代码: 一个可以参考的例子: package com.lzb.crm.web; import java.io.FileOutputStream; import ...
- poi导出word表格详解 超详细了
转:非常感谢原作者 poi导出word表格详解 2018年07月20日 10:41:33 Z丶royAl 阅读数:36138 一.效果如下 二.js代码 function export_word( ...
- poi导出word时设置兼容性
接上一篇poi导出word http://www.cnblogs.com/xiufengd/p/4708680.html. public static void setAuto(XWPFDocumen ...
- 使用POI导出Word(含表格)的实现方式及操作Word的工具类
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- SpringBoot集成文件 - 如何使用POI导出Word文档?
前文我们介绍了通过Apache POI导出excel,而Apache POI包含是操作Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API.所以 ...
- poi导出的excel的数字小数位过多?
最近在使用Apache的POI组件对Excel进行操作,在对excel导出的时候,导出的数字本来只有两位小数,得到的结果就变成了很多位小数.如下面的图所示: 虽然对单元格使用了setCellStyle ...
- 网页内容导出word/excel的js代码
IE设置: 工具-> Internet选项-> 安全->自定义级别-> 对没有标记安全级别的ActiveX控件进行初始化 设为启用! 1.导出word //指定区域导出到Wo ...
- Spring MVC中使用POI导出Word
内容绝大部分来源于网络 准备工作 准备[XwpfTUtil]工具类(来源于网络) 准备word模版 下载[XwpfTUtil]工具类 import org.apache.poi.xwpf.usermo ...
随机推荐
- PyQt 自定义信号带参数
import sys from PyQt5.QtCore import pyqtSignal, QObject from PyQt5.QtWidgets import QMainWindow, QAp ...
- cocos2d-x 3.10 PageView BUG
cocos2d-x 3.10 PageView 拖动滚动到下一个单元,没事件,3.11有修复.
- 下载imagenet2012数据集
摸索了一下,imagenet2012下载,跟大家分享一下 用迅雷会员加速都可以下载,有的用百度云也可以离线下载 http://www.image-net.org/challenges/LSVRC/20 ...
- Beta阶段第三次Scrum Meeting
情况简述 Beta阶段第三次Scrum Meeting 敏捷开发起始时间 2016/12/12 22:00 敏捷开发终止时间 2016/12/13 22:00 会议基本内容摘要 讨论决定了APP的名称 ...
- SSH--1
package com.etc.action; import java.io.IOException; import java.io.PrintWriter; import java.util.Has ...
- 如何将EXCEL表导入ORACLE数据库中?【转】
来源:https://zhidao.baidu.com/question/383828330.html?qbl=relate_question_2&word=excel%20%B1%ED%CA ...
- php基础之gd图像生成、缩放、logo水印和简单验证码实现
gd库是php最常用的图片处理库之一(另外一个是imagemagick),可以生成图片.验证码.水印.缩略图等等.要使用gd库首先需要开启gd库扩展,windows系统下需要在php.ini中将ext ...
- 图解JVM字节码执行引擎之栈帧结构
一.执行引擎 “虚拟机”的概念是相对于“物理机”而言的,这两种“机器”都有执行代码的能力.物理机的执行引擎是直接建立在硬件处理器.物理寄存器.指令集和操作系统层面的:而“虚拟机”的执行引擎是 ...
- ajax 提交表单文件上传
<form action="" method="post" enctype="multipart/form-data" id=&quo ...
- boost之lexical_cast
第一次翻译,虽然是个很简单的函数介绍... 文件boost/lexical_cast.hpp中定义了此函数: namespace boost { class bad_lexical_cast; tem ...