// 导出标题
String [] metaData = {"标题1","标题2","标题3","标题4"};
// 导出数据
List<Map<String, Object>> dataList = data; // data 为需要导出的数据
// 生成数据列
List<String[]> data = new ArrayList<String[]>();
// 生成列数据
for(Map<String, Object> dataTemp:dataList){
String[] row = new String[4];
row[0] = flow.get("第一个字段").toString();
row[1] = flow.get("第二个字段").toString();
row[2] = flow.get("第三个字段").toString();
row[3] = flow.get("第四个字段").toString();
}
// 导出数据名称
String fileName = ”excel名称“;
exportExcel("excel数据标题", metaData, data, fileName, response);
/**
* 导出excel
* @param title
* @param metaData 表头
* @param data 数据
* @param fileName 文件名
* @param response
*
* @author : weihui
* @createTime : 2015年12月27日 下午1:24:06
*/
public static void exportExcel(String title,String [] metaData,List<String[]> data,String fileName,HttpServletResponse response){
Workbook wb = createExcel(title, metaData, data);
try {
fileName = URLEncoder.encode(fileName,"UTF-8");
response.setContentType("application/x-download");
response.addHeader("Content-Disposition", "attachment;filename="+fileName+".xls");
wb.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建Excel表格文件
* @param title 标题
* @param metaData元数据
* @param data 和元数据匹配的数据项
* @return
*/
private static Workbook createExcel(String title,String [] metaData,List<String[]> data){
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
//合并 包含序号列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, metaData.length));
sheet.setDefaultColumnWidth(14); //默认列宽
//标题行
Row rowTitle = sheet.createRow(0);
rowTitle.setHeightInPoints((short)30);//行高
//行样式
CellStyle rowStyle = wb.createCellStyle();
rowStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER); //垂直居中
rowTitle.setRowStyle(rowStyle);
Cell cellTitle = rowTitle.createCell(0);
cellTitle.setCellValue(title);
//标题行样式
CellStyle cellTitleStyle = wb.createCellStyle();
cellTitleStyle.setAlignment(CellStyle.ALIGN_CENTER);
Font font = wb.createFont();
font.setFontHeightInPoints((short)24);
cellTitleStyle.setFont(font);
cellTitle.setCellStyle(cellTitleStyle);
//数据单元格样式
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setWrapText(true);
Font fc = wb.createFont();
// fc.setFontHeightInPoints((short)12);
cellStyle.setFont(fc);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);
//创建元数据
Row rMeta = sheet.createRow(1);
//序号列
Cell cellId = rMeta.createCell(0);
cellId.setCellStyle(cellStyle);
cellId.setCellValue("序号");
sheet.setColumnWidth(0, 2000);
for(int i =0 ; i < metaData.length; i++){
String meta = metaData[i];
Cell c = rMeta.createCell(i+1);
c.setCellStyle(cellStyle);
c.setCellValue(meta);
}
//如果没有数据则直接返回
if(data == null) return wb;
//创建数据
int index = 2;
for(int i = 0,count = data.size() ; i < count ;i++){
//创建第index 行
Row rd = sheet.createRow(index+i);
//取得记录
String [] d = data.get(i);
//为每个记录插值
Cell cId = rd.createCell(0);
cId.setCellStyle(cellStyle);
cId.setCellValue(i+1);
for(int j =0 ; j < metaData.length; j++){
//创建每个单元格
Cell cd = rd.createCell(j+1);
//System.out.println(d[j]);
if(d[j] != null && d[j].contains("\n")){//控制行高
//d.setHeight((short)(15.625*2));
rd.setHeightInPoints(12.75f * (d[j].split("\n").length >= 3 ? 3 :2));
}
cd.setCellStyle(cellStyle);
cd.setCellValue(d[j]);
}
}
return wb;
}