码迷,mamicode.com
首页 > 编程语言 > 详细

替换WORD/EXCEL模板文档中的内容并下载java

时间:2016-02-22 11:45:20      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:


import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.FieldsDocumentPart;
import org.apache.poi.hwpf.usermodel.Field;
import org.apache.poi.hwpf.usermodel.Fields;
import org.apache.poi.hwpf.usermodel.Range;
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.ss.usermodel.WorkbookFactory;


/*
* 案件模板下载 * * */ public static void generateFile(HttpServletRequest request,HttpServletResponse response){ Map<String, String> map = FastMap.newInstance(); String calendarYear = request.getParameter("calendarYear"); map.put("201X", calendarYear); //需要替换的内容 key为被替换者 String engFileTemplateName = "C:\Users\NUH-25\Desktop\ENG20160131.xlsx"; //被替换模板路径 String fileType = engFileTemplateName.substring(engFileTemplateName.lastIndexOf(".") + 1, engFileTemplateName.length()); if("xls".equals(fileType)||"xlsx".equals(fileType)) readwriteExcel(request,response,map,engFileTemplateName); else if("doc".equals(fileType)||"docx".equals(fileType)) readwriteWord(request,response,map,engFileTemplateName); } public static void readwriteWord(HttpServletRequest request,HttpServletResponse response,Map<String, String> map,String engFileTemplateName){ FileInputStream in; HWPFDocument hdt = null; try { in = new FileInputStream(new File(engFileTemplateName)); hdt = new HWPFDocument(in); } catch (Exception e1) { e1.printStackTrace(); } Fields fields = hdt.getFields(); Iterator<Field> it = fields.getFields(FieldsDocumentPart.MAIN).iterator(); /* while(it.hasNext()){ System.out.println(it.next().getType()); } */ //替换读取到的word模板内容的指定字段 Range range = hdt.getRange(); for (Map.Entry<String,String> entry:map.entrySet()) { range.replaceText(entry.getKey(),entry.getValue()); } //输出word内容文件流,提供下载 response.reset(); response.setContentType("application/x-msdownload"); response.setCharacterEncoding("gbk"); String fileType = engFileTemplateName.substring(engFileTemplateName.lastIndexOf(".") + 1, engFileTemplateName.length()); String fileName = ""+System.currentTimeMillis()+"."+fileType; response.addHeader("Content-Disposition", "attachment; filename="+fileName); ByteArrayOutputStream ostream = new ByteArrayOutputStream(); OutputStream servletOS = null; try { servletOS = response.getOutputStream(); hdt.write(ostream); servletOS.write(ostream.toByteArray()); servletOS.flush(); servletOS.close(); } catch (Exception e) { e.printStackTrace(); } } /** * * 案件附件模板excel修改下载 * * @param request * * @param response * * @throws IOException */ public static void readwriteExcel(HttpServletRequest request,HttpServletResponse response,Map<String, String> map,String engFileTemplateName){ try { String fileType = engFileTemplateName.substring(engFileTemplateName.lastIndexOf(".") + 1, engFileTemplateName.length()); FileInputStream in = new FileInputStream(new File(engFileTemplateName));; InputStream inputStream = new BufferedInputStream(in); Workbook workbook = null; if (fileType.equals("xls")) { workbook = new HSSFWorkbook(inputStream); } else if (fileType.equals("xlsx")) { //workbook = new XSSFWorkbook(inputStream); workbook = WorkbookFactory.create(inputStream); } else { System.out.println("您输入的excel格式不正确"); } Iterator<Sheet> iterator = workbook.sheetIterator(); while(iterator.hasNext()){ Sheet sheet = iterator.next(); for(Row row:sheet){ for(Cell cell:row){ if(1==cell.getCellType()){ String cellString = cell.getStringCellValue(); for (Map.Entry<String,String> entry:map.entrySet()) { if(cellString.contains(entry.getKey())){ String newString = cellString.replace(entry.getKey(), entry.getValue()); cell.setCellValue(newString); } } } } } } //输出excel内容文件流,提供下载 response.reset(); response.setContentType("application/x-msdownload"); response.setCharacterEncoding("gbk"); String fileName = ""+System.currentTimeMillis()+"."+fileType; response.addHeader("Content-Disposition", "attachment; filename="+fileName); ByteArrayOutputStream ostream = new ByteArrayOutputStream(); OutputStream servletOS = null; try { servletOS = response.getOutputStream(); workbook.write(ostream); servletOS.write(ostream.toByteArray()); servletOS.flush(); servletOS.close(); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); Debug.logError(e, module); } }

 

替换WORD/EXCEL模板文档中的内容并下载java

标签:

原文地址:http://www.cnblogs.com/transcend/p/5206372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!