既然被歸類到技術(shù)貼,我盡量控制少說廢話,直接上問題:
數(shù)據(jù)源如下圖:

webwxgetmsgimg (1).jpg
客戶要求效果圖:

webwxgetmsgimg.jpg
由以上兩圖可以看出,以我對jxls的簡單認(rèn)知是基本沒可能做出來的,于是乎,我采取了最笨的方法,使用poi將數(shù)據(jù)生生寫入excel,哈哈哈哈哈哈,祭出主要代碼:
<p>Here is the code:</p>
private int exportAccountBankTotalExcel(List<AccountBankTotal> listAccountBankTotal, Integer year, Integer month,
OutputStream outputStream, String templateFilePath) {
InputStream templateXls = null;
try {
//讀取excel模板,模板中只有一行標(biāo)題,templateFilePath是傳過來的路徑
templateXls = new BufferedInputStream(ExportMoneyExcelImpl.class.getResourceAsStream(templateFilePath));
XLSTransformer transformer = new XLSTransformer();
Map<String, Object> beans = new HashMap<String, Object>();
//創(chuàng)建Workbook對象
Workbook workBook = transformer.transformXLS(templateXls, beans);
// sheet對應(yīng)一個工作頁
Sheet sheet = workBook.getSheetAt(0);
// 設(shè)置樣式1
CellStyle cellStyle = workBook.createCellStyle();
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setAlignment((HSSFCellStyle.ALIGN_CENTER));
// 設(shè)置樣式2
CellStyle cellStyleRight = workBook.createCellStyle();
cellStyleRight.cloneStyleFrom(cellStyle);
cellStyleRight.setAlignment((HSSFCellStyle.ALIGN_RIGHT));
// 往excel中寫入新數(shù)據(jù)
Row firstRow = sheet.createRow(1); // 第一行讓給表頭了,這個假裝是第一行
Cell firstCell = firstRow.createCell(0); // 第一列
firstCell.setCellValue("日期");
firstCell.setCellStyle(cellStyle);
// 以acountDate為key
Map<String, List<AccountBankTotal>> map = new LinkedHashMap<String, List<AccountBankTotal>>();
for (AccountBankTotal accountBankTotal : listAccountBankTotal) {
if (map.containsKey(accountBankTotal.getAccountDate())) {
map.get(accountBankTotal.getAccountDate()).add(accountBankTotal);
} else {
List<AccountBankTotal> newList = new ArrayList<AccountBankTotal>();
newList.add(accountBankTotal);
map.put(accountBankTotal.getAccountDate(), newList);
}
}
// 聲明Set集合
Set<String> bankSet = new LinkedHashSet<String>();
// 遍歷Map將銀行名存到Set集合中
for (String key : map.keySet()) {
for (AccountBankTotal abt : map.get(key)) {
bankSet.add(abt.getBankName());
}
}
// 輸出銀行名到Excel
int i = 1;
for (Iterator it = bankSet.iterator(); it.hasNext();) {
firstRow.createCell(i).setCellValue(it.next().toString());
// logger.info("bankname:" + it.next().toString());
firstRow.getCell(i).setCellStyle(cellStyle);
i++;
}
// 遍歷Map,將Map中的鍵值寫入Excel
int count = 2;
for (String key : map.keySet()) {
sheet.createRow(count); // 從第二行開始創(chuàng)建行
int index = 0;
int countRow = count;
// 寫入日期&日期格式
sheet.getRow(count).createCell(index).setCellValue(key);
sheet.getRow(count).getCell(index).setCellStyle(cellStyle);
// 遍歷Set,通過bankName&date確定對應(yīng)的money
for (String bankName : bankSet) {
if (map.get(key).get(index).getBankName().equals(bankName)) {
sheet.getRow(countRow).createCell(index + 1)
.setCellValue(String.format("%.2f", map.get(key).get(index).getPayMoney()));
logger.info("map.Money: " + map.get(key).get(index).getPayMoney());
sheet.getRow(countRow).getCell(index + 1).setCellStyle(cellStyleRight);
}
index++;
}
count++;
}
// 將“合計”寫入Excel最后一行第一列
Row rowLast = sheet.createRow(sheet.getLastRowNum() + 1);
rowLast.createCell(0).setCellValue("合計");
rowLast.getCell(0).setCellStyle(cellStyle);
int countSet = 1;
// 以銀行名為key存儲數(shù)據(jù)
Map<String, List<AccountBankTotal>> tempMap = new LinkedHashMap<String, List<AccountBankTotal>>(); // 以銀行名為鍵
for (AccountBankTotal accountBankTotal : listAccountBankTotal) {
if (tempMap.containsKey(accountBankTotal.getBankName())) {
tempMap.get(accountBankTotal.getBankName()).add(accountBankTotal);
} else {
List<AccountBankTotal> newList = new ArrayList<AccountBankTotal>();
newList.add(accountBankTotal);
tempMap.put(accountBankTotal.getBankName(), newList);
}
}
List<String> sumList = new LinkedList<String>();
for (String key : tempMap.keySet()) {
BigDecimal sum = new BigDecimal("0.00");
for (int m = 0; m < tempMap.get(key).size(); m++) {
logger.info("Money: " + tempMap.get(key).get(m).getPayMoney());
sum = sum.add(tempMap.get(key).get(m).getPayMoney());
}
sumList.add(String.format("%.2f", sum));
}
// 遍歷 合計 值&將其寫入表格
for (String sum : sumList) {
rowLast.createCell(countSet);
rowLast.getCell(countSet).setCellValue(sum + "");
rowLast.getCell(countSet).setCellStyle(cellStyleRight);
countSet++;
}
// 合并單元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, bankSet.size()));
// 設(shè)置行高、列寬
for (int colNum = 0; colNum < 4; colNum++) {
sheet.setColumnWidth(colNum, 256 * 21);
for (int rowNum = 1; rowNum < sheet.getLastRowNum(); rowNum++) {
sheet.getRow(rowNum).setHeightInPoints(13.5f);
}
}
//寫入excel模板
workBook.write(outputStream);
outputStream.flush();
return 0;
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error("", e);
return -2;
} finally {
if (templateXls != null) {
try {
templateXls.close();
} catch (IOException e) {
}
}
}
}
以上代碼,注釋比較詳盡,如有類似問題,可在參考該代碼后自行動腦解決,寫完了,不說了,憋不住了,我要出宮去了。。。。