1. package com.neusoft.nda.basic.recordmanager.viewelec.servlet;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.io.UnsupportedEncodingException;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.ServletOutputStream;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. import org.apache.commons.io.FileUtils;
  16. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  18. import org.apache.poi.ss.usermodel.Cell;
  19. import org.apache.poi.ss.usermodel.Row;
  20. import org.apache.poi.ss.usermodel.Sheet;
  21. import org.apache.poi.ss.usermodel.Workbook;
  22. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  23.  
  24. public class ExportExcelForArchiveUseServlet extends HttpServlet{
  25.  
  26. /**
  27. *
  28. */
  29. private static final long serialVersionUID = -3695494222618327879L;
  30.  
  31. @Override
  32. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  33. throws ServletException, IOException {
    //获取模板路径
  34. String sourcePath=getServletContext().getRealPath("/WEB-INF/")+"/conf/arrangement/cataloguins/exceltemplate/exportArchiveUse.xls";
  35. writeExcel(req, resp, sourcePath);
  36. }
  37. @Override
  38. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  39. throws ServletException, IOException {
  40.  
  41. doPost(req, resp);
  42. }
  43. private static void writeExcel(HttpServletRequest req, HttpServletResponse resp,String finalXlsxPath) {
  44. ServletOutputStream out = null;
  45. // 读取Excel文档
  46. File finalXlsxFile = createNewFile(finalXlsxPath);//复制模板,
  47. Workbook workBook = null;
  48. try {
  49. workBook = getWorkbok(finalXlsxFile);
  50. } catch (IOException e1) {
  51. e1.printStackTrace();
  52. }
  53. //收集需要填入报表的数据
  54. // sheet 对应一个工作页 插入数据开始 ------
  55. Sheet sheet = workBook.getSheetAt(0);
  56. workBook.createCellStyle().setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
  57. workBook.createCellStyle().setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中

  58.     //操作单元格,把数据写入即可
  59. Row row6 = sheet.getRow(5);// 获取到第6行
  60. Cell cell5 = row6.getCell(4);// 6行 5列
  61.  
  62. Row row7 = sheet.getRow(6);// 获取到第7行
  63. Cell cell75 = row7.getCell(4);// 7行 5列
  64. //插入数据结束
  65. try {
  66. //设置下载的Excel的文件名称
  67. resp.setHeader("Content-Disposition", "attachment;filename=" + new String(finalXlsxFile.getName().getBytes("GBK"),"ISO8859_1"));
  68. } catch (UnsupportedEncodingException e2) {
  69. e2.printStackTrace();
  70. }
  71.  
  72. try {
  73. out=resp.getOutputStream();
  74. // out = new FileOutputStream(finalXlsxFile);
  75. } catch (FileNotFoundException e1) {
  76. e1.printStackTrace();
  77. } catch (IOException e) {
  78.  
  79. e.printStackTrace();
  80. }
  81. try {
  82. workBook.write(out);
  83. } catch (IOException e1) {
  84. e1.printStackTrace();
  85. }
  86. try {
  87. if (out != null) {
  88. out.flush();
  89. out.close();
  90. }
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }
  94.  
  95. }
  96.  
  97. /**
  98. * 判断excel格式版本
  99. *
  100. * @param file
  101. * @return
  102. * @throws IOException
  103. */
  104. private static Workbook getWorkbok(File file) throws IOException {
  105. Workbook wb = null;
  106. FileInputStream in = new FileInputStream(file);
  107. if (file.getName().endsWith(".xls")) { // Excel 2003
  108. wb = new HSSFWorkbook(in);
  109. } else if (file.getName().endsWith("xlsx")) { // Excel 2007/2010
  110. wb = new XSSFWorkbook(in);
  111. }
  112. return wb;
  113. }
  114.  
  115. private static File createNewFile(String path) {
  116. // 读取模板,并赋值到新文件************************************************************
  117. // 文件模板路径
  118. File file = new File(path);
  119. if (!file.exists()) {
  120. System.out.println("原模板文件不存在");
  121. }
  122. // 保存文件的路径
  123. String realPath = file.getParent();
  124. // 新的文件名
  125. String newFileName = "报表-" + System.currentTimeMillis()
  126. + ".xls";
  127. // 判断路径是否存在
  128. File dir = new File(realPath);
  129. if (!dir.exists()) {
  130. dir.mkdirs();
  131. }
  132. // 写入到新的excel
  133. File newFile = new File(realPath, newFileName);
  134. try {
  135. newFile.createNewFile();
  136. // 复制模板到新文件
  137. FileUtils.copyFile(file, newFile);
  138. } catch (Exception e) {
  139. e.printStackTrace();
  140. }
  141. return newFile;
  142. }
  143.  
  144. }

根据Excel模板,填写报表,并下载到web浏览器端的更多相关文章

  1. NPOI之使用EXCEL模板创建报表

    因为项目中要用到服务器端创建EXCEL模板 无法直接调用EXCEL 查了下发现NPOI很方便很简单就实现了 其中走了点弯路 第一次弄的时候发现输出的值是文本不是数字型无法直接计算公式 然后又发现打开报 ...

  2. jxls实现基于excel模板的报表

    此文章是基于 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台 一. jar包介绍 1. commons-collections-3.2.jar 2. commo ...

  3. 报表中的Excel操作之Aspose.Cells(Excel模板)

    原文:报表中的Excel操作之Aspose.Cells(Excel模板) 本篇中将简单记录下Aspose.Cells这个强大的Excel操作组件.这个组件的强大之处,就不多说,对于我们的报表总是会有导 ...

  4. c#操作Excel模板,替换命名单元格或关键字形成报表

    c#操作Excel模板,替换命名单元格或关键字形成报表 http://blog.sina.com.cn/s/blog_45eaa01a0102vqma.html一 建立Excel 模板文件 templ ...

  5. java 下载Excel模板

    前端: JSP: <div id="insertBtn" class="MyCssBtn leftBtn" onclick="download( ...

  6. java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)

    使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...

  7. java实现赋值excel模板,并在新文件中写入数据,并且下载

    /** * 生成excel并下载 */ public void exportExcel(){ File newFile = createNewFile(); //File newFile = new ...

  8. python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图

    python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 # coding=utf-8 from openpyxl import load_workbook ...

  9. PPT、Word、Excel模板免费下载

    本篇文章可能只有寥寥数字,但他的作用可能很大,可能帮助到很多朋友.本人喜欢搜集一些资源,也爱免费分享,因为好东西我藏不住,总感觉分享出来更快乐. 网址:https://www.bangongziyua ...

随机推荐

  1. 关于c++ template的branching和Recursion的一段很好的描述

    来自: <Learning Boost C++ Libraries>  第290页

  2. A Sample Linker Script

    from:http://www.hertaville.com/a-sample-linker-script.html A sample script file that will work with ...

  3. spring拦截器中使用spring的自动注入

    需要在spring的拦截器中使用自定义的服务,这要就设计到将服务注入到拦截器中.网上看的情况有两种: 1. @Configuration public class OptPermissionHandl ...

  4. Java之Builder模式(并用OC实现了这种模式)

    本人在学习Java,直接先学习Netty框架,因为Netty框架是业界最流行的NIO框架之一,在学习的过程中,了解到Netty服务端启动需要先创建服务器启动辅助类ServerBootstrap,它提供 ...

  5. 批量生产Xcode group 并映射 实体目录

    xaddgroup A Ruby gem . Batch Add Group To Xcodeproj , map Real Dir. Install $ gem install xaddgroup ...

  6. Mybatis 级联查询时只查出了一条数据

    造成这个问题的原因是: 主表和明细表的id字段名相同造成的. 问题的关键在于resultMap中如果不定义类似主键之类的能够区分每一条结果集的字段的话,会引起后面一条数据覆盖前面一条数据的现象.

  7. Postman 接口测试神器

    Postman 接口测试神器 Postman 是一个接口测试和 http 请求的神器,非常好用. 官方 github 地址: https://github.com/postmanlabs Postma ...

  8. linux和windows强制用户第一次登陆修改密码

    linux: passwd -e root windows: 计算机右键->管理->本地用户和组->用户->administrator->下一次登陆修改密码 如果密码复杂 ...

  9. SAP Tax Service可以取代TAXBRA / RVABRA吗?(翻译) 跨国贸易云税务解决方案

    这篇文章的内容基于Fausto Motter  的一篇文章,他在SAP社区用葡萄牙语写了一篇文章,讨论如何用云解决方案取代巴西税收计算.我翻译了他的部分文章,添加并修改了一些内容,目标是帮助全球的SA ...

  10. 必读:Spark与kafka010整合

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/rlnLo2pNEfx9c/article/details/79648890 SparkStreami ...