工具类:
public HSSFWorkbook exportExcelByList(HSSFWorkbook workbook, int sheetNum,String title, String[] headers, String[] columNames,
List<Map<String,Object>> dataset,String pattern){
// 生成一个表格
HSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(sheetNum,title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
for (short i = 0; i < headers.length; i++){
cell = row.createCell(i);
HSSFFont font = (HSSFFont) workbook.createFont();
font.setFontHeightInPoints((short) 12); // 字体高度
font.setFontName("宋体"); // 字体
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 宽度加粗
HSSFCellStyle style = (HSSFCellStyle) workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
style.setFont(font);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
// 遍历集合数据,产生数据行
int index = 0;
for(int ii = 0;ii<dataset.size();ii++){
index++;
row = sheet.createRow(index);
Map<String,Object> map = dataset.get(ii);
for (short i = 0; i < columNames.length; i++){
cell = row.createCell(i);
try{
Object value = map.get(columNames[i]);
String textValue = null;
// 判断值的类型后进行强制类型转换
if(null != value){
if (value instanceof Date){
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
textValue = sdf.format(date);
}else{
// 其它数据类型都当作字符串简单处理
textValue = value.toString();
}
}
// 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
if (textValue != null){
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Matcher matcher = p.matcher(textValue);
if (matcher.matches()){
// 是数字当作double处理
cell.setCellValue(Double.parseDouble(textValue));
}else{
HSSFRichTextString richString = new HSSFRichTextString(textValue);
cell.setCellValue(richString);
}
}else{
textValue="";
}
}catch (Exception e){
e.printStackTrace();
}
}
}
return workbook;
}
action类:
public String exportBiologicalAnalysis(){
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
ExportExcel<Sample> ex = new ExportExcel<Sample>();
int total=biologicalAnalysisService.findCount(conditions);
List<Map<String,Object>> biologicalAnalysis=new ArrayList<Map<String,Object>>();
if(total>0){
biologicalAnalysis=biologicalAnalysisService.find(conditions, 0, total);
}
String[] headers = {"批次","源序号","代码","文库名称(唯一标识)","文库编号","barcode","实验使用芯片","捕获文库名称","data/G","实验设计","优先级","分析范围(程序生成)","实验备注","上机时间","任务日期","采血类型","检测项目","产品线","任务备注","其他说明","合作机构","姓名","姓名(系统修正)","性别","年龄","家系号","与先证者关系","患者/轻微表型/正常","客户单位","科室","客户名字","样本类型","科研目的"};
String[] columNames = {"batchNo","resourceCode","seqCode","libraryName","libraryCode","barcode","chip","captureLibrary","seqData","design","priority","analysisRange","seqRemark","sequencTime","taskDate","diccode","examname","classescode","taskRemark","otherRemark","insName","checkerName","amendName","gender","age","familyCode","relativeCode","diseaseCondition","hospitalName","departmentName","doctorName","checkType","scientificPurposes"};
try {
workbook = ex.exportExcelByList(workbook,0,"生物分析",headers,columNames, biologicalAnalysis);
ByteArrayOutputStream output = new ByteArrayOutputStream();
workbook.write(output);
byte[] ba = output.toByteArray();
excelFile = new ByteArrayInputStream(ba);
output.flush();
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
poi 导出excel(传入map类型)
最新推荐文章于 2024-05-11 09:47:24 发布