:SpringELを参照することができhttps://www.cnblogs.com/yangzhilong/p/11282560.html
POI 3.1.7、春の書き込み5.1.7に基づいています。
エクセル以来#キーワードの数であり、すべての私は、#文字を置き換えるために$ {}を使用します。
コアカテゴリを助けます:
パッケージcom.longge.util。 輸入java.io.InputStreamを。 輸入はjava.util.ArrayList; 輸入はjava.util.List; 輸入org.apache.poi.ss.usermodel.Cell; 輸入org.apache.poi.ss.usermodel.CellStyle。 輸入org.apache.poi.ss.usermodel.CellType。 輸入org.apache.poi.ss.usermodel.Row。 輸入org.apache.poi.ss.usermodel.Sheet。 輸入org.apache.poi.ss.usermodel.Workbook; 輸入org.apache.poi.ss.usermodel.WorkbookFactory; 輸入org.springframework.core.io.ClassPathResource。 インポートorg.springframework.expression.EvaluationContext; 輸入org.springframework.expression.ExpressionParser。 輸入org.springframework.expression.spel.standard.SpelExpressionParser。 輸入org.springframework.expression.spel.support.StandardEvaluationContext; 輸入org.springframework.util.CollectionUtils。 / ** * @author yangzhilong * @date 2019年8月1日 * / パブリック クラスExcelTemplateWriteUtils { / ** *テンプレートワークブックをエクセル取得 * @param templateFilePath例:静的/ ExcelTemplateを/ xx.xlsx * @return * / パブリック 静的ブックgetWorkbook(文字列templateFilePath){ 試みる{ 入力ストリームです。 もし(templateFilePath.startsWith( "/" )){ ある = ExcelTemplateWriteUtils。クラス.getResourceAsStream(templateFilePath)。 } 他{ ある = 新しいClassPathResource(templateFilePath).getInputStream(); } 戻りWorkbookFactory.create(です)。 } キャッチ(例外e){ スロー 新規のRuntimeException(e)前記 } } / ** * @paramのシート * @param templateRowIndex * @paramのリスト * / パブリック 静的な 無効 fillDataWithTemplate(シートシート、int型 templateRowIndexを、リストには<?拡張オブジェクト> リスト){ int型 tIndex = templateRowIndex。 行templateRow = sheet.getRow(templateRowIndex)。 もし(CollectionUtils.isEmpty(リスト)){ sheet.removeRow(templateRow)。 } 他{ 一覧<オブジェクト>テンプレート= 新規のArrayList <> (templateRow.getPhysicalNumberOfCells())。 一覧 <CellStyle> sytles = 新しい ArrayListを<> (templateRow.getPhysicalNumberOfCells()); templateRow.forEach(セル - > { sytles.add(cell.getCellStyle()); cell.getCellStyle(); CellTypeタイプ = cell.getCellTypeEnum(); 場合(CellType.NUMERIC.equals(タイプ)){ (template.add cell.getNumericCellValue()); } そうでない 場合(CellType.STRING.equals(タイプ)){ 文字列値 =cell.getStringCellValue(); もし(value.startsWith( "$ {" )){ 値 = formatEl(値)。 } template.add(値)。 } そう であれば(CellType.BLANK.equals(タイプ)){ template.add(NULL )。 } 他{ スロー 新しい( "セルタイプをサポートすることはできません:" +のRuntimeExceptionをtype.toString()); } })。 ExpressionParserパーサー = 新しいです SpelExpressionParser()。 用(int型 ; iがLEN <I ++はI = 0、LEN =はlist.size(){}) オブジェクトアイテム = list.get(I)。 行newRow = sheet.createRow(tIndex ++ )。 EvaluationContextコンテキスト = 新しいStandardEvaluationContext(); context.setVariable( "アイテム" 、項目)。 ForEachUtils.forEach( 0、テンプレート、(インデックス値) - > { 細胞newCell = newRow.createCell(インデックス) newCell.setCellStyle(sytles.get(インデックス))。 もし(ヌル!=数){値){ 場合(値のinstanceof 文字列){ 文字列V = (文字列)値。 もし(v.startsWith( "#" )){ V = parser.parseExpression(V)以降.getvalue(文脈、文字クラス)。 } newCell.setCellValue(V)。 } そう であれば(値のinstanceof } } newCell.setCellValue(((ナンバー)値).doubleValue())。 }); } } } プライベート 静的な文字列formatEl(文字エル){ 戻り el.replace( "$ {"、 "#")を置き換えます( "}"、 ""。); } }
ユニットテストクラス:
パッケージcom.longge.util。 インポート 静的org.junit.Assert.assertEqualsを。 インポートのjava.io.File; 輸入java.io.FileOutputStreamは、 輸入はjava.util.ArrayList; 輸入java.util.HashMapを; 輸入はjava.util.List; 輸入java.util.Map; 輸入java.util.UUID。 輸入org.apache.poi.ss.usermodel.Sheet。 輸入org.apache.poi.ss.usermodel.Workbook; 輸入org.junit.Test; 輸入com.nike.cig.dto.CustomerDto; / ** * @author yangzhilong * @dateの2019年8月1日 * / パブリック クラスExcelTemplateWriteUtilsTestは{ @Test 公共 ボイド試験()スロー例外{ 文字列のパス = "UTIL / template.xlsx" 。 // 文字列のパス=「/util/template.xlsx」。 ブックブック= ExcelTemplateWriteUtils.getWorkbook(パス)。 シートのシート = workbook.getSheetAt(0 )。 一覧 <CustomerDto>リスト= 新しい ArrayListを<>(10 )。 用(int型 ; iが10 <I ++はiが0 = {) CustomerDto DTO = 新しいですCustomerDto(); dto.setChineseName( "名:" + I)。 dto.setCustomerCode( "コード:" + I)。 dto.setOwnerGroup( "グループ:" + I)。 list.add(DTO)。 } ExcelTemplateWriteUtils.fillDataWithTemplate(シート 1 、リスト)。 assertEquals(sheet.getPhysicalNumberOfRows()、 11 )。 workbook.write(新しいのFileOutputStream(新しいファイル( "C:\\ユーザー\\ RYan27 \\デスクトップ\\テスト\\"。+ UUID.randomUUID()のtoString()+ "の.xlsx" ))); workbook.close(); } }
テンプレートファイル:
最終文書を例に生成されます。