定义代码表转换
private static final Map<String,String> codeMap = new HashMap<String, String>();
static{
codeMap.put("prsc","BSIN_PRSC");
codeMap.put("rstp","BSIN_RSTP");
}
1、查询出要导出的数据集合
//查询返回数据 - list集合
List<zcdjxxCxExcelVo> list = dsmSmBsinService.selectExcel(vo);
//代码表转换,把需要转代码的字段的值转成对应的汉字
List<zcdjxxCxExcelVo> resuleList = new ArrayList<zcdjxxCxExcelVo>();
for(zcdjxxCxExcelVo voL :list){
sysCommonService.getLabelByValue(voL,codeMap);
resuleList.add(voL);
}
2、设置导出excel路径
//导出的文件路径
String filePath="D:\\excel.xls";
3、创建excel文件
//创建Excel文件(Workbook)
HSSFWorkbook workbook = new HSSFWorkbook();
4、设置单元格样式
//设置单元格样式
HSSFCellStyle styleYs = workbook.createCellStyle();
styleYs.setAlignment(HSSFCellStyle.ALIGN_LEFT);
5、创建第一个sheet页
//创建工作表(Sheet)
HSSFSheet sheet = workbook.createSheet("sheet页1");
6、设置单元格跨度
//设置单元格跨度
//10代表这个sheet页里有多少列数据
for (int i = 0; i<= 5; i++){
sheet.setColumnWidth(i,20*256);
}
7、设置表头数据
//表头数据
HSSFRow rowb = sheet2.createRow(0);// 创建行,从0开始
rowb.createCell(0).setCellValue("序号");
rowb.createCell(1).setCellValue("第一列");
rowb.createCell(2).setCellValue("第二列");
rowb.createCell(3).setCellValue("第三列");
rowb.createCell(4).setCellValue("第四列");
rowb.createCell(5).setCellValue("第五列");
8、填充单元格数据
for (int j = 0;j<resuleList.size();j++){
HSSFRow row3 = sheet3.createRow(j+1);// 创建行,从0开始
//设置序号自增
HSSFCell cell1 = row3.createCell(0);
cell1.setCellValue(j + 1);
cell1.setCellStyle(styleYs);
HSSFCell cell2 = row3.createCell(1);
cell2.setCellValue(resuleList.get(j).getRsnm());
cell2.setCellStyle(styleYs);
if (resuleList.get(j).getFpstnm() != null) {
HSSFCell cell = row11.createCell(2);
cell.setCellValue(resuleList.get(j).getFpstnm());
cell.setCellStyle(styleYs);
}
if (resuleList.get(j).getGyjzwss() != null) {
HSSFCell cell = row11.createCell(3);
cell.setCellValue(resuleList.get(j).getGyjzwss());
cell.setCellStyle(styleYs);
}
if (resuleList.get(j).getMnsz35() != null) {
HSSFCell cell = row11.createCell(4);
cell.setCellValue(resuleList.get(j).getMnsz35());
cell.setCellStyle(styleYs);
}
if (resuleList.get(j).getMaxfl() != null) {
HSSFCell cell = row11.createCell(5);
cell.setCellValue(resuleList.get(j).getMaxfl());
cell.setCellStyle(styleYs);
}
}
导出多个sheet页重复以上步骤