Java導(dǎo)出Excel文件的方法
1.請(qǐng)求中不帶請(qǐng)求中帶HttpServletResponse response。
使用ExcelData和ExportExcelUtils工具類(lèi)結(jié)XSSFWorkbook合實(shí)現(xiàn)Excel文件導(dǎo)出
ExcelData data = new ExcelData();
data.setName("頁(yè)簽名稱(chēng)");
// 標(biāo)題
List<String> titles = new ArrayList();
titles.add("第一列");
titles.add("第二列");
titles.add("第三列");
data.setTitles(titles);
// 內(nèi)容
List<List<Object>> rows = new ArrayList();
for(int i=0; i<10; i++){
List<Object> row = new ArrayList();
row.add(第+"i"+行第一列);
row.add(第+"i"+行第二列);
row.add(第+"i"+行第三列);
rows.add(row);
}
data.setRows(rows);
XSSFWorkbook wb = ExportExcelUtils.exportExcel(data);
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);
wb.close();
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/vnd.ms-excel");
headers.add("Connection", "close");
headers.add("file", "type");
headers.add("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
return new ResponseEntity<byte[]>(outByteStream.toByteArray(), headers, HttpStatus.CREATED);
2.請(qǐng)求中帶HttpServletResponse response。
使用XSSFWorkbook合實(shí)現(xiàn)Excel文件導(dǎo)出
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("UTF-8");
response.setHeader("file", "type");
response.setHeader("Connection", "close");
response.setHeader("Content-disposition", "attachment;filename=" + ExcelUtil.toUtf8String("文件名"));
String filePath = "/docs/file.xlsx";
InputStream inputStream = new ClassPathResource(filePath).getInputStream();
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(inputStream);
// sheet
XSSFSheet sheetAt = xssfWorkbook.getSheetAt(0);
sheetAt.setColumnWidth(0, 15000);
// 單元格格式
XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setBorderTop(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
// 標(biāo)題
writeValue(sheetAt, cellStyle, 0, 0, "第一列");
writeValue(sheetAt, cellStyle, 0, 0, "第二列");
writeValue(sheetAt, cellStyle, 0, 0, "第三列");
// 內(nèi)容
for(int i=1; i=10; i++){
writeValue(sheetAt, cellStyle, i, 0, "第"+i+"行第一列");
writeValue(sheetAt, cellStyle, i, 1, "第"+i+"行第二列");
writeValue(sheetAt, cellStyle, i, 2, "第"+i+"行第三列");
}
xssfWorkbook.write(response.getOutputStream());
if (null != xssfWorkbook) {
xssfWorkbook.close();
}
到此這篇關(guān)于Java導(dǎo)出Excel文件的方法的文章就介紹到這了,更多相關(guān)Java導(dǎo)出Excel文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)年獸大作戰(zhàn)游戲詳解
春節(jié)要到了,看慣了前端各種小游戲,確實(shí)做得很好,很精致。本文將為大家介紹一款java版本的年獸大作戰(zhàn)游戲,感興趣的小伙伴可以試一試2022-01-01
JAVA后臺(tái)轉(zhuǎn)換成樹(shù)結(jié)構(gòu)數(shù)據(jù)返回給前端的實(shí)現(xiàn)方法
這篇文章主要介紹了JAVA后臺(tái)轉(zhuǎn)換成樹(shù)結(jié)構(gòu)數(shù)據(jù)返回給前端的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
解決Springboot中Feignclient調(diào)用時(shí)版本問(wèn)題
這篇文章主要介紹了解決Springboot中Feign?client調(diào)用時(shí)版本問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
mybatis自動(dòng)生成時(shí)如何設(shè)置不生成Example類(lèi)詳解
這篇文章主要給大家介紹了關(guān)于mybatis自動(dòng)生成時(shí)如何設(shè)置不生成Example類(lèi)的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05
Java實(shí)現(xiàn)學(xué)生管理系統(tǒng)詳解流程
這篇文章主要為大家詳細(xì)介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

