码迷,mamicode.com
首页 > 其他好文 > 详细

关于一个标准的poi下载文件模板 可拿来来直接使用

时间:2019-09-24 10:32:13      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:stream   时间   enter   load   rac   数据   res   row   try   

@RequestMapping(value = "/RK/downloadDemo")
@ResponseBody
public void downloadExcel(HttpServletResponse response,HttpServletRequest request) {
try {
Workbook workbook = new HSSFWorkbook();
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-download");
String filedisplay = "资产入库模板.xls";
filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename="+ filedisplay);
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
Sheet sheet = workbook.createSheet("资产入库模板");
// 第三步,在sheet中添加表头第0行
Row row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER); // 创建一个居中格式
Cell cell = row.createCell(0);
cell.setCellValue("资产类别");
cell.setCellStyle(style);
sheet.setColumnWidth(0, (25 * 256)); //设置列宽,50个字符宽
cell = row.createCell(1);
cell.setCellValue("资产名称");
cell.setCellStyle(style);
sheet.setColumnWidth(1, (20 * 256)); //设置列宽,50个字符宽
cell = row.createCell(2);
cell.setCellValue("规格型号");
cell.setCellStyle(style);
sheet.setColumnWidth(2, (15 * 256)); //设置列宽,50个字符宽
cell = row.createCell(3);
cell.setCellValue("SN号");
cell.setCellStyle(style);
sheet.setColumnWidth(3, (15 * 256)); //设置列宽,50个字符宽
cell = row.createCell(4);
cell.setCellValue("单位");
cell.setCellStyle(style);
sheet.setColumnWidth(4, (20 * 256)); //设置列宽,50个字符宽
cell = row.createCell(5);
cell.setCellValue("单价");
cell.setCellStyle(style);
sheet.setColumnWidth(5, (20 * 256)); //设置列宽,50个字符宽
cell = row.createCell(6);
cell.setCellValue("残值率");
cell.setCellStyle(style);
sheet.setColumnWidth(6, (20 * 256)); //设置列宽,50个字符宽

cell = row.createCell(7);
cell.setCellValue("存放仓库");
cell.setCellStyle(style);
sheet.setColumnWidth(7, (20 * 256)); //设置列宽,50个字符宽

cell = row.createCell(8);
cell.setCellValue("购入时间");
cell.setCellStyle(style);
sheet.setColumnWidth(8, (20 * 256)); //设置列宽,50个字符宽

cell = row.createCell(9);
cell.setCellValue("使用期限");
cell.setCellStyle(style);
sheet.setColumnWidth(9, (20 * 256)); //设置列宽,50个字符宽

cell = row.createCell(10);
cell.setCellValue("供应商");
cell.setCellStyle(style);
sheet.setColumnWidth(10, (20 * 256)); //设置列宽,50个字符宽
// 第五步,写入实体数据 实际应用中这些数据从数据库得到
row = sheet.createRow(1);
row.createCell(0, Cell.CELL_TYPE_STRING).setCellValue("14");
row.createCell(1, Cell.CELL_TYPE_STRING).setCellValue("笔记本电脑");
row.createCell(2, Cell.CELL_TYPE_STRING).setCellValue("Y1123");
row.createCell(3, Cell.CELL_TYPE_STRING).setCellValue("SN01");
row.createCell(4, Cell.CELL_TYPE_STRING).setCellValue("台");
row.createCell(5, Cell.CELL_TYPE_STRING).setCellValue("5000");
row.createCell(6, Cell.CELL_TYPE_STRING).setCellValue("0.01");
row.createCell(7, Cell.CELL_TYPE_STRING).setCellValue("9");
row.createCell(8, Cell.CELL_TYPE_STRING).setCellValue("2019-09-01");
row.createCell(9, Cell.CELL_TYPE_STRING).setCellValue("36");
row.createCell(10, Cell.CELL_TYPE_STRING).setCellValue("联想专卖");
// 第六步,将文件存到指定位置
try
{
OutputStream out = response.getOutputStream();
workbook.write(out);
out.close();
}
catch (Exception e)
{
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}

关于一个标准的poi下载文件模板 可拿来来直接使用

标签:stream   时间   enter   load   rac   数据   res   row   try   

原文地址:https://www.cnblogs.com/490889763-qq/p/11576734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!