@Override
public void modelexport(HttpServletResponse response) throws IOException {
//===方法一
String templatePath=this.getClass().getClassLoader().getResource("templates/model.xlsx").getPath();
InputStream is = new FileInputStream(new File(templatePath));
Workbook workbook = new XSSFWorkbook(is);
org.apache.poi.ss.usermodel.Sheet sheet =workbook.getSheetAt(0); //獲取到第一個工作表
Row row1=sheet.createRow(1);
row1.createCell(1).setCellValue("asdfasdfsafd");
Row row6=sheet.createRow(6);
row6.createCell(1).setCellValue("sdafsdafsd");
Row row7=sheet.createRow(7);
row7.createCell(1).setCellValue("sadfasdfsadfd");
//清空response
response.reset();
//設置response的Header
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//重要點
// String filename = new String(fileName.getBytes(), "iso8859-1") + dateFormat.format(new Date().getTime())+".xls";
response.addHeader("Content-Disposition", "attachment;filename=example.xlsx");
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=gb2312");
// response.setCharacterEncoding("UTF-8");
//將excel寫入到輸出流中
workbook.write(os);
os.flush();
os.close();
// log.info("設置瀏覽器下載成功!");
//===方法二
// ClassPathResource resource = new ClassPathResource("templates/233.xlsx");
// boolean isFile = resource.isFile();
// if(!isFile){ //如果不存在返回
// return;
// }
// String path = resource.getFile().getPath();
// //獲取文件路徑 /* 數據寫入模板文件中 */
// // 更改文件名編碼
// String fileName = "ex.xlsx";
// String gFileName = URLEncoder.encode(fileName, "UTF-8");
// //如進行下載名爲:文件(3).txt,下載時顯示名爲:文件+(3).txt --空格變爲了+號
// //解決辦法如下
// String dFileName = gFileName.replaceAll("\\+", "%20");
// InputStream in = null; Workbook exl = null;
// ByteArrayOutputStream out = new ByteArrayOutputStream();
// try {
// in = new FileInputStream(path);
// exl = WorkbookFactory.create(in);
// Sheet sheet1 = exl.getSheetAt(0);
// int rowNums = sheet1.getLastRowNum();
// //模板的行數0開始,返回值比行數小1
// Row row = sheet1.createRow( 1 );
// row.createCell(2).setCellValue("sadfasdfgsfsafsdafafsa"); //序號
//
// //激活瀏覽器彈出窗口
// response.setContentType("application/x-msdownload");
// //瀏覽器彈出窗口顯示的文件名
// response.addHeader("Content-Disposition", "attachment;filename=" + dFileName);
// exl.write(out);
// //in = new ByteArrayInputStream(out.toByteArray());
// response.getOutputStream().write(out.toByteArray());
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (InvalidFormatException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// try {
// if (exl != null) {
// exl.close();
// } if (out != null) {
// out.close();
// } if (in != null) {
// in.close();
// }
// } catch (IOException ex) {
// ex.printStackTrace();
// }
// }
}