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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了替换WORD/EXCEL模板文档中的内容并下载java相关的知识,希望对你有一定的参考价值。


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的主要内容,如果未能解决你的问题,请参考以下文章

freeMarker生成word,excel文档

java操作office和pdf文件java读取word,excel和pdf文档内容

如何将word,excel转换成pdf格式

使用OpenXML操作Office文档

在sql server中存储html内容和其他文档(pdf、word、excel)?

跪求POI根据模板生成word文档的例子。 注意啦,是用书签标记的方式,替换书签中的内容。