标签:
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); } }
标签:
原文地址:http://www.cnblogs.com/transcend/p/5206372.html