由於工作需要,excel每次個工作表格式一樣,每次手工填寫太麻煩,於是想到了服務器自己來做這個工作。
protected HSSFWorkbook wb = null;
protected HSSFSheet sheet;
protected HSSFRow row = null;
protected HSSFCell cell = null;
public void export(String url,BailChargeDTO list,String sDate,String eDate, OutputStream output) throws Exception {
POIFSFileSystem fs = null;
try {
fs = new POIFSFileSystem(new FileInputStream(url));
wb = new HSSFWorkbook(fs);
int sdate = Integer.parseInt(sDate);
int edate = Integer.parseInt(eDate);
for(int date = sdate ; date <= edate ; ++date){
HSSFSheet fromsheet = wb.getSheet("template");
if (fromsheet != null && wb.getSheet(String.valueOf(date)) == null) {
HSSFSheet newsheet = wb.createSheet(String.valueOf(date));
new PoiExcelSheetCopy().copyRows(wb, fromsheet, newsheet, fromsheet.getFirstRowNum(), fromsheet.getLastRowNum());
}
sheet = wb.getSheet(String.valueOf(date));//獲取工作表名字
row = sheet.createRow(1);
Cell ztCell = row.createCell(0);
ztCell.setCellValue(" 日期:" + date);
// 創建單元格樣式對象
HSSFCellStyle cellstyle = wb.createCellStyle();
HSSFCellStyle textstyle = wb.createCellStyle();
HSSFCellStyle sumstyle = wb.createCellStyle();
// 創建字體對象
Font dateFont = wb.createFont();
dateFont.setFontHeightInPoints((short)18); // 將字體大小設置爲18px
dateFont.setFontName("宋體"); // 將“華文行楷”字體應用到當前單元格上
dateFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(dateFont); // 將字體應用到樣式上面
cellstyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
ztCell.setCellStyle(cellstyle);
//
Font textFont = wb.createFont();
textFont.setFontHeightInPoints((short)18); // 將字體大小設置爲18px
textFont.setFontName("宋體"); // 將“華文行楷”字體應用到當前單元格上
textstyle.setFont(textFont); // 將字體應用到樣式上面
textstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
Font suFont = wb.createFont();
suFont.setFontHeightInPoints((short)18); // 將字體大小設置爲18px
suFont.setFontName("宋體"); // 將“華文行楷”字體應用到當前單元格上
suFont.setColor(HSSFColor.RED.index);
sumstyle.setFont(suFont); // 將字體應用到樣式上面
sumstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//
int rownum = 4;
List<AgentAccountDetailDTO> dtoList = new BailChargeQueryExport().sortMap(list.getAgentAccount(),date).get(String.valueOf(date));
int len = dtoList.size();
for (int j = 0; j < len; j++) {
AgentAccountDetailDTO dto = dtoList.get(j);
row = sheet.createRow(rownum);
row.setHeightInPoints(20);
Cell c = row.createCell(0);
Cell d = row.createCell(1);
Cell e = row.createCell(2);
Cell f = row.createCell(3);
Cell h = row.createCell(4);
Cell i = row.createCell(5);
Cell k = row.createCell(6);
Cell g = row.createCell(7);
c.setCellValue(new HSSFRichTextString(String.valueOf(dto.getAgentName())));
d.setCellValue(dto.getAgentId());
e.setCellValue(dto.getAgentLink());
f.setCellValue(Double.parseDouble(dto.getRfee().toString()));
// row.createCell(2).setCellValue(new HSSFRichTextString(dto.getRuser()));
// row.createCell(4).setCellValue(dto.getRdate());
g.setCellValue(dto.getTransDesc());
c.setCellStyle(textstyle);
d.setCellStyle(textstyle);
e.setCellStyle(textstyle);
f.setCellStyle(textstyle);
g.setCellStyle(textstyle);
h.setCellStyle(textstyle);
i.setCellStyle(textstyle);
k.setCellStyle(textstyle);
//
rownum++;
}
row = sheet.createRow(rownum);
sheet.setForceFormulaRecalculation(true);
row.setHeightInPoints(20);
Cell c = row.createCell(0);
c.setCellValue("總數");
c.setCellStyle(sumstyle);
Cell sum = row.createCell(3);
sum.setCellType(HSSFCell.CELL_TYPE_FORMULA);//設置爲公式
// 設置公式內容
String formula = "SUM(D5:D"+ rownum +")";//計算一列總數
if(HSSFCell.CELL_TYPE_FORMULA == sum.getCellType()){
sum.setCellFormula(formula);
}
sum.setCellStyle(sumstyle);
}
wb.setActiveSheet(1);//激活滴2個工作表,保存打開
wb.write(output);
} catch (IOException e) {
throw new Exception("服務器報表文件位置異常,請報告管理員!謝謝!");
}
}
//對數據篩選
public Map<String ,List<AgentAccountDetailDTO>> sortMap(List<AgentAccountDetailDTO> list, int sdate){
Map<String ,List<AgentAccountDetailDTO>> map = new HashMap<String ,List<AgentAccountDetailDTO>>();
List<AgentAccountDetailDTO> dtoList = new ArrayList<AgentAccountDetailDTO>(); //重新聲明一個數組list
for(Iterator<AgentAccountDetailDTO> it = list.iterator();it.hasNext();){
AgentAccountDetailDTO dto = (AgentAccountDetailDTO)it.next();
if(Integer.parseInt(dto.getRdate()) == (sdate)){ //如果已經存在這個數組,就放在這裏
dtoList.add(dto);
}
}
map.put(String.valueOf(sdate), dtoList);
return map;
}
WPS 打開不能自動求和,ms excel 打開後可以自動求和。