java模板导出xlshss_Java使用jxls2根据模板导出excel

1 packagecom.sgcc.mall.onestop.utils;2

3 importcom.aspose.cells.License;4 importcom.aspose.cells.SaveFormat;5 importcom.aspose.cells.Workbook;6 importorg.apache.log4j.Logger;7 importorg.jxls.common.Context;8 importorg.jxls.util.JxlsHelper;9

10 importjavax.servlet.ServletOutputStream;11 importjavax.servlet.http.HttpServletResponse;12 import java.io.*;13 importjava.net.URLEncoder;14 importjava.util.Map;15

16

17 public final classFreeMarkerFileUtil {18 private static Logger log = Logger.getLogger(FreeMarkerFileUtil.class);19

20 public static final String UTF8 = "utf-8";21 /**

22 * 生成execl文件后缀名23 */

24 public static final String XLS_SUFFIX = ".xls";25 /**

26 * 生成pdf文件后缀名27 */

28 public static final String PDF_SUFFIX = ".pdf";29 /**

30 * 向浏览器输出execl文件类型,用于指定响应头信息31 */

32 public static final int XLS_OUT_TYPE = 1;33 /**

34 * 向浏览器输出execl文件响应头信息35 */

36 public static final String XLS_CONTENT_TYPE = "application/vnd.ms-excel;charset=" +UTF8;37 /**

38 * 向浏览器输出pdf文件类型,用于指定响应头信息39 */

40 public static final int PDF_OUT_TYPE = 2;41 /**

42 * 向浏览器输出pdf文件响应头信息43 */

44 public static final String PDF_CONTENT_TYPE = "application/pdf;charset=" +UTF8;45

46

47 /**

48 * 根据指定模板生成execl文件49 *50 *@paramtmpFilePath 模板文件51 *@paramfileName 文件名称52 *@parambeans 模板填充数据集53 *@return

54 */

55 public static File getXlsFile(String tmpFilePath, String fileName, Map beans) throwsIOException {56 File xlsFile =File.createTempFile(fileName, XLS_SUFFIX);57 Context context = newContext();58 context.putVar("beans", beans);59 log.info("进入数据填充");60 JxlsHelper.getInstance().processTemplate(new FileInputStream(tmpFilePath), newFileOutputStream(xlsFile), context);61 log.info("数据填充完成");62 returnxlsFile;63 }64

65 /**

66 * 根据指定模板生成execl的PDF格式文件67 *68 *@paramtmpFilePath 模板文件69 *@paramfileName 文件名称70 *@parambeans 模板填充数据集71 *@return

72 */

73 public static File getPdfFile(String tmpFilePath, String fileName, Map beans) throwsIOException {74 //根据模板获取execl文件

75 File xlsFile =getXlsFile(tmpFilePath, fileName, beans);76 File pdfFile =execl2Pdf(xlsFile, fileName);77 returnpdfFile;78 }79

80 /**

81 * execl文件转换为pdf文件82 *83 *@paramxlsFile execl文件84 *@parampdfFileName 生成的pdf文件名称85 *@return

86 */

87 public staticFile execl2Pdf(File xlsFile, String pdfFileName) {88 //验证License

89 if (!getLicense()) {90 throw new RuntimeException("验证License失败!");91 }92 try{93 InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(xlsFile), "UTF-8");94 Workbook wb = new Workbook(newFileInputStream(xlsFile));95 File pdfFile =File.createTempFile(pdfFileName, PDF_SUFFIX);96 //输出路径

97 FileOutputStream fileOS = newFileOutputStream(pdfFile);98 wb.save(fileOS, SaveFormat.PDF);99 returnpdfFile;100 } catch(Exception e) {101 e.printStackTrace();102 }103 return null;104 }105

106 /**

107 * 输出文件到浏览器108 *109 *@paramfile 需要输出的文件110 *@paramfileName 输出的文件名111 *@paramresponse112 *@paramtype 输出的文件类型113 *@throwsIOException114 */

115 public static void outFile(File file, String fileName, HttpServletResponse response, int type) throwsIOException {116 try{117 fileName =URLEncoder.encode(fileName, UTF8);118 } catch(UnsupportedEncodingException e) {119 e.printStackTrace();120 throw new RuntimeException("不支持的编码格式:" +UTF8);121 }122

123 response.reset();124 //设置响应文本格式

125 if (type ==XLS_OUT_TYPE) {126 response.setContentType(XLS_CONTENT_TYPE);127 } else if (type ==PDF_OUT_TYPE) {128 response.setContentType(PDF_CONTENT_TYPE);129 } else{130 throw new RuntimeException("类型错误");131 }132 response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName).getBytes(), "iso-8859-1"));133 InputStream bis = null;134 OutputStream bos = null;135 try{136 //将文件输出到页面

137 ServletOutputStream out =response.getOutputStream();138 bis = new BufferedInputStream(newFileInputStream(file));139 bos = newBufferedOutputStream(out);140 byte[] buff = new byte[1024];141 intbytesRead;142 //根据读取并写入

143 while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {144 bos.write(buff, 0, bytesRead);145 }146 } finally{147 if (bis != null) {148 bis.close();149 }150 if (bos != null) {151 bos.close();152 }153 }154

155 }156

157 /**

158 * 输出文件到本地磁盘159 *160 *@paramfile161 *@paramoutDir162 *@throwsIOException163 */

164 public static void outFile(File file, String outDir) throwsIOException {165 InputStream bis = null;166 OutputStream bos = null;167 try{168 bis = new BufferedInputStream(newFileInputStream(file));169 bos = new BufferedOutputStream(new FileOutputStream(outDir +file.getName()));170 byte[] buff = new byte[2048];171 intbytesRead;172 //根据读取并写入

173 while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {174 bos.write(buff, 0, bytesRead);175 }176 } finally{177 if (null !=bis) {178 bis.close();179 }180 if (null !=bos) {181 bos.close();182 }183 }184 }185 /**

186 * 验证license187 *188 *@return

189 */

190 public static booleangetLicense() {191 boolean result = false;192 try{193 InputStream is = FreeMarkerFileUtil.class.getClassLoader().getResourceAsStream("\\license.xml");194 License aposeLic = newLicense();195 aposeLic.setLicense(is);196 result = true;197 } catch(Exception e) {198 e.printStackTrace();199 }200 returnresult;201 }202 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值