已经excel布局(表头排列以及样式的情况下)做指定导出
(此篇不包含利用Java反射和智能合并单元格)
1.导出excel
//step1:创建工作文档对象
HSSFWorkbook wb = new HSSFWorkbook();
//step2:新建sheet对象(参数sheet name)
Sheet sheet1 = wb.createSheet("sheet1");
//step3:合并表头
/**
*1起始行 2截止行
*3起始列 4截止列
*/
sheet1.addMergedRegion(new CellRangeAddress(1, 2, 3, 4));
//step4:设置表格布局
HSSFCellStyle cellStyle = wb.createCellStyle();
//4.1对齐方式
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//4.2字体
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
cellStyle.setFont(font);
//step5设置列宽
/**
*注意事项:
*1.param1:第几列 param2:像素(字符的1/256)
*/
sheet1.setColumnWidth(0, 256 * 13);
//step6:初始化表头内容
Row row0 = sheet1.createRow(0);
//****行高****
row0.setHeightInPoints(50.25f);
//获取cell
Cell cell11 = row0.createCell(0);
//设置样式
cell11.setCellStyle(cellStyle);
//赋值
cell11.setCellValue("合并列");
/*
*注意:和并列此时需要注释掉,后序列的数组下标不受影响
*/
//cell12.setCellValue("合并列");
cell13.setCellValue("");
//循环写入数据
for (int i = 2; i < list.size(); i++) {
if (list.get(i) == null) {
break;
}
Row row = sheet1.createRow(i);
//设置行高
row.setHeightInPoints(14.25f);
//循环写入列数据
Cell cell0 = row.createCell(0);
cell0.setCellStyle(cellStyle);
if (list.get(i).getC_mileage() != null) {
cell0.setCellValue(new HSSFRichTextString(list.get(i).getC_mileage()));
}
}
//step7:返回wb对象,完成接口下载