Java導(dǎo)出Excel文件的方法
1.請(qǐng)求中不帶請(qǐng)求中帶HttpServletResponse response。
使用ExcelData和ExportExcelUtils工具類結(jié)XSSFWorkbook合實(shí)現(xiàn)Excel文件導(dǎo)出
ExcelData data = new ExcelData(); data.setName("頁簽名稱"); // 標(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中int[]與ArrayList<>數(shù)組轉(zhuǎn)換方法詳解
這篇文章主要介紹了在Java中將int[]和ArrayList進(jìn)行雙向轉(zhuǎn)換的方法,包括手動(dòng)遍歷和使用Java8?Stream?API兩種方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02Spring?Boot項(xiàng)目Jar包加密實(shí)戰(zhàn)教程
本文詳細(xì)介紹了如何在Spring?Boot項(xiàng)目中實(shí)現(xiàn)Jar包加密,我們首先了解了Jar包加密的基本概念和作用,然后學(xué)習(xí)了如何使用Spring?Boot的Jar工具和第三方庫來實(shí)現(xiàn)Jar包的加密和解密,感興趣的朋友一起看看吧2024-02-02Linux下Java Python啟動(dòng)管理腳本方便程序管理
這篇文章主要為大家介紹了Linux下Java/Python啟動(dòng)管理腳本方便程序管理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Springcloud中Feign傳遞參數(shù)的過程解析
這篇文章主要介紹了Springcloud中Feign傳遞參數(shù)的過程,單個(gè)參數(shù)的傳值有兩種方式,第一種使用@RequestParam/@PathVariable進(jìn)行傳值,傳遞多個(gè)參數(shù):多個(gè)參數(shù)的傳值可以使用多個(gè)@RequestParam來進(jìn)行傳參,需要的朋友可以參考下2023-09-09Spring Security實(shí)現(xiàn)登錄認(rèn)證實(shí)戰(zhàn)教程
這篇文章主要介紹了Spring Security實(shí)現(xiàn)登錄認(rèn)證實(shí)戰(zhàn)教程,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-06-06基于MockMvc進(jìn)行springboot調(diào)試(SpringbootTest)
這篇文章主要介紹了基于MockMvc進(jìn)行springboot調(diào)試(SpringbootTest),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10