您好,登錄后才能下訂單哦!
這篇文章主要介紹“java怎么使用poi生成excel”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“java怎么使用poi生成excel”文章能幫助大家解決問題。
創(chuàng)建一個工作簿
創(chuàng)建一個sheet
創(chuàng)建一個Row對象
創(chuàng)建一個cell對象(1個row+1個cell構(gòu)成一個單元格)
設(shè)置單元格內(nèi)容
設(shè)置單元格樣式. 字體 字體大小 是否加粗
保存
關(guān)閉流對象
2010以上格式使用XSSFWorkBook對象, 2003格式使用HSSFWorkBook對象, 其他對象操作基本一樣.
public void test1() { HSSFWorkbook workbook = new HSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); Font font = workbook.createFont(); font.setFontName("宋體"); font.setFontHeightInPoints((short) 12); cellStyle.setFont(font); HSSFSheet sheet = workbook.createSheet("Sheet1"); //設(shè)置單元格寬度 sheet.setColumnWidth(0, 30 * 256); sheet.setColumnWidth(1, 30 * 256); sheet.setColumnWidth(2, 30 * 256); Row row0 = sheet.createRow(0); Cell cell0 = row0.createCell(0); cell0.setCellValue("序號"); cell0.setCellStyle(cellStyle); Cell cell1 = row0.createCell(1); cell1.setCellValue("姓名"); Cell cell2 = row0.createCell(2); cell2.setCellValue("成績"); OutputStream os = null; try { os = new FileOutputStream("d:\\測試生成2003.xls"); workbook.write(os); os.close(); } catch (Exception e) { e.printStackTrace(); } }
@Test public void test2() { XSSFWorkbook workbook = new XSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); Font font = workbook.createFont(); font.setFontName("宋體"); font.setFontHeightInPoints((short) 12); cellStyle.setFont(font); XSSFSheet sheet = workbook.createSheet("Sheet1"); Row row0 = sheet.createRow(0); Cell cell0 = row0.createCell(0); cell0.setCellValue("序號"); cell0.setCellStyle(cellStyle); Cell cell1 = row0.createCell(1); cell1.setCellValue("姓名"); Cell cell2 = row0.createCell(2); cell2.setCellValue("成績"); OutputStream os = null; try { os = new FileOutputStream("d:\\測試生成2010.xlsx"); workbook.write(os); os.close(); } catch (Exception e) { e.printStackTrace(); } }
合并單元格在生成excel中算常見的一個場景, 通常先合并單元, 單元格內(nèi)容居中,并設(shè)置單元格邊框.
poi合并單元格使用CellRangeAddress類, 構(gòu)造函數(shù)包括4個參數(shù)firstRow, lastRow, firstCol, lastCol根據(jù)自己需要傳入行和列.
public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) { }
合并單元格后設(shè)置邊框poi已提供了RegionUtil靜態(tài)類, 可直接使用.
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2); sheet.addMergedRegion(region); RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet); RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet); RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet); RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);
左右居中 上下居中 自動換行
cellStyle.setAlignment(HorizontalAlignment.CENTER); cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); cellStyle.setWrapText(true);
使用SpringMVC/SpringBoot導(dǎo)出excel
@Controller @GetMapping("/excel2003") public void excel2003(HttpServletResponse httpServletResponse){ try { //2010格式設(shè)置 //response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //2003格式設(shè)置 response.setContentType("application/vnd.ms-excel"); httpServletResponse.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("學生成績單.xls", "utf-8")); ServletOutputStream outputStream = httpServletResponse.getOutputStream(); HSSFWorkbook workbook = new HSSFWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); Font font = workbook.createFont(); font.setFontName("宋體"); font.setFontHeightInPoints((short) 12); cellStyle.setFont(font); HSSFSheet sheet = workbook.createSheet("Sheet1"); Row row0 = sheet.createRow(0); Cell cell0 = row0.createCell(0); cell0.setCellValue("序號"); cell0.setCellStyle(cellStyle); Cell cell1 = row0.createCell(1); cell1.setCellValue("姓名"); Cell cell2 = row0.createCell(2); cell2.setCellValue("成績"); workbook.write(outputStream); } catch (Exception e) { e.printStackTrace(); } }
關(guān)于“java怎么使用poi生成excel”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。