假如已這樣的表頭導(dǎo)出數(shù)據(jù)
?1.把包含表頭的excel添加到項(xiàng)目資源目錄文章來源:http://www.zghlxwxcb.cn/news/detail-642600.html
?2.編寫代碼讀取表頭所在sheet,并且加入需導(dǎo)出的數(shù)據(jù)文章來源地址http://www.zghlxwxcb.cn/news/detail-642600.html
/**
* 導(dǎo)出excel
*/
public static void downloadExcel(List<List<Object>> list, HttpServletResponse response) throws IOException {
/*String tempPath =System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
File file = new File(tempPath);*/
//獲取模板文件第一個(gè)sheet
ClassPathResource resource = new ClassPathResource("cadreAndCompany.xls");
ExcelReader reader = ExcelUtil.getReader(resource.getStream());
Sheet rows = reader.getSheets().get(0);
BigExcelWriter bigExcelWriter = new BigExcelWriter(rows);
bigExcelWriter.passRows(3);//跳過表頭
// 一次性寫出內(nèi)容,使用默認(rèn)樣式,強(qiáng)制輸出標(biāo)題
bigExcelWriter.write(list);
//response為HttpServletResponse對(duì)象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//test.xls是彈出下載對(duì)話框的文件名,不能為中文,中文請(qǐng)自行編碼
response.setHeader("Content-Disposition","attachment;filename=file.xlsx");
ServletOutputStream out=response.getOutputStream();
// 終止后刪除臨時(shí)文件
//file.deleteOnExit();
bigExcelWriter.flush(out, true);
//此處記得關(guān)閉輸出Servlet流
IoUtil.close(out);
}
到了這里,關(guān)于hutool 導(dǎo)出復(fù)雜表頭excel的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!