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;
} }

最新文章

  1. 提高生产性工具(四) - XML数据库的尝试
  2. DES根据键值加密解密
  3. Session变量不能传送到下一页.解决: session.use_trans_sid = 1
  4. 详解LUA开发工具及其环境配置
  5. POJ2299Ultra-QuickSort (线段树和归并排序的解法)
  6. CXF客户端异常
  7. python(2)-字符串(2)
  8. 1172 Hankson 的趣味题
  9. FP-Tree算法的实现
  10. Android camera采集视频 X264编码
  11. JS七种加密解密方法
  12. 苹果手机button有色差
  13. Kettle的应用——对mysql数据进行表输入与导出
  14. Python数据类型一:数字与运算符
  15. VisualVM远程监控Java
  16. 11、OpenCV实现图像的灰度变换
  17. pytest六:parametrize-参数化
  18. hive top n
  19. idea中pom文件需要添加的依赖
  20. Android 自动化测试

热门文章

  1. grid - 通过网格线名称设置网格项目位置
  2. SpringBoot项目获取ApplicationContext来GetBean的方法
  3. SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本
  4. Java 基础【19】代理
  5. 【PMP】商业论证与效益管理文件
  6. java maven通过SMTP发送QQ邮件的完全步骤
  7. haproxy+keepalived(涵盖了lvs,nginx.haproxy比较)
  8. Python(可变/不可变类型,list,tuple,dict,set)
  9. Linux Shell函数返回值
  10. 解决Warning Couldn't flush user prefs: java.util.prefs.BackingStoreException: Couldn't get file lock.