| @@ -7,22 +7,39 @@ import org.jeecgframework.poi.word.parse.ParseWord07; | |||||
| /** | /** | ||||
| * Word使用模板导出工具类 | * Word使用模板导出工具类 | ||||
| * | |||||
| * @author JueYue | * @author JueYue | ||||
| * @date 2013-11-16 | * @date 2013-11-16 | ||||
| * @version 1.0 | * @version 1.0 | ||||
| */ | */ | ||||
| public class WordExportUtil { | public class WordExportUtil { | ||||
| /** | /** | ||||
| * 解析Word2007版本 | * 解析Word2007版本 | ||||
| *@Author JueYue | |||||
| *@date 2013-11-16 | |||||
| *@param url 模板地址 | |||||
| *@param map 解析数据源 | |||||
| *@return | |||||
| * | |||||
| * @param url | |||||
| * 模板地址 | |||||
| * @param map | |||||
| * 解析数据源 | |||||
| * @return | |||||
| */ | */ | ||||
| public static XWPFDocument exportWord07(String url,Map<String,Object> map) throws Exception{ | |||||
| public static XWPFDocument exportWord07(String url, Map<String, Object> map) | |||||
| throws Exception { | |||||
| return new ParseWord07().parseWord(url, map); | return new ParseWord07().parseWord(url, map); | ||||
| } | } | ||||
| /** | |||||
| * 解析Word2007版本 | |||||
| * | |||||
| * @param XWPFDocument | |||||
| * 模板 | |||||
| * @param map | |||||
| * 解析数据源 | |||||
| * @return | |||||
| */ | |||||
| public static void exportWord07(XWPFDocument document, | |||||
| Map<String, Object> map) throws Exception { | |||||
| new ParseWord07().parseWord(document, map); | |||||
| } | |||||
| } | } | ||||
| @@ -40,6 +40,16 @@ public class ParseWord07 { | |||||
| parseWordSetValue(doc, map); | parseWordSetValue(doc, map); | ||||
| return doc; | return doc; | ||||
| } | } | ||||
| /** | |||||
| * 按用户模板导出 | |||||
| * @param document | |||||
| * @param map | |||||
| * @throws Exception | |||||
| */ | |||||
| public void parseWord(XWPFDocument document, Map<String, Object> map) throws Exception { | |||||
| parseWordSetValue((JeecgXWPFDocument)document, map); | |||||
| } | |||||
| private void parseWordSetValue(JeecgXWPFDocument doc, | private void parseWordSetValue(JeecgXWPFDocument doc, | ||||
| Map<String, Object> map) throws Exception { | Map<String, Object> map) throws Exception { | ||||
| @@ -276,7 +286,4 @@ public class ParseWord07 { | |||||
| } | } | ||||
| } | } | ||||
| @@ -1,5 +1,5 @@ | |||||
| #Generated by Maven Integration for Eclipse | #Generated by Maven Integration for Eclipse | ||||
| #Thu Jul 24 23:06:08 CST 2014 | |||||
| #Thu Jul 24 23:11:13 CST 2014 | |||||
| version=2.0.1 | version=2.0.1 | ||||
| groupId=org.jeecgframework | groupId=org.jeecgframework | ||||
| m2e.projectName=easypoi-base | m2e.projectName=easypoi-base | ||||
| @@ -5,7 +5,7 @@ package org.jeecgframework.poi.excel.entity.vo; | |||||
| * @Author JueYue on 14-3-8. | * @Author JueYue on 14-3-8. | ||||
| * 静态常量 | * 静态常量 | ||||
| */ | */ | ||||
| public interface NormalPOIConstants extends BasePOIConstants { | |||||
| public interface NormalExcelConstants extends BasePOIConstants { | |||||
| /** | /** | ||||
| * 单Sheet导出 | * 单Sheet导出 | ||||
| */ | */ | ||||
| @@ -4,7 +4,7 @@ package org.jeecgframework.poi.excel.entity.vo; | |||||
| * @author JueYue | * @author JueYue | ||||
| * @date 2014年6月30日 下午9:26:52 | * @date 2014年6月30日 下午9:26:52 | ||||
| */ | */ | ||||
| public interface TemplatePOIConstants extends BasePOIConstants { | |||||
| public interface TemplateExcelConstants extends BasePOIConstants { | |||||
| /** | /** | ||||
| * 模板导出 | * 模板导出 | ||||
| */ | */ | ||||
| @@ -0,0 +1,21 @@ | |||||
| package org.jeecgframework.poi.excel.entity.vo; | |||||
| /** | |||||
| * Word 导出模板常量 | |||||
| * @author JueYue | |||||
| * @date 2014年7月24日 下午11:26:46 | |||||
| */ | |||||
| public interface TemplateWordConstants extends BasePOIConstants { | |||||
| /** | |||||
| * 模板导出 | |||||
| */ | |||||
| public final static String JEECG_TEMPLATE_WORD_VIEW = "jeecgTemplateWordView"; | |||||
| /** | |||||
| * 数据列表 | |||||
| */ | |||||
| public final static String URL = "url"; | |||||
| /** | |||||
| * 模板参数 | |||||
| */ | |||||
| public final static String MAP_DATA = "map"; | |||||
| } | |||||
| @@ -2,7 +2,7 @@ package org.jeecgframework.poi.excel.view; | |||||
| import org.apache.poi.hssf.usermodel.HSSFWorkbook; | import org.apache.poi.hssf.usermodel.HSSFWorkbook; | ||||
| import org.jeecgframework.poi.excel.entity.ExportParams; | import org.jeecgframework.poi.excel.entity.ExportParams; | ||||
| import org.jeecgframework.poi.excel.entity.vo.NormalPOIConstants; | |||||
| import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants; | |||||
| import org.jeecgframework.poi.excel.export.ExcelExportServer; | import org.jeecgframework.poi.excel.export.ExcelExportServer; | ||||
| import org.springframework.web.servlet.view.document.AbstractExcelView; | import org.springframework.web.servlet.view.document.AbstractExcelView; | ||||
| @@ -24,27 +24,27 @@ public class JeecgSingleExcelView extends AbstractExcelView { | |||||
| HSSFWorkbook hssfWorkbook, HttpServletRequest httpServletRequest, | HSSFWorkbook hssfWorkbook, HttpServletRequest httpServletRequest, | ||||
| HttpServletResponse httpServletResponse) throws Exception { | HttpServletResponse httpServletResponse) throws Exception { | ||||
| String codedFileName = "临时文件.xls"; | String codedFileName = "临时文件.xls"; | ||||
| if (model.containsKey(NormalPOIConstants.FILE_NAME)) { | |||||
| codedFileName = (String) model.get(NormalPOIConstants.FILE_NAME) | |||||
| if (model.containsKey(NormalExcelConstants.FILE_NAME)) { | |||||
| codedFileName = (String) model.get(NormalExcelConstants.FILE_NAME) | |||||
| + ".xls"; | + ".xls"; | ||||
| } | } | ||||
| httpServletResponse.setHeader("content-disposition", | httpServletResponse.setHeader("content-disposition", | ||||
| "attachment;filename=" | "attachment;filename=" | ||||
| + new String(codedFileName.getBytes(), "iso8859-1")); | + new String(codedFileName.getBytes(), "iso8859-1")); | ||||
| if (model.containsKey(NormalPOIConstants.MAP_LIST)) { | |||||
| if (model.containsKey(NormalExcelConstants.MAP_LIST)) { | |||||
| List<Map<String, Object>> list = (List<Map<String, Object>>) model | List<Map<String, Object>> list = (List<Map<String, Object>>) model | ||||
| .get(NormalPOIConstants.MAP_LIST); | |||||
| .get(NormalExcelConstants.MAP_LIST); | |||||
| for (Map<String, Object> map : list) { | for (Map<String, Object> map : list) { | ||||
| new ExcelExportServer().createSheet(hssfWorkbook, | new ExcelExportServer().createSheet(hssfWorkbook, | ||||
| (ExportParams) map.get(NormalPOIConstants.PARAMS), | |||||
| (Class<?>) map.get(NormalPOIConstants.CLASS), | |||||
| (Collection<?>) map.get(NormalPOIConstants.DATA_LIST)); | |||||
| (ExportParams) map.get(NormalExcelConstants.PARAMS), | |||||
| (Class<?>) map.get(NormalExcelConstants.CLASS), | |||||
| (Collection<?>) map.get(NormalExcelConstants.DATA_LIST)); | |||||
| } | } | ||||
| } else { | } else { | ||||
| new ExcelExportServer().createSheet(hssfWorkbook, | new ExcelExportServer().createSheet(hssfWorkbook, | ||||
| (ExportParams) model.get(NormalPOIConstants.PARAMS), | |||||
| (Class<?>) model.get(NormalPOIConstants.CLASS), | |||||
| (Collection<?>) model.get(NormalPOIConstants.DATA_LIST)); | |||||
| (ExportParams) model.get(NormalExcelConstants.PARAMS), | |||||
| (Class<?>) model.get(NormalExcelConstants.CLASS), | |||||
| (Collection<?>) model.get(NormalExcelConstants.DATA_LIST)); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -10,8 +10,8 @@ import javax.servlet.http.HttpServletResponse; | |||||
| import org.apache.poi.ss.usermodel.Workbook; | import org.apache.poi.ss.usermodel.Workbook; | ||||
| import org.jeecgframework.poi.excel.ExcelExportUtil; | import org.jeecgframework.poi.excel.ExcelExportUtil; | ||||
| import org.jeecgframework.poi.excel.entity.TemplateExportParams; | import org.jeecgframework.poi.excel.entity.TemplateExportParams; | ||||
| import org.jeecgframework.poi.excel.entity.vo.NormalPOIConstants; | |||||
| import org.jeecgframework.poi.excel.entity.vo.TemplatePOIConstants; | |||||
| import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants; | |||||
| import org.jeecgframework.poi.excel.entity.vo.TemplateExcelConstants; | |||||
| import org.springframework.web.servlet.view.AbstractView; | import org.springframework.web.servlet.view.AbstractView; | ||||
| /** | /** | ||||
| @@ -34,17 +34,17 @@ public class JeecgTemplateExcelView extends AbstractView { | |||||
| HttpServletRequest request, HttpServletResponse response) | HttpServletRequest request, HttpServletResponse response) | ||||
| throws Exception { | throws Exception { | ||||
| String codedFileName = "临时文件.xls"; | String codedFileName = "临时文件.xls"; | ||||
| if (model.containsKey(NormalPOIConstants.FILE_NAME)) { | |||||
| codedFileName = (String) model.get(NormalPOIConstants.FILE_NAME) | |||||
| if (model.containsKey(NormalExcelConstants.FILE_NAME)) { | |||||
| codedFileName = (String) model.get(NormalExcelConstants.FILE_NAME) | |||||
| + ".xls"; | + ".xls"; | ||||
| } | } | ||||
| response.setHeader("content-disposition", "attachment;filename=" | response.setHeader("content-disposition", "attachment;filename=" | ||||
| + new String(codedFileName.getBytes(), "iso8859-1")); | + new String(codedFileName.getBytes(), "iso8859-1")); | ||||
| Workbook workbook = ExcelExportUtil.exportExcel( | Workbook workbook = ExcelExportUtil.exportExcel( | ||||
| (TemplateExportParams)model.get(TemplatePOIConstants.PARAMS), | |||||
| (Class<?>) model.get(TemplatePOIConstants.CLASS), | |||||
| (List<?>)model.get(TemplatePOIConstants.LIST_DATA), | |||||
| (Map<String, Object>)model.get(TemplatePOIConstants.MAP_DATA)); | |||||
| (TemplateExportParams)model.get(TemplateExcelConstants.PARAMS), | |||||
| (Class<?>) model.get(TemplateExcelConstants.CLASS), | |||||
| (List<?>)model.get(TemplateExcelConstants.LIST_DATA), | |||||
| (Map<String, Object>)model.get(TemplateExcelConstants.MAP_DATA)); | |||||
| ServletOutputStream out = response.getOutputStream(); | ServletOutputStream out = response.getOutputStream(); | ||||
| workbook.write(out); | workbook.write(out); | ||||
| out.flush(); | out.flush(); | ||||
| @@ -0,0 +1,46 @@ | |||||
| package org.jeecgframework.poi.excel.view; | |||||
| import java.util.Map; | |||||
| import javax.servlet.ServletOutputStream; | |||||
| import javax.servlet.http.HttpServletRequest; | |||||
| import javax.servlet.http.HttpServletResponse; | |||||
| import org.apache.poi.xwpf.usermodel.XWPFDocument; | |||||
| import org.jeecgframework.poi.excel.entity.vo.TemplateWordConstants; | |||||
| import org.jeecgframework.poi.word.WordExportUtil; | |||||
| import org.springframework.web.servlet.view.AbstractView; | |||||
| /** | |||||
| * 模板视图 | |||||
| * | |||||
| * @author JueYue | |||||
| * @date 2014年6月30日 下午9:15:49 | |||||
| */ | |||||
| @SuppressWarnings("unchecked") | |||||
| public class JeecgTemplateWordView extends AbstractView { | |||||
| private static final String CONTENT_TYPE = "application/msword"; | |||||
| public JeecgTemplateWordView() { | |||||
| setContentType(CONTENT_TYPE); | |||||
| } | |||||
| @Override | |||||
| protected void renderMergedOutputModel(Map<String, Object> model, | |||||
| HttpServletRequest request, HttpServletResponse response) | |||||
| throws Exception { | |||||
| String codedFileName = "临时文件.docx"; | |||||
| if (model.containsKey(TemplateWordConstants.FILE_NAME)) { | |||||
| codedFileName = (String) model.get(TemplateWordConstants.FILE_NAME) | |||||
| + ".docx"; | |||||
| } | |||||
| response.setHeader("content-disposition", "attachment;filename=" | |||||
| + new String(codedFileName.getBytes(), "iso8859-1")); | |||||
| XWPFDocument document = WordExportUtil.exportWord07((String)model.get(TemplateWordConstants.URL), | |||||
| (Map<String, Object>)model.get(TemplateWordConstants.MAP_DATA)); | |||||
| ServletOutputStream out = response.getOutputStream(); | |||||
| document.write(out); | |||||
| out.flush(); | |||||
| } | |||||
| } | |||||
| @@ -1,5 +1,5 @@ | |||||
| #Generated by Maven Integration for Eclipse | #Generated by Maven Integration for Eclipse | ||||
| #Thu Jul 24 22:34:52 CST 2014 | |||||
| #Thu Jul 24 23:30:51 CST 2014 | |||||
| version=2.0.1 | version=2.0.1 | ||||
| groupId=org.jeecgframework | groupId=org.jeecgframework | ||||
| m2e.projectName=easypoi-web | m2e.projectName=easypoi-web | ||||