xlsm文件就是带宏的Excel文件
  POI和EasyExcel都是无法创建xlsm文件的。xlsm文件只能在Excel里面另存为xlsm文件。我通过自己的尝试,发现POI和EasyExcel创建xlsm虽然不会报错,但是打开文件都是提示损坏,而wps又能正常打开。
  POI和EasyExcel创建xlsm本质上应该都是把文件的后缀改为.xlsm,因为我把生成的损坏的xlsm文件后缀改为xlsx,可以正常打开。
POI创建xlsm文件不会报错,但是打开下载的文件时候会报错文件已损坏
这是因为POI只能对已有的xlsm文件进行查询与更改,而不能创建xlsm文件
当需求需要能下载xlsm文件时,解决办法就是在服务器中放一个正常的xlsm文件,然后用POI对文件进行清空,放上需要的数据传给接口再下载到本地。

以下是部分源码

@Permission("")@RequestMapping("/exportData")public void exportData(@RequestParam(value = "table") String table,@RequestParam(value = "uploadTime", required = false) String uploadTime,@RequestParam("tid") Integer tid, @RequestParam(value = "ym", required = false) String ym) {File file1 = new File(uploadPath + table + ".xlsm");table = table.trim().toUpperCase();User user = (User) session.getAttribute("login");List<Mapping> mapping = insertConfigService.findMappingList(table, db);//List<com.gr.entity.master.Mapping> mapping = dataMappingService.findMappingByTable(table);//模板表中文名String excelname = mapping.get(0).getTableNote();if (file1.exists()) {//存在//将数据放入服务器上的excelPath filePath = Paths.get(file1.toString());// declare a workbookXSSFWorkbook workbook;try {/** READING from the .xlsx file:*/FileInputStream in = new FileInputStream(filePath.toFile());workbook = XSSFWorkbookFactory.createWorkbook(in);XSSFSheet sheet = workbook.getSheetAt(0);FileOutputStream out1 = new FileOutputStream(filePath.toAbsolutePath().toString());sheet = workbook.getSheetAt(0);// create new cells and write the words into them//清空标题和数据for (int i = 1; i <= sheet.getLastRowNum(); i++) {XSSFRow row3 = sheet.getRow(i);for (int j = 0; j < row3.getLastCellNum(); j++) {XSSFCell cell = row3.getCell(j);cell.setCellValue("");}}//添加标题XSSFRow row1 = sheet.createRow(0);for (int j = 0; j < cols.size(); j++) {XSSFCell cell = row1.createCell(j);cell.setCellValue(cols.get(j));}//添加数据for (int i = 1; i <= data.size(); i++) {XSSFRow row = sheet.createRow(i);for (int j = 0; j < data.get(0).size(); j++) {XSSFCell cell = row.createCell(j);cell.setCellValue(data.get(i - 1).get(j).toString());}}// close the FileInputStreamin.close();// write the workbook using the FileOutputStreamworkbook.write(out1);// force the FileOutputStream to write everything until it is emptyout1.flush();// close the FileOutputStreamout1.close();// close the workbook.workbook.close();} catch (FileNotFoundException e) {System.err.println("The file \"" + filePath.toAbsolutePath().toString() + "\" could not be found.");e.printStackTrace();} catch (IOException e) {System.err.println("Error while reading the file \"" + filePath.toAbsolutePath().toString() + "\"");e.printStackTrace();} catch (EmptyFileException e) {System.err.println("The supplied file \"" + filePath.toAbsolutePath().toString() + "\" is empty.");e.printStackTrace();}//从服务器上下载response.setCharacterEncoding("UTF-8");response.setHeader("content-Type", "application/vnd.ms-excel");response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(excelname, "UTF-8") + ".xlsm");response.setHeader("Pragma", URLEncoder.encode(excelname, "UTF-8"));byte[] buffer = new byte[1024];FileInputStream fis = null; //文件输入流BufferedInputStream bis = null;OutputStream os = null; //输出流os = response.getOutputStream();fis = new FileInputStream(file1);bis = new BufferedInputStream(fis);int len = 0;while ((len = bis.read(buffer)) > 0) {os.write(buffer, 0, len);}bis.close();fis.close();return;}//写文件EasyExcel.write(response.getOutputStream()).head(heads).registerWriteHandler(new HeadWriteHandlerImpl(mapping.size(), mm)).registerWriteHandler(new Custemhandler()).sheet("Sheet1").doWrite(data);//将文件下载到固定地址EasyExcel.write(file1.toString()).head(heads).registerWriteHandler(new HeadWriteHandlerImpl(mapping.size(), mm)).registerWriteHandler(new Custemhandler()).sheet("Sheet1").doWrite(data);//xlsx转xlsm}

POI无法创建xlsm文件相关推荐

  1. 使用iText库创建PDF文件

    前言 译文连接:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ 对于excel文件的读 ...

  2. 基于POI的读写Excel文件的工具类

    依赖的jar包: import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStrea ...

  3. python gps与高德地图poi_Python——使用高德API获取指定城指定类别POI并实现XLSX文件合并...

    # 以下内容为原创,转载请注明出处 1 import xlrd #读xlsx 2 import xlsxwriter #写xlsx 3 import urllib.request #url请求,Pyt ...

  4. 使用POI读写word docx文件

    目录 1     读docx文件 1.1     通过XWPFWordExtractor读 1.2     通过XWPFDocument读 2     写docx文件 2.1     直接通过XWPF ...

  5. POI API 创建Excel 文档

    原文来自 http://www.blogjava.net/hezhi/articles/POI.html HSSF提供给用户使用的对象在org.apache.poi.hssf.usermodel包中, ...

  6. docx poi 原理_使用POI读写word docx文件

    目录 1     读docx文件 1.1     通过XWPFWordExtractor读 1.2     通过XWPFDocument读 2     写docx文件 2.1     直接通过XWPF ...

  7. 基于DOM4j和POI实现的XML文件转换为XLS(即标准EXCEL)的JAVA程序

    基于DOM4j和POI实现的XML文件转换为XLS(即标准EXCEL)的JAVA程序 #第一次写博客有点小紧张,希望各位大牛多多包涵 大四狗最近来公司实习,经理大大给了一个测试开发能力的任务,读取xm ...

  8. C#日常开发随手记------COM组件(Microsoft.Office.Interop.Excel)操作excel、如何创建\删除文件夹

    文章中写了点过程有点啰嗦,想直接看代码的直接下拉看加粗标题处 第一次使用COM组件操作excel,遇到了点坑,也有些感触. 一般来说操作excel,我比较常用的是OleDB,但是OleDB需要安装Ac ...

  9. 使用POI批量导出Excel文件(SSM)

    文章目录 前言 如何使用POI批量导出Excel文件(SSM) 一.什么是POI? 模块 二.使用步骤 1.引入依赖 2.mapper层代码 包括Mapper接口.Mapper SQL代码 Mappe ...

  10. android 创建 file 文件,文本 并写入内容

    创建file 文件在android 6.0 之后需要用户手动开启权限下面介绍下使用 这里只是写入内容所以就在配置文件中添加写入的权限, <uses-permission android:name ...

最新文章

  1. HTML5培训后好就业吗
  2. 华人首位!许晨阳获2021年美国数学会代数领域最高奖
  3. 重裝系統,磁盤消失解決方法
  4. React学习:生命周期、过滤器、event、axios-学习笔记
  5. 使用HTML5开发离线应用 - cache manifest
  6. 线程池和线程详细教程
  7. 如何用c语言程序写一段英文字母,菜鸟求助,写一个随机输出26个英文字母的程序...
  8. PyQt5多线程的执行和停止
  9. javabirdge php_PHP-Java-Bridge使用笔记
  10. Android VideoVIew 视频全屏拉伸
  11. android 抓取解析systrace
  12. poi ppt html,POI之PPT如何添加表格简单实例
  13. ios个人小游戏开发者如何做到年收益过百万?
  14. 集合下现在淘宝小号所遇见的常见问题
  15. Nexus5刷Firefox OS 2.0
  16. 如何用PPT编制方案 — 1. PPT的总体规划
  17. 痴呆患者血脑屏障(Blood-Brain Barrier, BBB)功能测量
  18. 客户关系维护注意事项 如何维护客户关系
  19. JSP详细DAO设计模式及应用(!)
  20. C++单个配置与多个配置

热门文章

  1. minio-operator部署minio服务,并用Java客户端访问minio
  2. Windows下智能卡开发
  3. react中dispatch_react-redux中dispatch是异步?
  4. 火车头采集html文档没内容,火车头采集器:编辑任务中常见问题
  5. ERROR: The process abc.exe with PID 6588 could not be terminated.
  6. dest在C语言什么作用,目前最全面的dest答疑问题及相关回答
  7. 【复盘】互联网老辛七月复盘-年中flag
  8. unity隐藏鼠标光标的2种方法
  9. 这篇python正则表达式颠覆你的人生观,详细到让你怀疑人生!
  10. 实验吧 ctf题目 天下武功唯快不破