The jar package that needs to be introduced:
<Dependency> <groupId>Org.apache.poi</groupId> <Artifactid>Poi</Artifactid> <version>3.16</version> </Dependency> <Dependency> <groupId>Org.apache.poi</groupId> <Artifactid>Poi-ooxml</Artifactid> <version>3.16</version> </Dependency>
1. Front-end
function () { var haschecked = $ (' input[name=clientoption] '). Is (': Checked '); if (! haschecked) { ' Please select at least one! ', Status: ' Confirm '}); return ; } []; $ (' input[name=clientoption]:checked '). each (function () { Values.push ($ ( this). Val ()); }) = '/admin/export?values= ' + values;})
2, back-end receive, and generate the original list data
@RequestMapping (value = "Export") Public voidexportclient (string[] values, httpservletresponse response) {Try{String title= "Title"; Map<integer, string> headermap =NewLinkedhashmap<> (3); Headermap.put (0, "Header0"); Headermap.put (1, "Header1"); Headermap.put (2, "Header2"); List<List<String>> ContentList =NewArraylist<>(); for(intindex = 0; Index < values.length; index++) {List<String> celllist =NewArraylist<>();Contentlist.add (Values[index]); } excelhelper.downloadexcel (response, title, Headermap, ContentList); } Catch(Exception e) {//Todo } }
3. Download
Public Static voidDownloadexcel (httpservletresponse response, String title, Map<integer, string> Headermap, list<list< String>>ContentList) { Try { //content written to ExcelXssfworkbook Workbook =Excelhelper.writetoexcel (title, Headermap, ContentList); //workbook writes to the output streamBytearrayoutputstream OS =NewBytearrayoutputstream (); Workbook.write (OS); //set the response parameter to open the download pageResponse.reset (); Response.setcontenttype ("Application/vnd.ms-excel;charset=utf-8"); Response.setheader ("Content-disposition", "attachment;filename=" +NewString ((title + Dateutils.todatetext (NewDate (), Dateutils.number_time_format) + ". xlsx"). GetBytes (), charset_iso_8859_1)); byte[] content =Os.tobytearray (); InputStream is=NewBytearrayinputstream (content); Servletoutputstream out=Response.getoutputstream (); Bufferedinputstream bis=NewBufferedinputstream (IS); Bufferedoutputstream Bos=NewBufferedoutputstream (out); byte[] Buff =New byte[2048]; intBytesread; while( -1! = (bytesread = bis.read (buff, 0, Buff.length))) {bos.write (buff,0, Bytesread); } //turn off the input and output streamBis.close (); Bos.close (); Out.flush (); Out.close (); } Catch(Exception e) {//Todo } }
4. List is written to the Excel object
Public StaticXssfworkbook Writetoexcel (String title, Map<integer, string> Headermap, list<list<string>>ContentList) {Xssfworkbook Workbook=NewXssfworkbook (); Try{xssfsheet sheet=Workbook.createsheet (title); //Excel style settingsXssfcellstyle TitleStyle =Workbook.createcellstyle (); Titlestyle.setalignment (HorizontalAlignment.Center); Font Titlefont=Workbook.createfont (); Titlefont.setfontheightinpoints (( Short) 12); Titlefont.setfontname ("Microsoft Yahei"); Titlefont.setbold (true); Titlestyle.setfont (Titlefont); //Write titleXssfrow row0 = Sheet.createrow (0); Xssfcell row0cell0= Row0.createcell (0); Row0cell0.setcellvalue (title); Row0cell0.setcellstyle (TitleStyle); //Excel Merge CellsCellrangeaddress region =NewCellrangeaddress (0, 0, 0, headermap.size ()-1); Sheet.addmergedregion (region); //Write principal content for(intRowIndex = 0; RowIndex < Contentlist.size (); rowindex++) {xssfrow rows= Sheet.createrow (RowIndex + 2); List<String> rowlist =Contentlist.get (RowIndex); for(intCellIndex = 0; CellIndex < Rowlist.size (); cellindex++) {Rows.createcell (cellindex). Setcellvalue (Rowlist.get (CellIndex)); } } //Write Header columnXssfrow row1 = Sheet.createrow (1); for(intHeaderindex = 0; Headerindex < Headermap.size (); headerindex++) {Row1.createcell (Headerindex). Setcellvalue (Headermap.get (Headerindex)); //Sheet.autosizecolumn (Headerindex, true);Sheet.setcolumnwidth (Headerindex, Row1.getcell (Headerindex). toString (). GetBytes (). Length * 3 * 256); } } Catch(Exception e) {//Todo } returnworkbook; }
Reference Links:
http://blog.csdn.net/renlei0109/article/details/36906409 http://bbs.csdn.net/topics/391888713?page=1http:// www.jb51.net/article/87574.htmhttp://www.cnblogs.com/crazyapple/p/5489588.htmlhttp://blog.csdn.net/houxuehan/ article/details/50960259http://blog.csdn.net/lp1791803611/article/details/52351333http://yjck.iteye.com/blog/ 1609232
Java Excel Export (POI)