1、一些常量
package com. dsanjun. poi. constant;
public class Constants {
public static String EXCEL_SUFFIX_07 = "xlsx" ;
public static String EXCEL_SUFFIX_03 = "xls" ;
}
2、Excel后缀名错误异常
package com. dsanjun. poi. exception;
public class NoSuchExcelSuffixException extends RuntimeException {
private static final long serialVersionUID = 7849681800154791175 L;
public static final String MESSAGE = "Excel form suffix name error" ;
public NoSuchExcelSuffixException ( ) {
super ( MESSAGE ) ;
}
}
3、列标题对象
package com. dsanjun. poi. create;
public class Title {
private Integer index;
private String name;
public Integer getIndex ( ) {
return index;
}
public void setIndex ( Integer index) {
this . index = index;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
}
4、Excel表格数据对象
package com. dsanjun. poi. create;
import java. util. List;
import java. util. Map;
public class ExcelData {
private List< Title> titleList;
private List< Map< String, Object>> content;
public List< Title> getTitleList ( ) {
return titleList;
}
public void setTitleList ( List< Title> titleList) {
this . titleList = titleList;
}
public List< Map< String, Object>> getContent ( ) {
return content;
}
public void setContent ( List< Map< String, Object>> content) {
this . content = content;
}
}
5、创建表单(链式构造器)
package com. dsanjun. poi. create;
import java. text. SimpleDateFormat;
import java. util. Date;
import java. util. List;
import java. util. Map;
import org. apache. poi. hssf. usermodel. HSSFWorkbook;
import org. apache. poi. ss. usermodel. Cell;
import org. apache. poi. ss. usermodel. Row;
import org. apache. poi. ss. usermodel. Sheet;
import org. apache. poi. ss. usermodel. Workbook;
import org. apache. poi. xssf. usermodel. XSSFWorkbook;
import com. dsanjun. poi. constant. Constants;
import com. dsanjun. poi. exception. NoSuchExcelSuffixException;
public class ExcelCreater {
private Workbook workbook;
public Workbook create ( ) {
return workbook;
}
public static void createRows ( Sheet sheet, ExcelData excelData) {
List< Title> titleList = excelData. getTitleList ( ) ;
List< Map< String, Object>> content = excelData. getContent ( ) ;
createExcelTilte ( sheet, titleList) ;
if ( content != null && content. size ( ) > 0 ) {
for ( int i = 0 ; i < content. size ( ) ; i++ ) {
createRow ( sheet, i + 1 , titleList, content. get ( i) ) ;
}
}
}
public static void createExcelTilte ( Sheet sheet, List< Title> titleList) {
Row titleRow = sheet. createRow ( 0 ) ;
for ( Title title : titleList) {
Cell titleCell = titleRow. createCell ( title. getIndex ( ) ) ;
titleCell. setCellValue ( title. getName ( ) ) ;
}
}
public static void createRow ( Sheet sheet, int rowIndex, List< Title> titleList, Map< String, Object> rowData) {
for ( int i = 0 ; i < rowData. size ( ) ; i++ ) {
Row contentRow = sheet. createRow ( rowIndex) ;
for ( int j = 0 ; j < titleList. size ( ) ; j++ ) {
Title headTitle = titleList. get ( j) ;
String headerName = headTitle. getName ( ) ;
Object data = rowData. get ( headerName) ;
if ( data instanceof String ) {
contentRow. createCell ( j) . setCellValue ( ( String) data) ;
} else if ( data instanceof Double ) {
contentRow. createCell ( j) . setCellValue ( ( Double) data) ;
} else if ( data instanceof Date ) {
String formatStr = "yyyy年MM月dd日 HH:mm:ss" ;
SimpleDateFormat format = new SimpleDateFormat ( formatStr) ;
contentRow. createCell ( j) . setCellValue ( format. format ( ( Date) data) ) ;
} else if ( data instanceof Long ) {
contentRow. createCell ( j) . setCellValue ( ( Long) data) ;
} else if ( data instanceof Float ) {
contentRow. createCell ( j) . setCellValue ( ( Float) data) ;
} else if ( data instanceof Integer ) {
contentRow. createCell ( j) . setCellValue ( ( Integer) data) ;
}
}
}
}
public ExcelCreater initSheet ( String sheetname) {
workbook. createSheet ( sheetname) ;
return this ;
}
public ExcelCreater initSheet ( ) {
workbook. createSheet ( ) ;
return this ;
}
public ExcelCreater initWorkbook ( String suffix) {
if ( ! Constants. EXCEL_SUFFIX_03 . equals ( suffix) && ! Constants. EXCEL_SUFFIX_07 . equals ( suffix) ) {
throw new NoSuchExcelSuffixException ( ) ;
}
if ( Constants. EXCEL_SUFFIX_03 . equals ( suffix) ) {
workbook = new HSSFWorkbook ( ) ;
}
if ( Constants. EXCEL_SUFFIX_07 . equals ( suffix) ) {
workbook = new XSSFWorkbook ( ) ;
}
return this ;
}
}
6、对外工具
package com. dsanjun. poi;
import org. apache. poi. ss. usermodel. Workbook;
import com. dsanjun. poi. create. ExcelCreater;
import com. dsanjun. poi. create. ExcelData;
public class POIExcelUtils {
public static Workbook workbook ( String suffix, ExcelData excelData) {
Workbook workbook = new ExcelCreater ( ) . initWorkbook ( suffix) . initSheet ( ) . create ( ) ;
ExcelCreater. createRows ( workbook. getSheetAt ( 0 ) , excelData) ;
return workbook;
}
}