excel模板文件下载
2017-08-15 19:20
337 查看
方案一:
以下demo主要采用的方案是在项目的resources/template文件夹下存放好限定了若干条件的excel文件,用流的方式下载,支持弹窗方式选定下载路径和后端指定路径的方式。
首先需要制作一个限定了若干条件的excel(例如限定文本/日期/数字/分数等格式,设置批注,设置输入值的校验等),目的在于限制运维人员规范的填写文件数据,避免胡乱的填写excel并进行错误的文件导入。博客不支持附件上传已制作好的excel模板文件,只好作罢,需要的可以留言索要。、
ok,下面就是上代码的时候了:
调用的工具类:(可复用)
方案二:直接用代码来生成,略繁琐,仅供参考。
/**
* 文件模板下载(EXCEL文件)--备份
* @return
* @throws IOException
*/
@RequestMapping(value = "/transDetail/download", method = {RequestMethod.POST, RequestMethod.GET} )
public void downloadTemplateFileBackUp(HttpServletRequest req, HttpServletResponse resp) throws IOException{
logger.info("downloadTemplateFile start!");
if (false) { //写死走xlsx格式
HSSFWorkbook xlsWorkbook = new HSSFWorkbook();
HSSFSheet sheet = xlsWorkbook.createSheet("交易导入的文件模板表");
HSSFRow row = sheet.createRow((int) 0);
HSSFCell cell = (HSSFCell)this.setSummaryTitle(row);
row = sheet.createRow(1);
row = sheet.createRow(2);
cell = (HSSFCell)this.setDetailTitle(row);
String fileName = "商户号_YYYYMMDD_批次号";
downloadOutStream(xlsWorkbook, resp, true, ".xls", fileName);
} else {
XSSFWorkbook xlsxWorkbook = new XSSFWorkbook();
XSSFSheet sheet = xlsxWorkbook.createSheet("交易导入的文件模板表");
XSSFRow row = sheet.createRow((int) 0);
XSSFCell cell = (XSSFCell)this.setSummaryTitle(row);
row = sheet.createRow(1);
row = sheet.createRow(2);
cell = (XSSFCell)this.setDetailTitle(row);
String fileName = "商户号_YYYYMMDD_批次号";
downloadOutStream(xlsxWorkbook, resp, true, ".xlsx", fileName);
}
}
public Cell setSummaryTitle(Row row){
// 设置表头
Cell cell = row.createCell(0);
cell.setCellValue("商户号");
cell = row.createCell(1);
cell.setCellValue("商户名称");
cell = row.createCell(2);
cell.setCellValue("商户批次号");
cell = row.createCell(3);
cell.setCellValue("支付总笔数(单位:笔)");
cell = row.createCell(4);
cell.setCellValue("支付总金额(单位:分)");
cell = row.createCell(5);
cell.setCellValue("退款总笔数(单位:笔)");
cell = row.createCell(6);
cell.setCellValue("退款总金额(单位:分)");
cell = row.createCell(7);
cell.setCellValue("净笔数(单位:笔)");
cell = row.createCell(8);
cell.setCellValue("净金额(单位:分)");
return cell;
}
public Cell setDetailTitle(Row row){
// 设置表头
Cell cell = row.createCell(0);
cell.setCellValue("序号");
cell = row.createCell(1);
cell.setCellValue("商户订单号");
cell = row.createCell(2);
cell.setCellValue("支付(退款)订单号");
cell = row.createCell(3);
cell.setCellValue("原支付订单号");
cell = row.createCell(4);
cell.setCellValue("商户交易日期");
cell = row.createCell(5);
cell.setCellValue("交易类型");
cell = row.createCell(6);
cell.setCellValue("币种");
cell = row.createCell(7);
cell.setCellValue("结算金额");
cell = row.createCell(8);
cell.setCellValue("客户类型");
cell = row.createCell(9);
cell.setCellValue("客户名称");
cell = row.createCell(10);
cell.setCellValue("证件类型");
cell = row.createCell(11);
cell.setCellValue("证件号码");
cell = row.createCell(12);
cell.setCellValue("款项类型");
cell = row.createCell(13);
cell.setCellValue("保税货物项下付款");
cell = row.createCell(14);
cell.setCellValue("交易编码");
cell = row.createCell(15);
cell.setCellValue("款项类型");
cell = row.createCell(16);
cell.setCellValue("人民币账号");
cell = row.createCell(17);
cell.setCellValue("交易附言");
return cell;
}
public void downloadOutStream(Workbook workbook, HttpServletResponse resp, boolean isPopupStyle, String suffix, String fileName) throws IOException{
if (isPopupStyle) { //弹出下载框的方式
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
resp.reset();
if (suffix.equals(".xls")) { //支持.xls格式的
resp.setContentType("application/vnd.ms-excel;charset=iso-8859-1");
resp.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
}
if (suffix.equals(".xlsx")) {
resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=iso-8859-1");
resp.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xlsx").getBytes(), "iso-8859-1"));
resp.setContentLength(content.length);
}
ServletOutputStream out = resp.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("下载发生异常,请重新下载!");
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
} else { //不弹出下载框的方式
FileOutputStream fos = new FileOutputStream("E:/"+ fileName + ".xlsx");
workbook.write(fos);
fos.close();
}
}
以下demo主要采用的方案是在项目的resources/template文件夹下存放好限定了若干条件的excel文件,用流的方式下载,支持弹窗方式选定下载路径和后端指定路径的方式。
首先需要制作一个限定了若干条件的excel(例如限定文本/日期/数字/分数等格式,设置批注,设置输入值的校验等),目的在于限制运维人员规范的填写文件数据,避免胡乱的填写excel并进行错误的文件导入。博客不支持附件上传已制作好的excel模板文件,只好作罢,需要的可以留言索要。、
ok,下面就是上代码的时候了:
/** * 功能:商户结算单文件的下载:xxx.xlsx文件 * @throws IOException */ @RequestMapping(value = "/transactionSettleOrder/download", method = {RequestMethod.POST, RequestMethod.GET} ) @RequestGuard(perm = "merchant.settle.download") public void downloadSettleOrderFile(HttpServletRequest req, HttpServletResponse resp) throws IOException { String queryParam = req.getParameter("queryParam").replace(""", "'").replace("''", "null"); SettlementOrderModel settlementOrderModel = JSONObject.parseObject(queryParam, SettlementOrderModel.class); logger.info("SettlementOrderRestApiController downloadSettleOrderFile....request params is : " + JSON.toJSONString(settlementOrderModel)); XSSFWorkbook xlsxWorkbook = new XSSFWorkbook(); XSSFSheet sheet = xlsxWorkbook.createSheet("结算单查询的文件下载"); XSSFRow row = sheet.createRow(0); String titleParams = "序号,商户号,商户名称,结算单号,结算日期,交易开始日期,交易截止日期,交易币种,支付金额,退款金额,支付手续费,退款手续费,结算金额,结算币种,结算单状态"; ExcelDownloadHelperController excelHelper = new ExcelDownloadHelperController(); XSSFCell cell = (XSSFCell)excelHelper.setTitle(row, titleParams); cell = (XSSFCell)settlementOrderService.setDetailData(sheet, cell, row, settlementOrderModel); String fileName = "商户结算单_" + (new SimpleDateFormat("yyyyMMdd").format(new Date())); ExcelDownloadHelperController excelhelper = new ExcelDownloadHelperController(); excelhelper.downloadOutStream(xlsxWorkbook, resp, true, fileName); }
调用的工具类:(可复用)
public class ExcelDownloadHelperController { /* * 功能:写入excel文件中的标题行 * @Param row * @Param titleParams 传入字符串参数,以逗号分割。例如"标题1,标题2,标题3" */ public Cell setTitle(Row row, String titleParams){ String[] titleArr = titleParams.split(","); Cell cell = null; for (int i = 0; i < titleArr.length; i++) { cell = row.createCell(i); cell.setCellValue(titleArr[i]); } return cell; } /* * 功能:用流的方式的excel文件的下载 * @Param workbook * @Param isPopupStyle 是否弹出下载框的方式,若是false,则自动下载为E:/{fileName}.xlsx * @Param fileName 要下载出的文件名 */ public void downloadOutStream(Workbook workbook, HttpServletResponse resp, boolean isPopupStyle, String fileName) throws IOException { if (isPopupStyle) { //弹出下载框的方式 ByteArrayOutputStream os = new ByteArrayOutputStream(); workbook.write(os); byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); // 设置response参数,可以打开下载页面 resp.reset(); resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=iso-8859-1"); resp.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xlsx").getBytes(), "iso-8859-1")); resp.setContentLength(content.length); ServletOutputStream out = resp.getOutputStream(); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(is); bos = new BufferedOutputStream(out); byte[] buff = new byte[2048]; int bytesRead; // Simple read/w bc8b rite loop. while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } bos.flush(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("下载发生异常,请重新下载!"); } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); } } else { //不弹出下载框的方式 FileOutputStream fos = new FileOutputStream("E:/"+ fileName + ".xlsx"); workbook.write(fos); fos.close(); } } }本着无私的精神,再附上前端的代码(html + angularJS):
//文件模板下载 $scope.downloadTemplate = function(){ location.href = "/proxy/cap-cbs-mgr/mgr/transDetail/download"; };
<div class="ice-column"> <ice-permission value='transaction.import.download' out='downloadTemplate123'> <ice-button text='文件模板下载' hanle-click='downloadTemplate()'/> </ice-permission> </div>
方案二:直接用代码来生成,略繁琐,仅供参考。
/**
* 文件模板下载(EXCEL文件)--备份
* @return
* @throws IOException
*/
@RequestMapping(value = "/transDetail/download", method = {RequestMethod.POST, RequestMethod.GET} )
public void downloadTemplateFileBackUp(HttpServletRequest req, HttpServletResponse resp) throws IOException{
logger.info("downloadTemplateFile start!");
if (false) { //写死走xlsx格式
HSSFWorkbook xlsWorkbook = new HSSFWorkbook();
HSSFSheet sheet = xlsWorkbook.createSheet("交易导入的文件模板表");
HSSFRow row = sheet.createRow((int) 0);
HSSFCell cell = (HSSFCell)this.setSummaryTitle(row);
row = sheet.createRow(1);
row = sheet.createRow(2);
cell = (HSSFCell)this.setDetailTitle(row);
String fileName = "商户号_YYYYMMDD_批次号";
downloadOutStream(xlsWorkbook, resp, true, ".xls", fileName);
} else {
XSSFWorkbook xlsxWorkbook = new XSSFWorkbook();
XSSFSheet sheet = xlsxWorkbook.createSheet("交易导入的文件模板表");
XSSFRow row = sheet.createRow((int) 0);
XSSFCell cell = (XSSFCell)this.setSummaryTitle(row);
row = sheet.createRow(1);
row = sheet.createRow(2);
cell = (XSSFCell)this.setDetailTitle(row);
String fileName = "商户号_YYYYMMDD_批次号";
downloadOutStream(xlsxWorkbook, resp, true, ".xlsx", fileName);
}
}
public Cell setSummaryTitle(Row row){
// 设置表头
Cell cell = row.createCell(0);
cell.setCellValue("商户号");
cell = row.createCell(1);
cell.setCellValue("商户名称");
cell = row.createCell(2);
cell.setCellValue("商户批次号");
cell = row.createCell(3);
cell.setCellValue("支付总笔数(单位:笔)");
cell = row.createCell(4);
cell.setCellValue("支付总金额(单位:分)");
cell = row.createCell(5);
cell.setCellValue("退款总笔数(单位:笔)");
cell = row.createCell(6);
cell.setCellValue("退款总金额(单位:分)");
cell = row.createCell(7);
cell.setCellValue("净笔数(单位:笔)");
cell = row.createCell(8);
cell.setCellValue("净金额(单位:分)");
return cell;
}
public Cell setDetailTitle(Row row){
// 设置表头
Cell cell = row.createCell(0);
cell.setCellValue("序号");
cell = row.createCell(1);
cell.setCellValue("商户订单号");
cell = row.createCell(2);
cell.setCellValue("支付(退款)订单号");
cell = row.createCell(3);
cell.setCellValue("原支付订单号");
cell = row.createCell(4);
cell.setCellValue("商户交易日期");
cell = row.createCell(5);
cell.setCellValue("交易类型");
cell = row.createCell(6);
cell.setCellValue("币种");
cell = row.createCell(7);
cell.setCellValue("结算金额");
cell = row.createCell(8);
cell.setCellValue("客户类型");
cell = row.createCell(9);
cell.setCellValue("客户名称");
cell = row.createCell(10);
cell.setCellValue("证件类型");
cell = row.createCell(11);
cell.setCellValue("证件号码");
cell = row.createCell(12);
cell.setCellValue("款项类型");
cell = row.createCell(13);
cell.setCellValue("保税货物项下付款");
cell = row.createCell(14);
cell.setCellValue("交易编码");
cell = row.createCell(15);
cell.setCellValue("款项类型");
cell = row.createCell(16);
cell.setCellValue("人民币账号");
cell = row.createCell(17);
cell.setCellValue("交易附言");
return cell;
}
public void downloadOutStream(Workbook workbook, HttpServletResponse resp, boolean isPopupStyle, String suffix, String fileName) throws IOException{
if (isPopupStyle) { //弹出下载框的方式
ByteArrayOutputStream os = new ByteArrayOutputStream();
workbook.write(os);
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
// 设置response参数,可以打开下载页面
resp.reset();
if (suffix.equals(".xls")) { //支持.xls格式的
resp.setContentType("application/vnd.ms-excel;charset=iso-8859-1");
resp.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
}
if (suffix.equals(".xlsx")) {
resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=iso-8859-1");
resp.setHeader("Content-Disposition", "attachment;filename="+ new String((fileName + ".xlsx").getBytes(), "iso-8859-1"));
resp.setContentLength(content.length);
}
ServletOutputStream out = resp.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("下载发生异常,请重新下载!");
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
} else { //不弹出下载框的方式
FileOutputStream fos = new FileOutputStream("E:/"+ fileName + ".xlsx");
workbook.write(fos);
fos.close();
}
}
相关文章推荐
- PHP将服务器上的文件下载(例如下载Excel模板)
- excel模板下载(可适用于各种格式文件下载)
- [WinForm每日一帖] 如何使用资源文件提供Excel模板下载
- php excel下载模板方法2,放excel文件在服务器上,直接拉取
- java实现赋值excel模板,并在新文件中写入数据,并且下载
- SpringMVC+POI下载文件模板和导出Excel
- Excel文件导入导出实战(3)-xml模板文件的上传下载
- 下载Excel文件模板 ---文件下载--SSM
- Java下载模板文件(Excel demo)
- 如何使用资源文件提供Excel模板下载
- java对word文件模板写入下载
- 下载模板,上传EXCEL
- 用easyui-filebox上传Excel文件(ASP.NET MVC)[附源码下载]
- java 下载样板Excel文件
- 【JavaScript】使用form表单实现excel下载模板
- HTML文件模板替换到转换PDF文件并下载PDF文件
- 开源jxls - java导Excel(多sheet) - 模板文件定义类似jsp的el表达式
- 使用jxl动态生成excel文件并提示下载
- 2、Python djang 框架下的word Excel TXT Image 等文件的下载
- 解决使用超链接下载word/EXCEL文件时被IE打开的问题