根据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 ...
随机推荐
- IIS 网站 HTTP 转 HTTPS
最近需要做 http 链接转成 https 链接,所以就去弄了,现在记录下: 1.准备SSL证书 最开始的时候用的是腾讯云的免费证书,有效期1年,但只能绑定一个二级域名.测试成功后,就去阿里云购买了证 ...
- IDEA创建多个模块MavenSpringBoot项目
最近在学习springboot,先从创建项目开始,一般项目都是一个项目下会有多个模块,这里先创建一个最简单的实例,一个项目下有一个springboot模块项目提供web服务,引用另一个java项目(相 ...
- Linear SVM和LR的区别和联系
首先,SVM和LR(Logistic Regression)都是分类算法.SVM通常有4个核函数,其中一个是线性核,当使用线性核时,SVM就是Linear SVM,其实就是一个线性分类器,而LR也是一 ...
- gdb fabs错误输出
https://sourceware.org/gdb/wiki/FAQ GDB doesn't know the return type nor the type of the arguments f ...
- Azure CentOS挂载磁盘
查看新增挂载磁盘 ls -l /dev/sd*  sudo fdisk /dev/sdc 依次输入:n,p,1,w  3.格式化分区 sudo mkfs -t ext4 /dev/sdc1  4 ...
- Vue自用axios封装
[本文出自天外归云的博客园] 这是我的Vue项目中的request.js文件,请求报错了看console就会有具体请求信息,方便调试.分享一下. 其中用到了axios和element-ui的组件,ax ...
- C# 耗时统计
Stopwatch sw = new Stopwatch(); sw.Restart(); var a = redis.ListRange<UserModel>(key); sw.Stop ...
- Linux下printf、fprintf、sprintf的区别
(1)fprintf() int fprintf( FILE *stream, const char *format, ... ); 用于文件操作,根据指定的format(格式)发送信息(参数)到 ...
- Swagger UI 与SpringMVC的整合 II
pom.xml <!-- swagger开始 --> <dependency> <groupId>io.springfox</groupId> < ...
- laravel框架生產vender文件夹
方法一.修改拓展 去php.ini中查看下面三个扩展项是否开启 extension=php_fileinfo.dll extension=php_mbstring.dll extension=php_ ...