關於一個標準的poi下載文件模板 可拿來來直接使用

@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();
}
}web

相關文章
相關標籤/搜索