Java PoiはExcelをエクスポートし、クライアントにダウンロードします。特定のコンテンツは次のとおりです
他のファイル形式の依存関係を含むMaven構成が投稿されています
<Dependency> groupId> org.apache.poi </groupid> <artifactid> poi-excelant </artifactid> <version> 3.12 </version> </dependency> <deprency> groupid> org.apache.poi </groupId> <artifactid> poi-scratchpad </deponifactid> </</</</</</</</</< <groupid> org.apache.poi </groupid> <artifactid> poi-ooxml </artifactid> <bersion> 3.8 </version> </dependency> <依存関係> groupid> org.apache.poi </groupid> <artifactid> poi-ooxml-schemas </artifactid> </>>>>>> </>>> </バージョン
サービスレイヤー
@Override public void Export(long sblsh、string excelname、outputstream out){//最初のステップは、Excelファイルhssfworkbook wb = new hssfworkbook()に対応するWebbookを作成することです。 //テーブルを生成するhssfsheetシート= wb.createsheet(excelname); // 3番目のステップは、シートにテーブルヘッダーの0番目の行を追加することですhssfrow row = sheet.createrow(0); // 4番目のステップは、セルを作成し、値テーブルヘッダーを設定して、テーブルヘッダーを中央に設定することです。 style.setalignment(hssfcellstyle.align_center); //中心形式を作成しますhssfcellセル= row.createcell(0); Cell.SetCellStyle(スタイル); byte kjzz = qyjbxxmapper.getkjzz(sblsh);リスト<a> record = this.selectbysblsh(sblsh); this.insertdata(wb、sheet、row、record、out); }} catch(Exception E){logger.info(e.getMessage()); }} / ***データをテーブルにインポート* @param wb execl file* @paramシートテーブル* @param列のテーブルrow* @param Record data exported* @param out output stream* / private void insertdata(hssfworkbook wb、hssfsheet sheet、hssfrow row、sheet <<a <<auttremow、low {1); for(int i = 0; i <title.length; i ++){row.createCell(i).setCellValue(title [i]); } for(int i = 0; i <record.size(); i ++){row = sheet.createrow(i+2); data = record.get(i); row.createcell(0).setCellValue(data.gethc()); row.createCell(1).setCellValue(data.getXm()); bigdecimal je = data.getje(); if(je!= null){row.createcell(2).setCellValue(je.doublevalue()); }} //セルのマージ、最初の2桁は開始列と端の行を表し、最後の2桁は開始と端の列を表します。 Sheet.AddmerGedRegion(地域); wb.write(out); out.flush(); out.close(); wb.close(); } catch(Exception E){logger.info(e.getMessage()); }}コントローラ
@RequestMapping( "/export")public void export(long sblsh、httpservletrequest request、httpservletresponse response){respons.setcontenttype( "octets/stream"); string excelname = "filename"; try {Response.Addheader( "Content-Disposition"、 "Attachment; filename ="+new String(excelname.getBytes( "gb2312")、 "iso8859-1")+"。xls"); outputStream out = response.getOutputStream(); aservice.export(sblsh、excelname、out); } catch(Exception e){e.printstacktrace(); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。