根据Excel模板,填写报表,并下载到web浏览器端
- package com.neusoft.nda.basic.recordmanager.viewelec.servlet;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import javax.servlet.ServletException;
- import javax.servlet.ServletOutputStream;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.io.FileUtils;
- import org.apache.poi.hssf.usermodel.HSSFCellStyle;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- public class ExportExcelForArchiveUseServlet extends HttpServlet{
- /**
- *
- */
- private static final long serialVersionUID = -3695494222618327879L;
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
//获取模板路径- String sourcePath=getServletContext().getRealPath("/WEB-INF/")+"/conf/arrangement/cataloguins/exceltemplate/exportArchiveUse.xls";
- writeExcel(req, resp, sourcePath);
- }
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- doPost(req, resp);
- }
- private static void writeExcel(HttpServletRequest req, HttpServletResponse resp,String finalXlsxPath) {
- ServletOutputStream out = null;
- // 读取Excel文档
- File finalXlsxFile = createNewFile(finalXlsxPath);//复制模板,
- Workbook workBook = null;
- try {
- workBook = getWorkbok(finalXlsxFile);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- //收集需要填入报表的数据
- // sheet 对应一个工作页 插入数据开始 ------
- Sheet sheet = workBook.getSheetAt(0);
- workBook.createCellStyle().setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
- workBook.createCellStyle().setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中
//操作单元格,把数据写入即可- Row row6 = sheet.getRow(5);// 获取到第6行
- Cell cell5 = row6.getCell(4);// 6行 5列
- Row row7 = sheet.getRow(6);// 获取到第7行
- Cell cell75 = row7.getCell(4);// 7行 5列
- //插入数据结束
- try {
- //设置下载的Excel的文件名称
- resp.setHeader("Content-Disposition", "attachment;filename=" + new String(finalXlsxFile.getName().getBytes("GBK"),"ISO8859_1"));
- } catch (UnsupportedEncodingException e2) {
- e2.printStackTrace();
- }
- try {
- out=resp.getOutputStream();
- // out = new FileOutputStream(finalXlsxFile);
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- workBook.write(out);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- try {
- if (out != null) {
- out.flush();
- out.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 判断excel格式版本
- *
- * @param file
- * @return
- * @throws IOException
- */
- private static Workbook getWorkbok(File file) throws IOException {
- Workbook wb = null;
- FileInputStream in = new FileInputStream(file);
- if (file.getName().endsWith(".xls")) { // Excel 2003
- wb = new HSSFWorkbook(in);
- } else if (file.getName().endsWith("xlsx")) { // Excel 2007/2010
- wb = new XSSFWorkbook(in);
- }
- return wb;
- }
- private static File createNewFile(String path) {
- // 读取模板,并赋值到新文件************************************************************
- // 文件模板路径
- File file = new File(path);
- if (!file.exists()) {
- System.out.println("原模板文件不存在");
- }
- // 保存文件的路径
- String realPath = file.getParent();
- // 新的文件名
- String newFileName = "报表-" + System.currentTimeMillis()
- + ".xls";
- // 判断路径是否存在
- File dir = new File(realPath);
- if (!dir.exists()) {
- dir.mkdirs();
- }
- // 写入到新的excel
- File newFile = new File(realPath, newFileName);
- try {
- newFile.createNewFile();
- // 复制模板到新文件
- FileUtils.copyFile(file, newFile);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return newFile;
- }
- }
根据Excel模板,填写报表,并下载到web浏览器端的更多相关文章
- NPOI之使用EXCEL模板创建报表
因为项目中要用到服务器端创建EXCEL模板 无法直接调用EXCEL 查了下发现NPOI很方便很简单就实现了 其中走了点弯路 第一次弄的时候发现输出的值是文本不是数字型无法直接计算公式 然后又发现打开报 ...
- jxls实现基于excel模板的报表
此文章是基于 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台 一. jar包介绍 1. commons-collections-3.2.jar 2. commo ...
- 报表中的Excel操作之Aspose.Cells(Excel模板)
原文:报表中的Excel操作之Aspose.Cells(Excel模板) 本篇中将简单记录下Aspose.Cells这个强大的Excel操作组件.这个组件的强大之处,就不多说,对于我们的报表总是会有导 ...
- c#操作Excel模板,替换命名单元格或关键字形成报表
c#操作Excel模板,替换命名单元格或关键字形成报表 http://blog.sina.com.cn/s/blog_45eaa01a0102vqma.html一 建立Excel 模板文件 templ ...
- java 下载Excel模板
前端: JSP: <div id="insertBtn" class="MyCssBtn leftBtn" onclick="download( ...
- java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)
使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...
- java实现赋值excel模板,并在新文件中写入数据,并且下载
/** * 生成excel并下载 */ public void exportExcel(){ File newFile = createNewFile(); //File newFile = new ...
- python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图
python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 # coding=utf-8 from openpyxl import load_workbook ...
- PPT、Word、Excel模板免费下载
本篇文章可能只有寥寥数字,但他的作用可能很大,可能帮助到很多朋友.本人喜欢搜集一些资源,也爱免费分享,因为好东西我藏不住,总感觉分享出来更快乐. 网址:https://www.bangongziyua ...
随机推荐
- 关于c++ template的branching和Recursion的一段很好的描述
来自: <Learning Boost C++ Libraries> 第290页
- A Sample Linker Script
from:http://www.hertaville.com/a-sample-linker-script.html A sample script file that will work with ...
- spring拦截器中使用spring的自动注入
需要在spring的拦截器中使用自定义的服务,这要就设计到将服务注入到拦截器中.网上看的情况有两种: 1. @Configuration public class OptPermissionHandl ...
- Java之Builder模式(并用OC实现了这种模式)
本人在学习Java,直接先学习Netty框架,因为Netty框架是业界最流行的NIO框架之一,在学习的过程中,了解到Netty服务端启动需要先创建服务器启动辅助类ServerBootstrap,它提供 ...
- 批量生产Xcode group 并映射 实体目录
xaddgroup A Ruby gem . Batch Add Group To Xcodeproj , map Real Dir. Install $ gem install xaddgroup ...
- Mybatis 级联查询时只查出了一条数据
造成这个问题的原因是: 主表和明细表的id字段名相同造成的. 问题的关键在于resultMap中如果不定义类似主键之类的能够区分每一条结果集的字段的话,会引起后面一条数据覆盖前面一条数据的现象.
- Postman 接口测试神器
Postman 接口测试神器 Postman 是一个接口测试和 http 请求的神器,非常好用. 官方 github 地址: https://github.com/postmanlabs Postma ...
- linux和windows强制用户第一次登陆修改密码
linux: passwd -e root windows: 计算机右键->管理->本地用户和组->用户->administrator->下一次登陆修改密码 如果密码复杂 ...
- SAP Tax Service可以取代TAXBRA / RVABRA吗?(翻译) 跨国贸易云税务解决方案
这篇文章的内容基于Fausto Motter 的一篇文章,他在SAP社区用葡萄牙语写了一篇文章,讨论如何用云解决方案取代巴西税收计算.我翻译了他的部分文章,添加并修改了一些内容,目标是帮助全球的SA ...
- 必读:Spark与kafka010整合
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/rlnLo2pNEfx9c/article/details/79648890 SparkStreami ...