以前のプロジェクトの実施は反射により財産を取得するので、デモにも反映しているので、私はより適切なポイントである何これらの二つのかわからない、に一覧<地図>に格納されている多くのオンライン記事を参照してください、または持っていますnoにどのような良い方法。
まず導入ポンポン、オプションのロンボク島
<依存性> <のgroupId> org.apache.poi </のgroupId> <たartifactId> POI </たartifactId> <バージョン> 3.13 </バージョン> </依存> <依存性> <のgroupId> org.apache.poi </のgroupId> <たartifactId> POI-OOXML </たartifactId> <バージョン> 3.13 </バージョン> </依存> <依存性> <のgroupId> org.projectlombok </のgroupId> <たartifactId>ロンボク</たartifactId> </オプション<オプション>真> </依存関係>
エンティティ
@data @AllArgsConstructor パブリッククラスの学生{ 公共の文字列の名前。 公共int型のスコア。 公共int型の年齢; }
サービス
輸入org.springframework.stereotype.Service。 輸入はjava.util.ArrayList; 輸入はjava.util.List; @Service パブリッククラスStudentService { 公共の一覧<学生> GETLIST(学生学生、int型のインデックス、int型のサイズ){ 学生STUDENT1 =新しい学生( "张三"、90、18); 学生student2 =新しい学生( "李四"、85、17); 学生student3 =新しい学生( "王五"、70、19); 一覧<学生>リスト=新しいArrayListを<>(); list.add(STUDENT1)。 list.add(student2)。 list.add(student3)。 リストを返します。 } }
コントローラ
輸入lombok.Cleanup; 輸入org.apache.poi.ss.usermodel.Workbook; 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.http.HttpHeaders。 輸入org.springframework.http.HttpStatus。 輸入org.springframework.http.ResponseEntity。 輸入org.springframework.web.bind.annotation.RequestMapping。 輸入org.springframework.web.bind.annotation.RestController。 輸入java.io.ByteArrayOutputStreamを。 インポートにjava.io.IOException; 輸入java.io.UnsupportedEncodingException。 輸入java.net.URLEncoder。 輸入はjava.util.List; @RestController パブリッククラスStudentController { @Autowired 公共StudentService studentService。 @RequestMapping( "/ exportStudentExcel") 公衆ResponseEntity <バイト[]> exportExcel(学生学生){ リスト<学生>一覧= studentService.getList(学生、0、10);これらの行のみを変更する必要が//毎回 文字列のファイル名を=「生徒の成績の統計」;数行変更する//時間必要 文字列[]ゲッターは= {「のgetName 」、「getScore」、「getAgeを」}; // 数行を変更する各のみ必要 文字列を[]ヘッダ= { "名前"、 "スコア"、 "年齢"}; //は、すべての数行を変更する必要は = ExcelUtils.createWorkBook(リスト、ゲッターWBワークブック 、ヘッダ、student.getClass()); // 毎回わずか数行は変更する必要が @Cleanup ByteArrayOutputStream ByteArrayOutputStream新しい新しいOS =を(); 試み{ wb.write(OS); }キャッチ(IOExceptionを電子){ e.printStackTrace(); } バイト[] os.toByteArray含量=(); HttpHeaders httpHeaders =新しいHttpHeaders(); {試みる fileNameに= URLEncoder.encode(ファイル名、 "UTF-8"); }キャッチ(にUnsupportedEncodingException電子){ e.printStackTrace(); } httpHeaders.setContentDispositionFormData( "アタッチメント"は、ファイル名+ "の.xlsx")。 新しいResponseEntity <バイト[]>(コンテンツ、httpHeaders、HttpStatus.OK)を返します。 } }
ExcelUtils
ExcelUtilsはクラス{パブリック / ** * Excel文書を作成する * キー配列コレクションのマップの* @paramゲッター一覧 * @paramヘッダーは、列名をエクセル * / パブリック静的ワークブックcreateWorkBook(一覧リスト、String []型ゲッター、String []型のヘッダー、クラスclazz){ リスト<方法、> = getMethodsByStrsメソッド(ゲッター、clazz); //作成の.xlsxワークブック ワークブック新しい新しいXSSFWorkbook WBを=(); //最初のシート(ページ)を作成し、指定された シートのシート= WB .createSheet(「シート1」); //手動列幅は、列の最初のパラメータセットとして表現するように設定し、第2列は、幅パラメータを示し、nは高くなるように画素の列の数である。 (INT I = 0 ; Iはgetters.lengthを<; Iは++){ sheet.setColumnWidth((ショート)I、(ショート)(35.7 * 200です)); } //最初の行作成 行を= sheet.createRowヘッダ(0); //二つの細胞は、書式作成 CellStyle cellStyle1 wb.createCellStyle =を(); CellStyle cellStyle2 wb.createCellStyle =(); // 2フォントの作成 フォントfont1を= wb.createFont(); //タイトルフォントの フォントfont2で= wb.createFont(); //テキストのフォント //タイトル太字 font1.setBoldweight(Font.BOLDWEIGHT_BOLD); //設定された2つのセルパターン setCellStype(cellStyle1 、font1); setCellStype(cellStyle2、font2で); //セットヘッダ のための(INT I = 0; I <headers.length; Iは++){ セルCELL header.createCell =(I); cell.setCellValue(ヘッダ[I]) ; cell.setCellStyle(cellStyle1)。 } //设置データ INT headersNum = 1。 以下のために(INT i = 0; i)は、(はlist.sizeを<; iは++){ 行の行は= sheet.createRowは(iはheadersNum + 1)。 用(int型J = 0; J <methods.size(); J ++){ {試みる オブジェクトを呼び出し= methods.get(J).invoke(list.get(I))。 IF(!呼び出し= NULL){ row.createCell(J).setCellValue(invoke.toString())。 } }キャッチ(例外e){ e.printStackTrace(); } } } WBを返します。 } プライベート静的ボイドsetCellStype(CellStyle cellStyle、フォントのフォント){ font.setFontHeightInPoints((ショート)10)。 font.setColor(IndexedColors.BLACK.getIndex())。 cellStyle.setFont(フォント)。 cellStyle.setBorderLeft(CellStyle.BORDER_THIN)。 cellStyle.setBorderRight(CellStyle.BORDER_THIN)。 cellStyle.setBorderTop(CellStyle.BORDER_THIN)。 cellStyle.setBorderBottom(CellStyle.BORDER_THIN)。 cellStyle.setAlignment(CellStyle.ALIGN_CENTER)。 } プライベート静的リスト<方法> getMethodsByStrs(String []型ゲッター、クラスclazz){ リスト<方法>リスト=新規のArrayList <>(); (文字列ゲッター:ゲッター)は{ {試します list.add(clazz.getDeclaredMethod(ゲッタ))。 }キャッチ(ないNoSuchMethodException電子){ e.printStackTrace(); } } リストを返します。 } }