依赖包
- 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);
}