Java用策略模式優雅地匯出Excel
依賴包
- common-lang
- poi
ExcelExport介面
public interface ExcelExport {
boolean printHeader();//是否列印表頭
Map<String, Column> getHeaderMap();//返回bean欄位和列名的對映
String getOutputFilePath();//excel輸出路徑
int getStartRowIndex();//從第幾行開始列印
String getOutputFileName();//excel檔名
String getSheetName();//表名
CellStyle getHeaderCellStyle(XSSFWorkbook workbook);//表頭的格式
//add more if necessary
}
export函式
public static<T> void export(List<T> list, ExcelExport export) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
workbook.createCellStyle();
XSSFSheet sheet = workbook.createSheet(export.getSheetName());
Map<String, Column> headerMap = export.getHeaderMap();
int rowIndex = export.getStartRowIndex() - 1;
if(!export.printHeader()) {
rowIndex--;
}else {
XSSFRow row = sheet.createRow(rowIndex);
Object[] colNames = headerMap.values().toArray();
for (int colIndex = 0; colIndex < colNames.length; colIndex++) {
XSSFCell cell = row.createCell(colIndex);
Column column = (Column)colNames[colIndex];
sheet.setColumnWidth(colIndex, column.getWidth());
cell.setCellStyle(export.getHeaderCellStyle(workbook));
cell.setCellValue(((Column)colNames[colIndex]).getColumnDisplayName());
}
}
for (int listIndex = 0; listIndex < list.size(); listIndex++) {
int colIndex = 0;
T rowObj = list.get(listIndex);
XSSFRow row = sheet.createRow(++rowIndex);
for (String colField : headerMap.keySet()) {
Object colVaule = PropertyUtils.getProperty(rowObj, colField);
XSSFCell cell = row.createCell(colIndex);
cell.setCellValue(colVaule.toString());
colIndex++;
}
}
FileOutputStream fos = new FileOutputStream(export.getOutputFilePath());
workbook.write(fos);
fos.flush();
fos.close();
}
抽象ExcelExport類
主要作用是設定預設值(行為)
public abstract class AbstractExcelExport implements ExcelExport {
private boolean printHeader = true;
private int startRowIndex = 0;
private String outputFilePath = "d://";
private String sheetName = "default sheet name";
private String outputFileName = "default file name";
public boolean printHeader() {
return printHeader;
}
public void setPrintHeader(boolean printHeader) {
this.printHeader = printHeader;
}
public int getStartRowIndex() {
return startRowIndex;
}
public void setStartRowIndex(int startRowIndex) {
this.startRowIndex = startRowIndex;
}
public String getOutputFilePath() {
return outputFilePath;
}
public void setOutputFilePath(String outputFilePath) {
this.outputFilePath = outputFilePath;
}
public String getSheetName() {
return sheetName;
}
public void setSheetName(String sheetName) {
this.sheetName = sheetName;
}
public String getOutputFileName() {
return outputFileName;
}
public void setOutputFileName(String outputFileName) {
this.outputFileName = outputFileName;
}
}
具體實現類
繼承自抽象類,實現介面
public class CourseExport extends AbstractExcelExport {
@Override
public LinkedHashMap<String, Column> getHeaderMap() {
return new LinkedHashMap<String, Column>(){{
put("id", new Column("Course Id", 10000));
put("name", new Column("Course Name", 10000));
}};
}
@Override
public CellStyle getHeaderCellStyle(XSSFWorkbook workbook) {
CellStyle cs = workbook.createCellStyle();
Font titleFont = workbook.createFont();
titleFont.setFontHeightInPoints((short) 18);
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
cs.setAlignment(CellStyle.ALIGN_CENTER);
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
cs.setFont(titleFont);
return cs;
}
}
測試程式碼
public static void testExportExcel() throws Exception {
Course c1 = new Course("WSDFERHI_43DFG", "Math");
Course c2 = new Course("DFOIDOIH_345IS", "Mandarin");
List<Course> ca = new ArrayList<>();
ca.add(c1);
ca.add(c2);
CourseExport ex = new CourseExport();
ex.setPrintHeader(false);
ex.setStartRowIndex(1);
ExcelUtils.export(ca, ex);
}
相關文章
- Excel優雅匯出Excel
- 如何優雅的匯出ExcelExcel
- 更優雅地實現策略模式模式
- 如何用Java語言優雅地匯出Word文件Java
- 打造炫酷效果:用Java優雅地製作Excel迷你圖JavaExcel
- SpringBoot實現Excel匯入匯出,效能爆表,用起來夠優雅!Spring BootExcel
- Java匯出ExcelJavaExcel
- Java 通過Xml匯出Excel檔案,Java Excel 匯出工具類,Java匯出Excel工具類JavaXMLExcel
- java匯出Excel定義匯出模板JavaExcel
- 設計模式:如何優雅地使用工廠模式設計模式
- laravel 推薦優雅匯出pdfLaravel
- 轉java操作excel匯入匯出JavaExcel
- SpringBoot 實現 excel 全自由匯入匯出,效能強的離譜,用起來還特優雅Spring BootExcel
- java匯出Excel檔案JavaExcel
- java poi 匯出excel加密JavaExcel加密
- PHP 匯出 Excel 的優化PHPExcel優化
- 設計模式:如何優雅地使用責任鏈模式設計模式
- java 匯入匯出Excel工具類ExcelUtilJavaExcel
- 如何優雅地列印一個Java物件?Java物件
- 在Java中如何優雅地判空Java
- poi 匯出Excel java程式碼ExcelJava
- 記一次 Java 匯出大批量 Excel 優化JavaExcel優化
- Java之POI操作Excel表-匯入匯出JavaExcel
- 關於java中Excel的匯入匯出JavaExcel
- Java:匯出Excel大批量資料的優化過程JavaExcel優化
- 優雅地寫cssCSS
- 匯出excelExcel
- java使使用者EasyExcel匯入匯出excelJavaExcel
- 如何優雅地停止 Spring Boot 應用?Spring Boot
- 用php把資料匯出excelPHPExcel
- 百萬級別資料Excel匯出優化Excel優化
- 如何優雅地使用 macOSMac
- 如何優雅地使用 GitGit
- 使用策略模式優雅引用第三方框架模式框架
- vue excel匯入匯出VueExcel
- js匯出EXCELjs匯出EXCELJSExcel
- React如何優雅地寫單頁面應用?React
- PHP 匯出 ExcelPHPExcel