1. public class ExcelUtils {
  2.  
  3. private static final String INSPECTIONRECORD_SURFACE_TEMPLET_PATH = "download\\template\\materialList.xlsx";
  4. private static XSSFCellStyle cellstyle = null;
  5.  
  6. public static void exportBom(HttpServletRequest request, HttpServletResponse response, Map map) throws IOException {
  7. //模板的路径,这个在自己的项目中很容易弄错,相对位置一定要写对啊
  8. String psth = request.getRealPath("/") + INSPECTIONRECORD_SURFACE_TEMPLET_PATH;
  9. Workbook webBook = readExcel(psth);
  10. Sheet sheet = webBook.getSheetAt(0);
  11. //调用样式方法
  12. cellstyle = createCellStyle(webBook);
  13. sheet.setColumnWidth(0, 3766);//列宽
  14. sheet.setColumnWidth(1, 4766);//列宽
  15. //开始操作模板,找到某行某列(某个cell),需要注意的是这里有个坑,行和列的计数都是从0开始的
  16. //一次数据插入的位置不对,别灰心,多试几次就好啦,你要是能看懂我下面的代码,数据插在了什么位置,你就明白了
  17. //打印时间
  18. int rows = 0;
  19. Row row = sheet.getRow(rows);
  20. //调用方法,赋值并给定样式(写样式的时候这个工具类只能用XSSF而不能用HSSF,不然样式会没响应)
  21. setCellStyleWithValue(row.createCell(0),(String) map.get("title"));
  22.  
  23. rows = 2;
  24. row = sheet.getRow(rows);
  25. row.createCell(1).setCellValue((String) map.get("date"));
  26. // setCellStyleWithValue(row.createCell(1),(String) map.get("date"));
  27.  
  28. //负责人
  29. rows = 3;
  30. row = sheet.getRow(rows);
  31. row.createCell(1).setCellValue((String) map.get("leader"));
  32. // setCellStyleWithValue(row.createCell(1),(String) map.get("leader"));
  33.  
  34. //审核人
  35. rows = 4;
  36. row = sheet.getRow(rows);
  37. row.createCell(1).setCellValue((String) map.get("Auditleader"));
  38. // setCellStyleWithValue(row.createCell(1),(String) map.get("Auditleader"));
  39.  
  40. //在调用模板的时候,数据的插入不能直接应用模板行,不然数据会覆盖掉哪行
  41. rows = 7;
  42. row = sheet.createRow(rows);
  43. row.createCell(0).setCellValue("编号");
  44. row.createCell(1).setCellValue("名称");
  45. row.createCell(2).setCellValue("规格");
  46. row.createCell(3).setCellValue("数量");
  47. row.createCell(4).setCellValue("益值");
  48. row.setRowStyle(cellstyle);
  49.  
  50. List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("resultList");
  51. for (int i = 0; i < list.size(); i++) {
  52. row = sheet.createRow(rows++);
  53. /* row.createCell(0).setCellValue(StringUtils.objToStr(list.get(i).get("物料编号")));
  54. row.createCell(1).setCellValue(StringUtils.objToStr(list.get(i).get("物料名称")));
  55. row.createCell(2).setCellValue(StringUtils.objToStr(list.get(i).get("物料规格")));
  56. row.createCell(3).setCellValue(Double.parseDouble(StringUtils.objToStr(list.get(i).get("物料消耗数量"))));
  57. row.createCell(4).setCellValue(Float.parseFloat(StringUtils.objToStr(list.get(i).get("物料消耗损益值"))));*/
  58.  
  59. setCellStyleWithValue(row.createCell(0),StringUtils.objToStr(list.get(i).get("物料编号")));
  60. setCellStyleWithValue(row.createCell(1),StringUtils.objToStr(list.get(i).get("物料名称")));
  61. setCellStyleWithValue(row.createCell(2),StringUtils.objToStr(list.get(i).get("物料规格")));
  62. setCellStyleWithValue(row.createCell(3),Double.parseDouble(StringUtils.objToStr(list.get(i).get("物料消耗数量"))));
  63. setCellStyleWithValue(row.createCell(4),Float.parseFloat(StringUtils.objToStr(list.get(i).get("物料消耗损益值"))));
  64.  
  65. }
  66. writeExcel(response, webBook, (String) map.get("title"));
  67. }
  68.  
  69. private static XSSFWorkbook readExcel(String filePath) {
  70. InputStream in = null;
  71. XSSFWorkbook work = null;
  72. try {
  73. in = new FileInputStream(filePath);
  74. work = new XSSFWorkbook(in);
  75. } catch (FileNotFoundException e) {
  76. System.out.println("文件路径错误");
  77. e.printStackTrace();
  78. } catch (IOException e) {
  79. System.out.println("文件输入流错误");
  80. e.printStackTrace();
  81. }
  82. return work;
  83. }
  84.  
  85. private static void writeExcel(HttpServletResponse response, Workbook work, String fileName) throws IOException {
  86. OutputStream out = null;
  87. try {
  88. out = response.getOutputStream();
  89. response.setContentType("application/ms-excel;charset=UTF-8");
  90. response.setHeader("Content-Disposition", "attachment;filename="
  91. .concat(String.valueOf(URLEncoder.encode(fileName + ".xls", "UTF-8"))));
  92. work.write(out);
  93. } catch (IOException e) {
  94. System.out.println("输出流错误");
  95. e.printStackTrace();
  96. } finally {
  97. out.close();
  98. }
  99. }
  100.  
  101. private static Cell setCellStyleWithStyleAndValue(CellStyle style, Cell cell, String value) {
  102. cell.setCellStyle(style);
  103. cell.setCellValue(value);
  104. return cell;
  105. }
  106.  
  107. private static Cell setCellStyleWithStyleAndValue(CellStyle style, Cell cell, Double value) {
  108. cell.setCellStyle(style);
  109. cell.setCellValue(value);
  110. return cell;
  111. }
  112.  
  113. private static Cell setCellStyleWithValue(Cell cell, String value) {
  114. cell.setCellStyle(cellstyle);
  115. cell.setCellValue(value);
  116. return cell;
  117. }
  118.  
  119. private static Cell setCellStyleWithStyleAndValue(CellStyle style, Cell cell, RichTextString value) {
  120. cell.setCellStyle(style);
  121. cell.setCellValue(value);
  122. return cell;
  123. }
  124.  
  125. private static Cell setCellStyleWithValue(Cell cell, int value) {
  126. cell.setCellStyle(cellstyle);
  127. cell.setCellValue(value);
  128. return cell;
  129. }
  130.  
  131. private static Cell setCellStyleWithValue(Cell cell, double value) {
  132. cell.setCellStyle(cellstyle);
  133. cell.setCellValue(value);
  134. return cell;
  135. }
  136.  
  137. private static XSSFCellStyle createCellStyle(Workbook wb) {
  138. cellstyle = (XSSFCellStyle) wb.createCellStyle();
  139. cellstyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
  140. // cellstyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
  141. // cellstyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
  142. // cellstyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
  143. // cellstyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
  144. cellstyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
  145. return cellstyle;
  146. }
  147. }
  148.  
  149. 我自己的空模板,有数据的图就不贴出来了!
  150.  

java后台poi根据模板导出excel的更多相关文章

  1. apache poi根据模板导出excel

    需要预先新建编辑好一个excel文件,设置好样式. 编辑好输出的数据,根据excel坐标一一对应. 支持列表数据输出,列表中列合并. 代码如下: package com.icourt.util; im ...

  2. Java使用POI实现数据导出excel报表

    Java使用POI实现数据导出excel报表 在上篇文章中,我们简单介绍了java读取word,excel和pdf文档内容 ,但在实际开发中,我们用到最多的是把数据库中数据导出excel报表形式.不仅 ...

  3. POI通过模板导出EXCEL文件

    一般的EXCEL导出使用POI先创建一个HSSFWorkbook,然后通过不断创建HSSFRow,HSSFCell后设置单元格内容便可以完成导出. 这次在项目中需要用到模板,导出的内容包括(1.模板中 ...

  4. Java利用POI实现导入导出Excel表格示例代码

    转自:https://www.jb51.net/article/95526.htm 介绍 Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组件组成,其 ...

  5. JAVA 通过POI 模版,导出excel

    如有不足,欢迎指正,谢谢 ! 1.Maven引入  POI jar包.模版和结果文件.rar下载 <dependency> <groupId>org.apache.poi< ...

  6. java使用freemarker作为模板导出Excel表格

    1:首先新建一个excel表格自己弄好格式如下图 2:把excel 表格另存为xml格式文件如下图 3:这个时候的文件就是xml 格式的文件了,在myeclipse里面项目工程里面新建一个文件后缀为. ...

  7. Java无模板导出Excel,Apache-POI插件实现

    开发环境 jdk 1.8 Maven 3.6 Tomcat 8.5 SpringBoot 2.1.4.RELEASE Apache-POI 3.6 Idea 注意: 我是在现有的基于SpringBoo ...

  8. Java中使用poi导入、导出Excel

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  9. ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据

    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案   ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...

随机推荐

  1. Intellij IDEA 14 自动生成 serialVersionUID

    1.  Preferences > Editor > Inspections > Java > Serialization issues > Serializable c ...

  2. pat1025. PAT Ranking (25)

    1025. PAT Ranking (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...

  3. SQL命令行操作

    命令行操作(mysql.exe)    0.登录  :       mysql -u root -p    1.显示数据库列表:    show databases;     2.选择数据库:     ...

  4. cmd/bat 判断日期,并删除文件

    代码来自互联网. ---------------------------------------- @echo off set a=2018-07-9 set ndate=%date:~,10%set ...

  5. 斗鱼扩展--notifications提示(十二)

    来说下 桌面通知 Notification,HTML5支持 Web Notifications 的实例,但是要经过用户允许,  chrome://settings/content/notificati ...

  6. java webServer(一)

    java webServer实现 浏览器和服务器使用的是http协议,http协议使用的是tcp 这里主要在服务器端监听端口号 实现功能 通过浏览器向服务器发送http请求:http://localh ...

  7. HTTP的GET和POST格式解析

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yc0188/archive/2009/10/29/4741871.aspx HTTP报文是面向文本的,报文中的每一个字 ...

  8. 粗看ES6之函数

    标签: es6 javascript 箭头函数 ES6为了书写方便引入了函数的全新简写方式-箭头函数 <!DOCTYPE html> <html> <head> & ...

  9. The fourteenth day

    A man is not old as long as he is seeking something. A man is not old until regrets take the place o ...

  10. 零基础逆向工程34_Win32_08_线程控制_CONTEXT结构

    线程控制 实验 挂起线程 ::SuspendThread(hThread); 恢复线程 ::ResumeThread(hThread); 终止线程 (这里讲了同步调用与异步调用) 方式一: 此方法结束 ...