In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.