最近在寫新項目,其中遇到了比較好玩的要求,就是要把統計功能中的圖表和表格導出去。在選擇用什么方案解決問題 ,使用了POI導出EXCEL,導出的圖表看起來不好看,背景有方格就棄用了,最后敲定了用ITEXT框架來寫。使用Itext的jar分別是:com.lowagie.itext.2.1.7和com.lowagie.itext-rtf.2.1.7。
/**
* word創建表單
*@param tableDatas真實數據集合
*@param columns標題
*@param imagePath文件地址
*@param wordFileName 文檔名字
*/
public String itextCreateTable(List tableDatas,String[] columns ,String wordFileName){
booleanbool =true;
try{
//把文件放到桌面上
File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
String desktopPath = desktopDir.getAbsolutePath();
Document document =newDocument(PageSize.A4.rotate());
RtfWriter2.getInstance(document, newFileOutputStream(desktopPath+"\\"+wordFileName+".doc"));
document.open();
//創建多個表格
Table aTable =newTable(columns.length);
for(String s:columns) {
//把表格上方的標題創建出來
aTable.addCell(newCell(s));
}
//把數據填寫到表格中,只要夠了表格數量會自動換行
for(TableData tableData : tableDatas){
aTable.addCell(newCell(tableData.getKey()));
for(String number:tableData.getValue()) {
aTable.addCell(newCell(number));
}
}
document.add(aTable);
document.add(newParagraph("\n"));
document.close();
}catch(Exception e) {
bool =false;
e.printStackTrace();
}
if(bool){
return"導出成功(在桌面)";
}else{
return"請關閉word文檔,再重新導出";
}
}
tableDatas={"總投資(萬元)",[ 1,2,3,4,5,6,7,8,9]}
columns = ["項目類別", "實施方案", "規劃環評","大綱+報告書","僅大綱","報告書","報告表","補充報告","補充報告書","補充報告表"]
這兩個數據都是在頁面處理傳到后臺
1.png