|
@@ -0,0 +1,177 @@
|
|
|
+package com.mall.util;
|
|
|
+
|
|
|
+import net.sf.jxls.exception.ParsePropertyException;
|
|
|
+import net.sf.jxls.transformer.XLSTransformer;
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 根据模板导出excel到服务器本地
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class ExcelBuilder {
|
|
|
+ private static final String CONTENT_TYPE = "application/x-msdownload;charset=UTF-8";
|
|
|
+ private Logger log = Logger.getLogger(this.getClass());
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param config templates/s_ana_lus.xls
|
|
|
+ * @param model Map<String, Object>
|
|
|
+ * @return fileName
|
|
|
+ */
|
|
|
+ public String export(String config, Map<String, Object> model,HttpServletRequest request) throws IllegalAccessException {
|
|
|
+ String fileName = "ERROR";
|
|
|
+ InputStream in = null;
|
|
|
+ OutputStream out = null;
|
|
|
+ try {
|
|
|
+ log.debug("准备导出...");
|
|
|
+ String tmpFileName = UUID.randomUUID() + ".xls";
|
|
|
+ String output=request.getSession().getServletContext().getRealPath("download/" + tmpFileName);
|
|
|
+ log.debug("输出路径:" + output);
|
|
|
+ in = ExcelBuilder.class.getClassLoader().getResourceAsStream(config);
|
|
|
+ out = new FileOutputStream(output);
|
|
|
+ XLSTransformer transformer = new XLSTransformer();
|
|
|
+ transformer.transformXLS(in, model).write(out);
|
|
|
+ //导出成功后 才设置导出的文件名
|
|
|
+ fileName = tmpFileName;
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (ParsePropertyException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (in != null)
|
|
|
+ in.close();
|
|
|
+ if (out != null)
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.debug("返回文件名:" + fileName);
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String export(String config, Map<String, Object> model,String filename,HttpServletRequest request) throws IllegalAccessException {
|
|
|
+ String fileName = "ERROR";
|
|
|
+ InputStream in = null;
|
|
|
+ OutputStream out = null;
|
|
|
+ try {
|
|
|
+ log.debug("准备导出...");
|
|
|
+// String tmpFileName = UUID.randomUUID() + ".xls";
|
|
|
+ String tmpFileName=filename+".xls";
|
|
|
+ String output=request.getSession().getServletContext().getRealPath("download/" + tmpFileName);
|
|
|
+ log.debug("输出路径:" + output);
|
|
|
+ in = ExcelBuilder.class.getClassLoader().getResourceAsStream(config);
|
|
|
+ out = new FileOutputStream(output);
|
|
|
+ XLSTransformer transformer = new XLSTransformer();
|
|
|
+ transformer.transformXLS(in, model).write(out);
|
|
|
+ //导出成功后 才设置导出的文件名
|
|
|
+ fileName = tmpFileName;
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (ParsePropertyException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (in != null)
|
|
|
+ in.close();
|
|
|
+ if (out != null)
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.debug("返回文件名:" + fileName);
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String export(String config, Map<String, Object> model,String filename,String path) throws IllegalAccessException {
|
|
|
+ String fileName = "ERROR";
|
|
|
+ InputStream in = null;
|
|
|
+ OutputStream out = null;
|
|
|
+ try {
|
|
|
+ String tmpFileName=filename+".xls";
|
|
|
+ String tmpPath=path+tmpFileName;
|
|
|
+ in = ExcelBuilder.class.getClassLoader().getResourceAsStream(config);
|
|
|
+ out = new FileOutputStream(tmpPath);
|
|
|
+ XLSTransformer transformer = new XLSTransformer();
|
|
|
+ transformer.transformXLS(in, model).write(out);
|
|
|
+ //导出成功后 才设置导出的文件名
|
|
|
+ fileName = tmpFileName;
|
|
|
+ } catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (in != null)
|
|
|
+ in.close();
|
|
|
+ if (out != null)
|
|
|
+ out.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+ //模板导出excel
|
|
|
+ public String export(String config, Map<String, Object> model,String title,HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
+ String fileName = "ERROR";
|
|
|
+ InputStream in = null;
|
|
|
+ OutputStream out = null;
|
|
|
+ try {
|
|
|
+ log.debug("准备导出...");
|
|
|
+ String tmpFileName = UUID.randomUUID() + ".xls";//随机导出Excel名称
|
|
|
+ String file_title=title+".xls";
|
|
|
+ final String path="C:/files/export/";
|
|
|
+ final File filePath = new File(path);
|
|
|
+ if(!filePath.exists()) //没有目录的,自动创建目录
|
|
|
+ filePath.mkdirs();
|
|
|
+ String tmpPath=path+tmpFileName;//文件完整路径
|
|
|
+ log.debug("输出路径:" + tmpPath);
|
|
|
+
|
|
|
+ in = ExcelBuilder.class.getClassLoader().getResourceAsStream(config);
|
|
|
+ out = new FileOutputStream(tmpPath);
|
|
|
+ XLSTransformer transformer = new XLSTransformer();
|
|
|
+
|
|
|
+ HSSFWorkbook b = transformer.transformXLS(in, model);
|
|
|
+ b.write(out);
|
|
|
+ //导出成功后 才设置导出的文件名
|
|
|
+ fileName = tmpFileName;
|
|
|
+
|
|
|
+ FileUtil fileUtil=new FileUtil();
|
|
|
+ response.setContentType(CONTENT_TYPE);
|
|
|
+ //下载生成的Excel
|
|
|
+ fileUtil.doDownloadFile(tmpPath, file_title, request, response);
|
|
|
+ //删除生成的Excel
|
|
|
+ fileUtil.doRemoveFile(tmpPath);
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (ParsePropertyException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (in != null)
|
|
|
+ in.close();
|
|
|
+ if (out != null)
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.debug("返回文件名:" + fileName);
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+}
|