首先poi需要的jar包全部导入工程
温馨提示:千万要注意xmlbeans的版本号,最好使用2.6.0以上的版本,否则poi会报错的噢!
一,poi根据excel模板,修改内容导出新excel 文档(只支持HSSF)
excel模板就存放在了静态文件夹下,然后拿到这个文档之后,通过poi对单元格的赋值进行创建。
public class CreateExcel {
public static void main(String[] args) throws IOException {
//拿到excel模板的输入流
InputStream inputStream = this.getClass().getResourceAsStream("/static/attachments/03-企业认证产品技术信息表V1.0.xls");
POIFSFileSystem fs = new POIFSFileSystem(inputStream);
//读取excel模板
HSSFWorkbook wb = new HSSFWorkbook(fs);
//读取了模板内所有sheet内容
HSSFSheet sheet = wb.getSheetAt(0);
//在相应的单元格进行赋值
HSSFCell cell = sheet.getRow(1).getCell(3);
cell.setCellValue("测试");
HSSFCell cell2 = sheet.getRow(3).getCell(3);
cell2.setCellValue("数据");
HSSFCell cell3 = sheet.getRow(0).getCell(0);
cell3.setCellValue("大标题");
//修改模板内容导出新模板(导出到了本地磁盘)
FileOutputStream out = new FileOutputStream("D:/export.xls");
wb.write(out);
out.close();
}
}
二,poi导出excel文档输出到浏览器下载
上面的导出只是导出到了本地磁盘,正常业务应该是输出到浏览器下载。
输出到浏览器下载的根本原因就是把输出流交给Response对象,然后超链接访问并接收Response对象。
因此按钮应该是超链接形式的跳转,跳转到导出的后台方法(不是用不了ajax,老是因为类型问题出错,索性废弃)
function jsxx_export() {
location.href = "/export/jsxx?pk="+pk;
}
后台导出方法(参数要加上HttpServletResponse)
@RequestMapping("/jsxx")
@ResponseBody
public void jsxxExport(String pk,HttpServletResponse response) throws Exception{
KpFileTechnicalMessage message = (KpFileTechnicalMessage) this.getLightServiceUtil().callServiceWithParam("TechMessageSc_getById", pk);
//操作系统类型及版本
String operateSystemKind = message.getOperateSystemKind();
//###################这里从数据库查询到数据(中间省略)#####################
//其他商业闭源软件
String otherClosedSoftware = message.getOtherClosedSoftware();
InputStream inputStream = this.getClass().getResourceAsStream("/static/attachments/03-企业认证产品技术信息表V1.0.xls");
POIFSFileSystem fs=new POIFSFileSystem(inputStream);
HSSFWorkbook wb=new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheet("产品技术信息表");
sheet.getRow(1).getCell(3).setCellValue(operateSystemKind);
//#################给excel单元格赋值(中间省略)####################
sheet.getRow(18).getCell(3).setCellValue(otherClosedSoftware);
//调用下面的输入到浏览器下载的方法
exportExcelByDownload(wb,response,"技术信息表导出");
}
excel表格直接浏览器下载的通用方法
//excel表格直接浏览器下载
public void exportExcelByDownload(HSSFWorkbook wb, HttpServletResponse httpServletResponse, String fileName) throws Exception {
//响应类型为application/octet- stream情况下使用了这个头信息的话,那就意味着不想直接显示内容
//httpServletResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
//attachment为以附件方式下载
httpServletResponse.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(
fileName + ".xls",
"utf-8"));
/**
* 代码里面使用Content-Disposition来确保浏览器弹出下载对话框的时候。
* response.addHeader("Content-Disposition","attachment");一定要确保没有做过关于禁止浏览器缓存的操作
*/
httpServletResponse.setHeader("Cache-Control", "No-cache");
httpServletResponse.flushBuffer();
wb.write(httpServletResponse.getOutputStream());
wb.close();
}
这样点击按钮进行导出就直接在浏览器左下角进行下载了!
三,poi根据excel模板,修改内容导出新excel 文档(支持XSSF)
HSSF是xls格式的excel文档,而XSSH则是xlsx格式的excel文档,上面的导出excel是支持HSSF的,想要支持XSSH格式的话,其实具体过程还是和上面的导出过程一样,只不过在创建Workbook对象的时候有所区别。
创建.xls的workbook对象
InputStream inputStream = this.getClass().getResourceAsStream("/static/attachments/03-企业认证产品技术信息表V1.0.xls");
POIFSFileSystem fs=new POIFSFileSystem(inputStream);
HSSFWorkbook wb=new HSSFWorkbook(fs);
创建.xlsx的workbook对象
InputStream inputStream = this.getClass().getResourceAsStream("/static/attachments/07-适配认证资料checklist.xlsx");
//下面的两行(XSSH)就用不了了,因为下面的是HSSF,注掉
// POIFSFileSystem fs=new POIFSFileSystem(inputStream);
// HSSFWorkbook wb=new HSSFWorkbook(fs);
//使用workbook工厂创建workbook文档对象
Workbook wb = WorkbookFactory.create(inputStream);
其他的用法都一样!
四,poi根据word模板(书签)创建导出word文档
创建一个要导出固定格式的word模板。
写测试类并运行
package com.ftx.word;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WordTest {
public static void main(String[] args) {
//测试数据准备
//1.标题
String title = "t1heluosh1-使用POI导出Word";
//2.段落数据
String font_song_four_red = "这里是宋体四号红色字体";
String font_black_three_black = "这里是黑体三号号黑色字体";
String font_micro_five_red = "这里是微软雅黑五号红色字体";
//存放段落数据
Map map = new HashMap();
map.put("title", title);
map.put("font_song_four_red", font_song_four_red);
map.put("font_black_three_black", font_black_three_black);
map.put("font_micro_five_red", font_micro_five_red);
//3.表格数据
List> excelMapList = new ArrayList>();
Map excelMapTemp = null;
for (int i=1;i<11;i++) {
excelMapTemp = new HashMap();
excelMapTemp.put("excel.no1", "one-"+i);
excelMapTemp.put("excel.no2", "two-"+i);
excelMapTemp.put("excel.no3", "three-"+i);
excelMapTemp.put("excel.no4", "four-"+i);
excelMapTemp.put("excel.no5", "five-"+i);
excelMapList.add(excelMapTemp);
}
//模板存放位置
String demoTemplate = "D:/template.docx";
//生成文档存放位置
String targetPath = "D:/target.doc";
//初始化导出
WordExport export = new WordExport(demoTemplate);
try {
export.init();
} catch (IOException e) {
e.printStackTrace();
}
try {
export.export(map);
//0为表格的下标,第一个表格下标为0,以此类推
export.export(excelMapList, 0);
export.generate(targetPath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
加入poi导出word的工具类(即插即用)
package com.ftx.word;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
i