public class ExcelData implements Serializable {
private static final long serialVersionUID = 4444017239100620999L;
// 表头
private List titles;
// 数据
private List> rows;
// 页签名称
private String name;
public List getTitles() {
return titles;
}
public void setTitles(List titles) {
this.titles = titles;
}
public List> getRows() {
return rows;
}
public void setRows(List> rows) {
this.rows = rows;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ExportExcelUtils {
public static void exportExcel(HttpServletResponse response, String fileName, List datas) throws Exception {
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/vnd.ms-excel");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "utf-8"));
exportExcel(datas, response.getOutputStream());
}
public static void exportExcel(List datas, OutputStream out) throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
try {
for (ExcelData data : datas) {
String sheetName = data.getName();
if (null == sheetName) {
sheetName = "Sheet";
}
XSSFSheet sheet = wb.createSheet(sheetName);
writeExcel(wb, sheet, data);
}
wb.write(out);
} finally {
wb.close();
}
}
private static void writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelData data) {
int rowIndex = 0;
rowIndex = writeTitlesToExcel(wb, sheet, data.getTitles());
writeRowsToExcel(wb, sheet, data.getRows(), rowIndex);
autoSizeColumns(sheet, data.getTitles().size() + 1);
}
private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, List titles) {
int rowIndex = 0;
int colIndex = 0;
Font titleFont = wb.createFont();
titleFont.setFontName("simsun");
titleFont.setBold(true);
// titleFont.setFontHeightInPoints((short) 14);
titleFont.setColor(IndexedColors.BLACK.index);
XSSFCellStyle titleStyle = wb.createCellStyle();
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
titleStyle.setFillForegroundColor(new XSSFColor(new Color(182, 184, 192)));
titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
titleStyle.setFont(titleFont);
setBorder(titleStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
Row titleRow = sheet.createRow(rowIndex);
// titleRow.setHeightInPoints(25);
colIndex = 0;
for (String field : titles) {
Cell cell = titleRow.createCell(colIndex);
cell.setCellValue(field);
cell.setCellStyle(titleStyle);
colIndex++;
}
rowIndex++;
return rowIndex;
}
public static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, List> rows, int rowIndex) {
int colIndex = 0;
Font dataFont = wb.createFont();
dataFont.setFontName("simsun");
// dataFont.setFontHeightInPoints((short) 14);
dataFont.setColor(IndexedColors.BLACK.index);
XSSFCellStyle dataStyle = wb.createCellStyle();
// dataStyle.setAlignment(HorizontalAlignment.CENTER);
dataStyle.setAlignment(HorizontalAlignment.LEFT);
dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
dataStyle.setFont(dataFont);
setBorder(dataStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
for (List
具体使用:
ExcelData data = new ExcelData();
List> rows = new ArrayList<>();
data.setName("i18n字符串");
List title = new ArrayList<>();
title.add("字符串名称");
title.add(langEntity.getName());
title.add("审核状态");
data.setTitles(title);
for (NameValueEntity nameValueEntity : collect) {
List