ssm集成poi實現下載
目前直接將文件導出,不使用uploadfile的方法在ssm集成中主要是根據response方法進行操作
其中主要有以下兩種方法:
1.使用ResponseEntity<byte[]>
//導出excel
@RequestMapping(value="find_select_2",method=RequestMethod.GET)
public ResponseEntity<byte[]> find_select_2(@RequestParam(value = "selectMethod") String selectMethod,@RequestParam(value = "gochecked") String gochecked,SelectAllFrist saf,HttpServletResponse response) throws IOException{
/* // 只是讓瀏覽器知道要保存爲什麼文件而已,真正的文件還是在流裏面的數據,你設定一個下載類型並不會去改變流裏的內容。
//而實際上只要你的內容正確,文件後綴名之類可以隨便改,就算你指定是下載excel文件,下載時我也可以把他改成pdf等。
response.setContentType("application/vnd.ms-excel");
// 傳遞中文參數編碼
String codedFileName = java.net.URLEncoder.encode("中文","UTF-8");
response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");*/
List<SelectAllFrist> list = service.export(selectMethod, gochecked, saf);
// 定義一個工作薄
Workbook workbook = new HSSFWorkbook();
// 創建一個sheet頁
Sheet sheet = workbook.createSheet("導出信息");
// 創建一行
Row row = sheet.createRow(0);
// 在本行賦值 以0開始
row.createCell(0).setCellValue("ID");
row.createCell(1).setCellValue("類型");
row.createCell(2).setCellValue("建單時間");
row.createCell(3).setCellValue("供應商/客戶");
row.createCell(4).setCellValue("建單人");
row.createCell(5).setCellValue("狀態");
row.createCell(6).setCellValue("庫房");
// 定義樣式
CellStyle cellStyle = workbook.createCellStyle();
// 格式化日期
//cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
// 遍歷輸出
for (int i = 1; i <= list.size(); i++) {
SelectAllFrist saf2 = list.get(i - 1);
row = sheet.createRow(i);
row.createCell(0).setCellValue(saf2.getId());
row.createCell(1).setCellValue(saf2.getType());
row.createCell(2).setCellValue(saf2.getCreate_date());
row.createCell(3).setCellValue(saf2.getSupplier());
row.createCell(4).setCellValue(saf2.getCreate_man_id());
row.createCell(5).setCellValue(saf2.getStatus());
row.createCell(6).setCellValue(saf2.getWarehouse());
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
workbook.write(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpHeaders headers = new HttpHeaders();
String fileName = new String("giveme.xls".getBytes("UTF-8"), "iso-8859-1");//爲了解決中文名稱亂碼問題
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ResponseEntity<byte[]> filebyte = new ResponseEntity<byte[]>(out.toByteArray(),headers, HttpStatus.CREATED);
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return filebyte;
}
2.使用@ResponseBody
//導出excel
@RequestMapping(value="find_select",method=RequestMethod.GET)
public @ResponseBody void find_select(@RequestParam(value = "selectMethod") String selectMethod,@RequestParam(value = "gochecked") String gochecked,SelectAllFrist saf,HttpServletResponse response) throws IOException{
// 只是讓瀏覽器知道要保存爲什麼文件而已,真正的文件還是在流裏面的數據,你設定一個下載類型並不會去改變流裏的內容。
//而實際上只要你的內容正確,文件後綴名之類可以隨便改,就算你指定是下載excel文件,下載時我也可以把他改成pdf等。
response.setContentType("application/vnd.ms-excel");
// 傳遞中文參數編碼
String codedFileName = java.net.URLEncoder.encode("myexcel","UTF-8");
response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
List<SelectAllFrist> list = service.export(selectMethod, gochecked, saf);
// 定義一個工作薄
Workbook workbook = new HSSFWorkbook();
// 創建一個sheet頁
Sheet sheet = workbook.createSheet("導出信息");
// 創建一行
Row row= sheet.createRow(0);
// 在本行賦值 以0開始
row.createCell(0).setCellValue("ID");
row.createCell(1).setCellValue("類型");
row.createCell(2).setCellValue("建單時間");
row.createCell(3).setCellValue("供應商/客戶");
row.createCell(4).setCellValue("建單人");
row.createCell(5).setCellValue("狀態");
row.createCell(6).setCellValue("庫房");
// 定義樣式
CellStyle cellStyle = workbook.createCellStyle();
// 格式化日期
//cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
// 遍歷輸出
for (int i = 1; i <= list.size(); i++) {
SelectAllFrist saf2 = list.get(i - 1);
row = sheet.createRow(i);
row.createCell(0).setCellValue(saf2.getId());
row.createCell(1).setCellValue(saf2.getType());
row.createCell(2).setCellValue(saf2.getCreate_date());
row.createCell(3).setCellValue(saf2.getSupplier());
row.createCell(4).setCellValue(saf2.getCreate_man_id());
row.createCell(5).setCellValue(saf2.getStatus());
row.createCell(6).setCellValue(saf2.getWarehouse());
}
try {
OutputStream fOut = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(fOut);
bufferedOutPut.flush();
workbook.write(bufferedOutPut);
bufferedOutPut.close();
} catch (IOException e) {
e.printStackTrace();
}
}