JExcel API使用笔记
JExcel是一个开源的支持excel的java类库,广泛利用其api来生成excel报表
net.sourceforge.jexcelapi
jxl
2.6.12
从jira中查询数据,导出生成一个搜集用户体验问题的excel报表,并且简单设置表格格式。
@Test
public void testBugStatistic() throws Exception {
List issues = myJiraClient.getIssues("issuetype = Bug AND BUG类型 = 用户体验 AND assignee in (lifei66) ORDER BY created DESC");
WritableWorkbook workbook = Workbook.createWorkbook(new File("7Fresh移动端用户体验问题.xls"));
WritableSheet sheet = workbook.createSheet("用户体验优化建议", 0);
List titles = Arrays.asList("issueKey", "标题", "报告人", "创建日期", "状态", "链接");
List labels = new ArrayList<>();
for (int i = 0; i < titles.size(); i++) {
for (int j = 0; j < issues.size() + 1; j++) {
if (j == 0) {
WritableCellFormat titleCellFormat = new WritableCellFormat(new WritableFont(WritableFont.createFont("微软雅黑"), 14, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK));
titleCellFormat.setBackground(Colour.VERY_LIGHT_YELLOW);
titleCellFormat.setAlignment(Alignment.getAlignment(2));
titleCellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.getAlignment(1));
titleCellFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
labels.add(new Label(i, j, titles.get(i), titleCellFormat));
} else {
WritableCellFormat dataCellFormat = new WritableCellFormat();
dataCellFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
switch (titles.get(i)) {
case "issueKey":
labels.add(new Label(i, j, issues.get(j - 1).getKey(), dataCellFormat));
break;
case "标题":
labels.add(new Label(i, j, issues.get(j - 1).getSummary(), dataCellFormat));
break;
case "报告人":
labels.add(new Label(i, j, issues.get(j - 1).getReporter().getDisplayName(), dataCellFormat));
break;
case "创建日期":
WritableCellFormat dateCellFormat = new WritableCellFormat(new DateFormat("yyyy/MM/dd HH:mm:ss"));
dateCellFormat.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
sheet.addCell(new DateTime(i, j, issues.get(j - 1).getCreationDate().toDate(), dateCellFormat));
break;
case "状态":
labels.add(new Label(i, j, issues.get(j - 1).getStatus().getName(), dataCellFormat));
break;
case "链接":
WritableHyperlink hyperlink = new WritableHyperlink(i, j, new URL("http://jira.jd.com/browse/" + issues.get(j - 1).getKey()));
Label issueLink = new Label(i, j, "http://jira.jd.com/browse/" + issues.get(j - 1).getKey(), dataCellFormat);
hyperlink.setDescription(issueLink.getContents());
sheet.addCell(issueLink);
sheet.addHyperlink(hyperlink);
break;
}
}
}
}
for (Label label : labels) {
sheet.addCell(label);
}
expandColumns(sheet, titles.size());
workbook.write();
workbook.close();
}
private void expandColumns(WritableSheet sheet, int columnSize) {
for (int i = 0; i < columnSize; i++) {
CellView cellView = sheet.getColumnView(i);
cellView.setAutosize(true);
sheet.setColumnView(i, cellView);
}
}