1.使用简介
需要满足导出的excel数据能够导入到项目中,目前通过POI对Excel模板进行读取,然后在进行写入数据,最后实现导出功能。啥也不说—上代码
2.代码部分
- 克隆模板中的workbook(主方法)
protected Logger logger = LoggerFactory.getLogger(this.getClass());
public void exportSiChuan(HttpServletRequest request, HttpServletResponse response) {
OutputStream out = null;
try {
// 填充数据 可以 装入Map中
//获取数据
//数据1
Office office = UserUtils.getCurrentOrgan();
//数据2
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setOwnerOrgan(office);
List<PropertyInfo> propertyInfoList = propertyInfoService.findList(propertyInfo);
//下面代码进行科隆workbook
// excel模板路径
String path = request.getSession().getServletContext().getRealPath("/");
//XXX 模版文件需要进行配置读取(模板目录为/static/file)
String filePath = path + "\\static\\file\\templete_2.xlsx";
// 拿到模板文件
FileInputStream tps = new FileInputStream(new File(filePath));
final XSSFWorkbook tpWorkbook = new XSSFWorkbook(tps);
//HSSFWorkbook wb = new HSSFWorkbook(tps); //03版本 xls
//此处设置是为了能够在页面进行弹窗
response.reset();
response.setContentType("application/vnd.ms-excel; charset=utf-8");
response.setCharacterEncoding("utf-8");
respon