使用jxl动态合并单元格

使用jxl动态合并单元格_第1张图片

public List exportExcel(Domain domain, Finder finder) throws Exception {

SqlSession sqlSession = null;
try {
sqlSession = getSqlSessionFactory(domain).openSession();
KpDao dao = sqlSession.getMapper(KpDao.class);
List> res = dao.findCriterion(finder);
this.queryCriterionAttr(dao, res);
System.out.println(res.size());
if (res != null && res.size() > 0) {
String author = res.get(0).get("author").toString();
// 1:创建excel文件
String filePath = "E:/test.xls";
File file = new File(filePath);
file.createNewFile();
// 2:创建工作簿
WritableWorkbook workbook = Workbook.createWorkbook(file);
// 3:创建sheet,设置第二三四..个sheet,依次类推即可
WritableSheet sheet = workbook.createSheet(author, 0);
// 合并单元格
sheet.mergeCells(0, 0, 3, 0);
sheet.mergeCells(0, 1, 0, 2);
sheet.mergeCells(1, 1, 2, 1);
sheet.mergeCells(3, 1, 3, 2);
// 设置单元格宽度
sheet.setColumnView(0, 10); // 设置第1列宽度为40
sheet.setColumnView(1, 25);
sheet.setColumnView(2, 100);
sheet.setColumnView(3, 10);
// 设置单元格高度
sheet.setRowView(0, 800);// 设置第2行高度为50,注意单位为像素,1厘米=240象素
sheet.setRowView(1, 600);// 设置第2行高度为50,注意单位为像素,1厘米=240象素
sheet.setRowView(2, 600);// 设置第2行高度为50,注意单位为像素,1厘米=240象素
// 设置字体
// 构造标题格式:黑体、12号、非粗体、非斜体、无下划线、黑色
WritableFont titleFont = new WritableFont(jxl.write.WritableFont.createFont("黑体"), 12,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat twcf = new WritableCellFormat(titleFont);
twcf.setAlignment(Alignment.CENTRE);// 把水平对齐方式指定为居中
twcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
twcf.setWrap(true);// 通过调整宽度和高度自动换行
// 设置标题
sheet.addCell(new Label(0, 0, author, twcf));
sheet.addCell(new Label(0, 1, "项目类别", twcf));
sheet.addCell(new Label(1, 1, "目标考评内容", twcf));
sheet.addCell(new Label(3, 1, "分值", twcf));
sheet.addCell(new Label(1, 2, "任务分类", twcf));
sheet.addCell(new Label(2, 2, "目标任务", twcf));
// 定义数据字体格式等
WritableFont classifyFont = new WritableFont(jxl.write.WritableFont.createFont("宋体"), 11,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat cwcf = new WritableCellFormat(classifyFont);
cwcf.setWrap(true);// 通过调整宽度和高度自动换行
cwcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
WritableFont contentFont = new WritableFont(jxl.write.WritableFont.createFont("宋体"), 10,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat swcf = new WritableCellFormat(contentFont);
swcf.setWrap(true);// 通过调整宽度和高度自动换行
swcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
// 拼接数据
String categorytemp = "";
String classifytemp = "";
for (int i = 0; i < res.size(); i++) {
categorytemp = res.get(i).get("category").toString();
classifytemp = res.get(i).get("classify").toString();
sheet.addCell(new Label(0, i + 3, categorytemp, twcf)); // 项目类别
sheet.addCell(new Label(1, i + 3, classifytemp, cwcf)); // 任务分类
sheet.addCell(new Label(2, i + 3, res.get(i).get("target").toString(), swcf)); // 目标任务
List scoreList = (List) res.get(i).get("criterion");
if (scoreList.size() > 0) {
String score = scoreList.get(0).get("score").toString();
sheet.addCell(new Label(3, i + 3, score, swcf)); // 分值
}
}


// 合并项目类别
sheet.mergeCells(0, 3, 0, 3 + res.size() - 1);
// 合并任务分类
int x = 0;
Map cellsMap = new HashMap();
for (int y = 0; y < res.size(); y++) {
if (classifytemp.equals(res.get(y).get("classify").toString())) {
cellsMap.put(x, y + 3); // key为合并开始单元格,value为合并结束单元格,注:key值相同时,自动覆盖前值
} else {
x = y + 3;
}
classifytemp = res.get(y).get("classify").toString();
}
for (Map.Entry entry : cellsMap.entrySet()) {
// 循环map,合并单元格
sheet.mergeCells(1, entry.getKey(), 1, entry.getValue());
}
workbook.write();
// 最后一步,关闭工作簿
workbook.close();
// downFile(filePath);
}
return res;
} catch (Exception e) {
logger.error(e.toString());
if (null != sqlSession) {
sqlSession.rollback();
}
throw e;
} finally {
if (null != sqlSession) {
sqlSession.close();
}
}
}

你可能感兴趣的:(使用jxl动态合并单元格)