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 import and export Excel in SpringBoot

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use POI to import and export Excel in SpringBoot. For this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

1. Create the Excel document HSSFWorkbook workbook = new HSSFWorkbook (); 2. Set the basic information of the document, this step is optional / / get document information and configure DocumentSummaryInformation dsi = workbook.getDocumentSummaryInformation (); / / document category dsi.setCategory ("employee information"); / / set document administrator dsi.setManager ("Jiangnan Rain"); / / set organization dsi.setCompany ("XXX Group"); / / get summary information and configure SummaryInformation si = workbook.getSummaryInformation () / / set document subject si.setSubject ("employee Information Table"); / / set document title si.setTitle ("employee Information"); / / set document author si.setAuthor ("XXX Group"); / / set document remarks si.setComments ("remarks not available")

This information is displayed in the details pane:

3. Create an Excel form with the name of sheet HSSFSheet sheet = workbook.createSheet ("XXX Group employee Information Table"); 4. Create a line HSSFRow headerRow = sheet.createRow (0)

0 represents the first line.

5. Create the first cell in the first row and set the data HSSFCell cell0 = headerRow.createCell (0); cell0.setCellValue ("number"); 6. Write Excel to ByteArrayOutputStream baos = new ByteArrayOutputStream (); workbook.write (baos); 7. Create ResponseEntity and return return new ResponseEntity (baos.toByteArray (), headers, HttpStatus.CREATED)

Core steps on these seven steps, of course, there are other settings cell data format, cell background, cell width, etc., we can study in the source code, here will not repeat.

Import Excel data

Data import mainly involves three steps 1. File upload; 2.Excel parsing; 3. Data insertion. The third step is relatively simple. Let's focus on the first two steps.

File upload

The Upload control in ElementUI is used for file upload, as follows:

{{fileUploadBtnText}}

When you are uploading, the file upload control is not available until the upload succeeds or fails. During the upload process, the upload button will be displayed with loading.

Then receive the uploaded file in SpringMVC:

@ RequestMapping (value = "/ importEmp", method = RequestMethod.POST) public RespBean importEmp (MultipartFile file) {/ /...} Excel resolution

Convert the uploaded MultipartFile into an input stream, and then give it to POI to parse. Can be divided into the following four steps:

1. Create HSSFWorkbook object HSSFWorkbook workbook = new HSSFWorkbook (new POIFSFileSystem (file.getInputStream (); 2. Get how many sheet there are, and then iterate through int numberOfSheets = workbook.getNumberOfSheets (); for (int I = 0; I < numberOfSheets; iTunes +) {HSSFSheet sheet = workbook.getSheetAt (I); / /...} 3. Get how many lines there are in the sheet and iterate through the lines (notice that the first line is the title) int physicalNumberOfRows = sheet.getPhysicalNumberOfRows (); Employee employee;for (int j = 0; j < physicalNumberOfRows; jacks +) {if (j = = 0) {continue;// header line} / /.} 4. Get how many cells there are in each row, traversing the cell int physicalNumberOfCells = row.getPhysicalNumberOfCells (); employee = new Employee (); for (int k = 0; k < physicalNumberOfCells; kits +) {HSSFCell cell = row.getCell (k) / /.} this is the answer to the question about how to import and export Excel using POI in SpringBoot. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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

Internet Technology

Wechat

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

12
Report