標籤:add filesyste lap ready cep trace etc display free
大多時候會出現需要匯出excel的功能,利用poi可以實現簡單的匯出,可以說poi的功能非常強大可以做到細節的定製化操作,但相對於在office操作excel,利用poi完全產生excel會顯得非常複雜,細節代碼會特別多。在這個時候我們可以事先利用offfice制定好一個模版檔案,在利用jxls來通過模版檔案來實現複雜excel的匯出。
模版的製作和一般excel的編寫是一樣的,在需要填充的網格利用{}來進行取出java中傳進來的變數值,
在java中:
InputStream is =getFileInputStream(templateFileName);//擷取模板輸入資料流XLSTransformer transformer = new XLSTransformer();HSSFWorkbook resultWorkbook =transformer.transformMultipleSheetsList(is,prints/*要列印的list*/,
sheetNames/*sheet的名稱list*/, "print"/*excel的bean名稱*/, new HashMap(), 0);
在excel中,jxls會將要列印的list通過我們定義的bean名稱取出進行遍曆,在源碼中
public HSSFWorkbook transformMultipleSheetsList(InputStream is, List objects, List newSheetNames, String beanName, Map beanParams, int startSheetNum) throws ParsePropertyException { HSSFWorkbook hssfWorkbook = null; try { if (beanParams != null && ((Map)beanParams).containsKey(beanName)) { throw new IllegalArgumentException("Selected bean name ‘" + beanName + "‘ already exists in the bean map"); } if (beanName == null) { throw new IllegalArgumentException("Bean name must not be null"); } if (beanParams == null) { beanParams = new HashMap(); } POIFSFileSystem fs = new POIFSFileSystem(is); hssfWorkbook = new HSSFWorkbook(fs); for(int sheetNo = 0; sheetNo < hssfWorkbook.getNumberOfSheets(); ++sheetNo) { String spreadsheetName = hssfWorkbook.getSheetName(sheetNo); if (!this.isSpreadsheetToRemove(spreadsheetName)) { if (this.isSpreadsheetToRename(spreadsheetName)) { hssfWorkbook.setSheetName(sheetNo, this.getSpreadsheetToReName(spreadsheetName)); } HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(sheetNo); if (startSheetNum == sheetNo && objects != null && !objects.isEmpty()) { for(int i = 0; i < objects.size(); ++i) { Object bean = objects.get(i); String beanKey = beanName; if (i != 0) { beanKey = beanName + i; HSSFSheet newSheet = hssfWorkbook.createSheet((String)newSheetNames.get(i)); Util.copySheets(newSheet, hssfSheet, beanName, beanKey); Util.copyPageSetup(newSheet, hssfSheet); Util.copyPrintSetup(newSheet, hssfSheet); } else { hssfWorkbook.setSheetName(sheetNo, (String)newSheetNames.get(i)); } ((Map)beanParams).put(beanKey, bean);//在此次將我們傳入的list通過map的形式放入beanParams } } } else { hssfWorkbook.removeSheetAt(sheetNo); --sheetNo; } } } catch (IOException var16) { var16.printStackTrace(); } if (hssfWorkbook != null) { for(int i = 0; i < hssfWorkbook.getNumberOfSheets(); ++i) { Util.setPrintArea(hssfWorkbook, i); } } this.transformWorkbook(hssfWorkbook, (Map)beanParams);//通過模板和beanParams生產excel return hssfWorkbook; }
View Code
在excel中通過beanParams的Map形式來進行取值,有點類似於freemarker文法,但不同於freemarker
在此案例中,我定義了一個類(domain)來存放list,類的形式如下(即要列印的list的元素是以下類)
public class ExcelPrintDomain implements Serializable { private List lista = new ArrayList(); private Object domaina; private ServletContext context; private int pageNo; public ExcelPrintDomain() { } public ExcelPrintDomain(ServletContext context) { this.context = context; } public void addDomain(Object obj) { this.domains.add(obj); } public void addLista(Object obj) { this.lista.add(obj); } public int getPageNo() { return this.pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public List getLista() { return this.lista; } public void setLista(List lista) { this.lista = lista; } public Object getDomaina() { return this.domaina; } public void setDomaina(Object domaina) { this.domaina = domaina; }}
View Code
所以在模板中${print.domaina.orgName}代表的是取出domain裡面的domaina裡面的key為orgName的value值,
${print.lista.ec01002}代表的是在遍曆中取出domain中的lista包含的Map裡面的key為ec01002的value值。
java+jxls利用excel模版進行匯出