Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use poi to generate excel for java

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "java how to use poi to generate excel" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "java how to use poi to generate excel" article can help you solve the problem.

Generating excel using poi usually involves the following steps

Create a workbook

Create a sheet

Create a Row object

Create a cell object (1 row+1 cell to form a cell)

Set cell contents

Set the cell style. Is the font size bold?

Save

Close the stream object

Generate a workbook

More than 2010 format uses XSSFWorkBook object, 2003 format uses HSSFWorkBook object, other object operation is basically the same.

Generate 2003 format 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 (Song style); font.setFontHeightInPoints ((short) 12); cellStyle.setFont (font) HSSFSheet sheet = workbook.createSheet ("Sheet1"); / / set cell width 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 ("serial number"); cell0.setCellStyle (cellStyle); Cell cell1 = row0.createCell (1) Cell1.setCellValue ("name"); Cell cell2 = row0.createCell (2); cell2.setCellValue ("score"); OutputStream os = null; try {os = new FileOutputStream ("d:\\ test generated 2003.xls"); workbook.write (os); os.close ();} catch (Exception e) {e.printStackTrace () }} generate more than 2010 format @ Testpublic 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 ("Arial"); font.setFontHeightInPoints ((short) 12); cellStyle.setFont (font) XSSFSheet sheet = workbook.createSheet ("Sheet1"); Row row0 = sheet.createRow (0); Cell cell0 = row0.createCell (0); cell0.setCellValue ("serial number"); cell0.setCellStyle (cellStyle); Cell cell1 = row0.createCell (1); cell1.setCellValue ("name"); Cell cell2 = row0.createCell (2); cell2.setCellValue ("grade"); OutputStream os = null Try {os = new FileOutputStream ("d:\\ test generation 2010.xlsx"); workbook.write (os); os.close ();} catch (Exception e) {e.printStackTrace ();}} merge cells

Merging cells is a common scene in generating excel. Usually, the cells are merged first, the cell contents are centered, and the cell border is set.

Poi merge cells using CellRangeAddress class, the constructor includes four parameters firstRow, lastRow, firstCol, lastCol according to their own needs to pass rows and columns.

Public CellRangeAddress (int firstRow, int lastRow, int firstCol, int lastCol) {}

Setting the border after merging cells poi has provided the RegionUtil static class, which can be used directly.

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); set cell style

Left and right center, upper and lower center, automatic line wrapping

CellStyle.setAlignment (HorizontalAlignment.CENTER); cellStyle.setVerticalAlignment (VerticalAlignment.CENTER); cellStyle.setWrapText (true)

Export excel using SpringMVC/SpringBoot

@ Controller@GetMapping ("/ excel2003") public void excel2003 (HttpServletResponse httpServletResponse) {try {/ / 2010 formatting / / response.setContentType ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); / / 2003 formatting response.setContentType ("application/vnd.ms-excel"); httpServletResponse.addHeader ("Content-Disposition", "attachment;fileName=" + URLEncoder.encode ("student transcripts .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 ("Song style"); font.setFontHeightInPoints ((short) 12) CellStyle.setFont (font); HSSFSheet sheet = workbook.createSheet ("Sheet1"); Row row0 = sheet.createRow (0); Cell cell0 = row0.createCell (0); cell0.setCellValue ("serial number"); cell0.setCellStyle (cellStyle); Cell cell1 = row0.createCell (1); cell1.setCellValue ("name"); Cell cell2 = row0.createCell (2) Cell2.setCellValue ("grades"); workbook.write (outputStream);} catch (Exception e) {e.printStackTrace ();} this is the end of the introduction on "how java uses poi to generate excel". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report