public static HSSFWorkbook createBook(String title,List<Map<String,Object>> headers,List<Map<String,String>> data ){
HSSFWorkbook workbook =new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(title);
//设置表格默认列宽
sheet.setDefaultColumnWidth(15);
//表格标题行
HSSFRow row = sheet.createRow(0);
int i = 0;
for(Map<String,Object> header : headers){
for(String s : header){
HSSFCell cell = row.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(header.get(s).toString);
cell.setCellValue(text);
i++;
}
}
//表格数据行
int index = 0;
for(Map<String,Object> map : data){
int j = 0;
index++;
row.sheet.createRow(index);
for(String s : map.keySet){
HSSFCell cell = row.creareCell(j);
Object value = null == map.get(s) ? "" : map.get(s);
HSSFRichTextString text = new HSSFRichTextString(value.toString());
j++;
}
}
return workbook;
}
创建表格文件(java)
于 2023-11-15 17:07:07 首次发布