How to use poi to export data in springboot
This article will explain in detail how springboot uses poi to export data. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Idea+restful style is used.
First, introduce dependencies as follows:
Org.apache.xmlbeans xmlbeans 2.6.0 dom4j dom4j 1.6.1 org.apache.poi poi 3.9 org.apache.poi poi-ooxml 3.9
Step 2:
Utility class for uploading files
Import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell Import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.ss.usermodel.IndexedColors;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook / * * @ author qingmu * @ version creation time: 4:53:29 on December 28, 2017 * Class description: POI export utility class * / public class ExportPOIUtils {/ / Parameter description: fileName: file name projects: object collection columnNames: column name keys: key public static void start_download (HttpServletResponse response, String fileName, List projects, String [] columnNames in map String [] keys) throws IOException {/ / corresponds the properties of objects in the collection to List List list=createExcelRecord (projects, keys) ByteArrayOutputStream os = new ByteArrayOutputStream (); try {/ / download the converted Workbook object createWorkBook (list,keys,columnNames) .write (os) via stream;} catch (IOException e) {e.printStackTrace ();} byte [] content = os.toByteArray (); InputStream is = new ByteArrayInputStream (content); / / set the response parameter to open the download page response.reset (); response.setContentType ("application/vnd.ms-excel;charset=utf-8") Response.setHeader ("Content-Disposition", "attachment;filename=" + new String ((fileName + ".xls"). GetBytes (), "iso-8859-1"); ServletOutputStream out = response.getOutputStream (); BufferedInputStream bis = null; BufferedOutputStream bos = null; try {bis = new BufferedInputStream (is); bos = new BufferedOutputStream (out); byte [] buff = new byte [2048]; int bytesRead; while (- 1! = (bytesRead = bis.read (buff, 0, buff.length)) {bos.write (buff, 0, bytesRead) } catch (final IOException e) {throw e;} finally {if (bis! = null) bis.close (); if (bos! = null) bos.close ();} private static List createExcelRecord (List projects, String [] keys) {List listmap = new ArrayList (); Map map = new HashMap (); map.put ("sheetName", "sheet"); listmap.add (map); Object project=null; for (int j = 0; j < projects.size (); jacks +) {project=projects.get (j) Map mapValue = new HashMap (); for (int item0; I