正常导出excel表格使用的poi,但是导出复杂的excel有点困难,但是可以使用freemaker模板来导出复杂的excel。

1、都是先生成一个Excel表格的模板,最好是增加一行数据。具体看图里面的步骤。

2、项目整体结构

3、下面就直接看代码

  1. public class Data {
  2. //代码复制之后直接就可以运行了
  3. public static void main(String[] args) {
  4. demo();
  5. }
  6.  
  7. public static void demo() {
  8. // 项目下的template路径
  9. String path = new File("").getAbsolutePath() + "\\template";
  10. Map<String, Object> map = new HashMap<String, Object>();
  11. // 模板所在的路径
  12. map.put("tempFoldPath", path);
  13. // 生成的路径
  14. map.put("file", path + "/采购订单.xls");
  15. // 模板名称
  16. map.put("tampPath", "采购订单.ftl");
  17. // 最后生成的表格的名称
  18. map.put("excelName", "采购订单-" + "Demo" + ".xls");
  19. // 封装数据
  20. Map<String, Object> exlParam = new HashMap<>();
  21. exlParam.put("findList", new Data().list());
  22. // 调用方法,返回浏览器访问的地址
  23. String downloadUrl = ExportExcelUtil.exportExcel(map, exlParam);
  24. }
  25.  
  26. // 自己造假数据,正常来说都是从数据库查询出来拼装数据的
  27. public List<Purbill> list() {
  28. List<Purbill> purbillList = new ArrayList<>();
  29. purbillList.add(new Purbill("1", "2", "名称", "采购名称", "规格参数", "参数指标", "场地", "10吨", 10, 20.2, 220.2, "品牌"));
  30. return purbillList;
  31. }
  32.  
  33. }
  34.  
  35. class Purbill {
  36. private String bidId;
  37. private String billno;
  38. private String categoryName;
  39. private String purname;
  40. private String specparams;
  41. private String paramnorm;
  42. private String productAddress;
  43. private String unit;
  44. private Integer nums;
  45. private Double price;
  46. private Double totalprice;
  47. private String brand;
  48.  
  49. public Purbill(String bidId, String billno, String categoryName, String purname, String specparams,
  50. String paramnorm, String productAddress, String unit, Integer nums, Double price, Double totalprice,
  51. String brand) {
  52. super();
  53. this.bidId = bidId;
  54. this.billno = billno;
  55. this.categoryName = categoryName;
  56. this.purname = purname;
  57. this.specparams = specparams;
  58. this.paramnorm = paramnorm;
  59. this.productAddress = productAddress;
  60. this.unit = unit;
  61. this.nums = nums;
  62. this.price = price;
  63. this.totalprice = totalprice;
  64. this.brand = brand;
  65. }
  66.  
  67. public String getBidId() {
  68. return bidId;
  69. }
  70.  
  71. public void setBidId(String bidId) {
  72. this.bidId = bidId;
  73. }
  74.  
  75. public String getBillno() {
  76. return billno;
  77. }
  78.  
  79. public void setBillno(String billno) {
  80. this.billno = billno;
  81. }
  82.  
  83. public String getCategoryName() {
  84. return categoryName;
  85. }
  86.  
  87. public void setCategoryName(String categoryName) {
  88. this.categoryName = categoryName;
  89. }
  90.  
  91. public String getPurname() {
  92. return purname;
  93. }
  94.  
  95. public void setPurname(String purname) {
  96. this.purname = purname;
  97. }
  98.  
  99. public String getSpecparams() {
  100. return specparams;
  101. }
  102.  
  103. public void setSpecparams(String specparams) {
  104. this.specparams = specparams;
  105. }
  106.  
  107. public String getParamnorm() {
  108. return paramnorm;
  109. }
  110.  
  111. public void setParamnorm(String paramnorm) {
  112. this.paramnorm = paramnorm;
  113. }
  114.  
  115. public String getProductAddress() {
  116. return productAddress;
  117. }
  118.  
  119. public void setProductAddress(String productAddress) {
  120. this.productAddress = productAddress;
  121. }
  122.  
  123. public String getUnit() {
  124. return unit;
  125. }
  126.  
  127. public void setUnit(String unit) {
  128. this.unit = unit;
  129. }
  130.  
  131. public Integer getNums() {
  132. return nums;
  133. }
  134.  
  135. public void setNums(Integer nums) {
  136. this.nums = nums;
  137. }
  138.  
  139. public Double getPrice() {
  140. return price;
  141. }
  142.  
  143. public void setPrice(Double price) {
  144. this.price = price;
  145. }
  146.  
  147. public Double getTotalprice() {
  148. return totalprice;
  149. }
  150.  
  151. public void setTotalprice(Double totalprice) {
  152. this.totalprice = totalprice;
  153. }
  154.  
  155. public String getBrand() {
  156. return brand;
  157. }
  158.  
  159. public void setBrand(String brand) {
  160. this.brand = brand;
  161. }
  162.  
  163. }

主要是两个map,一个map是封装模板的位置和生成表格的位置,第二个map是封装的数据。正常来说导出表格之后都是返回url请求的地址,这样在真实项目中根据地址就可以下载出来了。

4、下面是一个excel导出的一个工具类

  1. public class ExportExcelUtil {
  2.  
  3. public static String exportExcel(Map<String, Object> map, Map<String, Object> exlParam) {
  4. Template dateTmp = null;
  5. Writer fw = null;
  6. InputStream in = null;
  7. OutputStream out = null;
  8. try {
  9. // 此处需要给你个版本信息,Configuration cfg = new Configuration();这个方法已经过时了
  10. Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
  11. String tempFoldPath = (String) map.get("tempFoldPath"); // 模板所在的路径
  12. String file = (String) map.get("file");// 生成表格模板的路径
  13. String tampPath = (String) map.get("tampPath");// 模板名称
  14. String excelName = (String) map.get("excelName");// 最后生成表格的名称
  15.  
  16. // **********初始化参数**********
  17. File tempFoldFile = new File(tempFoldPath);
  18. if (!tempFoldFile.exists()) {
  19. tempFoldFile.mkdirs();
  20. }
  21. cfg.setDirectoryForTemplateLoading(tempFoldFile);
  22. cfg.setDefaultEncoding("UTF-8");
  23. cfg.setTemplateUpdateDelay(0);
  24. cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
  25. // **********获取freemaker模板**********
  26. dateTmp = cfg.getTemplate(tampPath);
  27.  
  28. // **********将数据写入freemaker模板**********
  29. fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(file)), "UTF-8"));
  30. dateTmp.process(exlParam, fw);
  31.  
  32. // **********从freemaker模板读出数据写到Excel表格并生成出来**********
  33. String fileDir = "excel";
  34. // 文件保存目录 项目目录下面
  35. String filePath = new File("").getAbsolutePath();
  36. // 生成保存文件路径
  37. String createPath = filePath + "/" + fileDir + "/";
  38. // 构建源文件
  39. File files = new File(file);
  40. // 文件夹不存在就创建
  41. createFolder(createPath);
  42. // 删除原来的文件
  43. deleteFile(createPath + excelName);
  44. // 构建目标文件
  45. File fileCopy = new File(createPath + excelName);
  46. // 目标文件不存在就创建
  47. if (!(fileCopy.exists())) {
  48. fileCopy.createNewFile();
  49. }
  50. // 源文件创建输入流
  51. in = new FileInputStream(files);
  52. // 目标文件创建输出流
  53. out = new FileOutputStream(fileCopy, true);
  54. // 创建字节数组
  55. byte[] temp = new byte[1024];
  56. int length = 0;
  57. // 源文件读取一部分内容
  58. while ((length = in.read(temp)) != -1) {
  59. // 目标文件写入一部分内容
  60. out.write(temp, 0, length);
  61. }
  62. // 资源服务器访问目录 这边需要配置tomcat的虚拟路径,就可以直接在url上面下载表格了
  63. String serverPath = "resourceServer";
  64. String savePath = "/" + serverPath + "/" + fileDir + "/" + excelName;
  65. // 服务器图片访问目录
  66. return savePath;
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. return null;
  70. } finally {
  71. try {
  72. fw.close();
  73. // 关闭文件输入输出流
  74. in.close();
  75. out.close();
  76. } catch (IOException e) {
  77. // TODO Auto-generated catch block
  78. e.printStackTrace();
  79. }
  80.  
  81. }
  82.  
  83. }
  84.  
  85. // 创建文件
  86. public static boolean createFolder(String path) {
  87. File file = new File(path);
  88. if (!file.exists()) {
  89. return file.mkdir();
  90. } else {
  91. return true;
  92. }
  93. }
  94.  
  95. public static boolean deleteFile(String filePath) {// 删除单个文件
  96. boolean flag = false;
  97. File file = new File(filePath);
  98. if (file.exists() && file.isFile()) {
  99. file.delete();// 文件删除
  100. flag = true;
  101. }
  102. return true;
  103. }
  104.  
  105. }

这个也不用多说,里面注释基本上已经解释清楚了,

4、因为数据放到freemaker模板里面了所以只需要使用freemaker模板的语法取出数据存放在模板里面就可以了

4、Excel导出打开出现问题的原因

  当office2010之前的版本打开会出现格式不正确的提示,然后点击确定之后还是报格式错误或者可以打开但是没有数据,这种解决方法只需要将ss:ExpandedRowCount这个值设置和行数相等或者设置大一点就不会出现这种问题了。

错误提示

Demo地址:https://files.cnblogs.com/files/yangk1996/FreeMaker.zip

java使用freemarker导出复杂的excel表格的更多相关文章

  1. Java使用freemarker导出word和excel

    www.linxiaosheng.com/post/2013-12-05/40060346181 https://github.com/upyun/java-sdk

  2. Java操作Jxl实现导出数据生成Excel表格数据文件

    实现:前台用的框架是Easyui+Bootstrap结合使用,需要引入相应的Js.Css文件.页面:Jsp.拦截请求:Servlet.逻辑处理:ClassBean.数据库:SQLserver. 注意: ...

  3. java用freemarker导出数据到word(含多图片)

    一.制作word模版 新建word文档,按照需要设置好字体等各种格式:这里为了显得整齐使用了无边框的表格. 将word文档另存为xml文件(注意不是word xml文档,我吃了这家伙的大亏了) 然后用 ...

  4. 在ASP.NET Web Forms中使用页面导出伪xls Excel表格

    将数据导出为Excel表格是比较常见的需求,也有很多组件支持导出真正的Excel表格.由于Excel能打开HTML文件,并支持其中的table元素以及p之类的文本元素的显示,所以把.html扩展名改为 ...

  5. 导出数据到Excel表格

    开发工具与关键技术:Visual Studio 和 ASP.NET.MVC,作者:陈鸿鹏撰写时间:2019年5月25日123下面是我们来学习的导出数据到Excel表格的总结首先在视图层写导出数据的点击 ...

  6. spring boot 使用POI导出数据到Excel表格

    在spring boot 的项目经常碰到将数据导出到Excel表格的需求,而POI技术则对于java操作Excel表格提供了API,POI中对于多种类型的文档都提供了操作的接口,但是其对于Excel表 ...

  7. PHP批量导出数据为excel表格

    之前用插件phoexcel写过批量导入数据,现在用到了批量导出,就记录一下,这次批量导出没用插件,是写出一个表格,直接输出 //$teacherList 是从数据库查出来的二维数组 $execlnam ...

  8. php动态导出数据成Excel表格

    一.封装 Excel 导出类 include/components/ExecExcel.php <?php /*** * @Excel 导入导出类. */ class ExecExcel { / ...

  9. Python导出数据到Excel表格-NotImplementedError: formatting_info=True not yet implemented

    在使用Python写入数据到Excel表格中时出现报错信息记录:“NotImplementedError: formatting_info=True not yet implemented” 报错分析 ...

随机推荐

  1. 大话CNN

    这几年深度学习快速发展,在图像识别.语音识别.物体识别等各种场景上取得了巨大的成功,例如AlphaGo击败世界围棋冠军,iPhone X内置了人脸识别解锁功能等等,很多AI产品在世界上引起了很大的轰动 ...

  2. ubuntu18桌面卡死解决方法

    1 直接 alt+f2 会弹出个输入框 里边输入 小写 r 回车 这样会重启你的gnome-shell 桌面环境 2 ctrl+f3 进入终端 黑白屏环境 top 一下 你会发现 gnome-shel ...

  3. spring4-2-bean配置-7-Spring表达式语言SpEL

  4. yii2.0 报错Cookievalidationkey Must Be Configured With A Secret Key

    'components' => [ 'request' => [ // !!! insert a secret key in the following (if it is empty) ...

  5. Python监控日志程序-乾颐堂

    一个简易的日志监控的脚本,功能如下:1.windows环境2.当匹配日志关键字时会发出声音,匹配的关键字不同,播放的声音不同3.能做到实时响应 注意:是在win环境下哦 直接上代码吧 1 2 3 4 ...

  6. typeof()和instanceof的用法区别

    typeof() typeof() 是一个一元运算,放在一个运算数之前,运算数可以是任意类型.它返回值是一个字符串,该字符串说明运算数的类型.,typeof一般只能返回如下几个结果:number,bo ...

  7. C#6.0特性(快来围观)(转)

    出处:http://www.cnblogs.com/HJL-Blog/p/4457632.html 说明一下,很多博友一进来就认为仅仅是语法糖,C#语法的更新,代表着它的进步,语法糖是为了让我们更好的 ...

  8. 【转载】Mysql中的Btree与Hash索引比较

    转载地址:http://www.jb51.net/article/62533.htm 这篇文章主要介绍了Mysql中的Btree与Hash索引比较,本文起讲解了B-Tree 索引特征.Hash 索引特 ...

  9. 移动开发iOS&Android对比学习--异步处理

    在移动开发里很多时候需要用到异步处理.Android的主线程如果等待超过一定时间的时候直接出现ANR(对不熟悉Android的朋友这里需要解释一下什么叫ANR.ANR就是Application Not ...

  10. APUE(7)---进程环境

    一.main函数 C程序总是从main函数开始执行.main函数的原型是: int main(int argv, char *argv[]); 当内核执行C程序时,在调用main前先调用一个特殊的启动 ...