標籤:java java web 讀寫excel表格
本來是以做資料採礦的目的進去哪網的,結構卻成了系統開發。。。
不過還是比較認真的做了三個月,老師很認同我的工作態度和成果。。。
實習馬上就要結束了,總結一下幾點之前沒有注意過的變成習慣和問題,分享給大家。
同時打個廣告:去哪網內審部招JavaWeb開發實習生,時間非常自由,每周一天、周六周日甚至都可以,時間充裕的小夥伴給我留言啊,掙個零花錢,還能長點經驗。。。。(保研的、想工作的大四狗最合適不過了。。。)
需喲的包(java操作excel包 jxl.jar):http://download.csdn.net/detail/mmc2015/9009859
還是直接上代碼,官方文檔參考http://www.andykhan.com/jexcelapi/:
//(NOTE: when creating a spreadsheet from a ServletInputStream you must remove the HTTP header information before creating the Workbook object.) <html>@SuppressWarnings("rawtypes")public static void createExcel(TreeMap MonitorPointInstanceDetailsMap) throws WriteException, IOException{String monitorPointName = MonitorPointInstanceDetailsMap.get("monitorPointName").toString();//輸出EXCELString nowDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());String serverRealRootPath =<strong> GetServerRealPathUtil.getRootPath()</strong>;//參考前幾篇部落格//System.out.println(serverRealRootPath);String fileName = serverRealRootPath+File.separator+nowDate+".xlsx";File file = new File(fileName);OutputStream os = new FileOutputStream(file); //建立工作薄 WritableWorkbook workbook = Workbook.createWorkbook(file); //建立新的一頁 WritableSheet sheet = workbook.createSheet("First Sheet", 0); //建立要顯示的內容,建立一個儲存格,第一個參數為列座標,第二個參數為行座標,第三個參數為內容 //The other point to note is that the cell's location is specified as (column, row). Both are zero indexed integer values - A1 being represented by (0,0), B1 by (1,0), A2 by (0,1) and so on. /*構造合并的表頭*/ //添加合併儲存格,第一個參數是起始列,第二個參數是起始行,第三個參數是終止列,第四個參數是終止行 sheet.mergeCells(0, 0, 6, 0); //設定字型種類和黑體顯示,字型為Arial,字型大小大小為15,採用黑體顯示 WritableFont mergeTitleBold = new WritableFont(WritableFont.ARIAL, 15, WritableFont.BOLD); //產生一個儲存格樣式控制對象 WritableCellFormat mergeTitleFormate = new WritableCellFormat(mergeTitleBold); //儲存格中的內容水平方向置中 mergeTitleFormate.setAlignment(jxl.format.Alignment.CENTRE); //儲存格的內容垂直方向置中 mergeTitleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); Label mergeTitle = new Label(0, 0, "表頭表頭。。。", mergeTitleFormate); sheet.setRowView(0, 800, false);//設定行的高度 sheet.addCell(mergeTitle); int currentRow = 1; Iterator it=MonitorPointInstanceDetailsMap.entrySet().iterator();while(it.hasNext()){ Map.Entry entry = (Map.Entry)it.next(); String processID = entry.getKey().toString(); if(processID.equals("monitorPointName")){ continue; } String value = entry.getValue().toString(); //System.out.println(processID+"---"+value); String processName = value.split(";")[0]; if((value.split(";")).length!=5){ //該流程【在統計周期內】還沒有對應的流程執行個體資訊 continue; } String[] processInstance_Name = (value.split(";")[1]).split(","); String[] processInstance_SenderName = (value.split(";")[2]).split(","); String[] processInstance_CreateDate = (value.split(";")[3]).split(","); String[] processInstance_StopNodeName = (value.split(";")[4]).split(","); for(int i=0;i<processInstance_Name.length&&!processInstance_Name[i].equals("");i++){ if(i==0){//增加標題列 //設定字型種類和黑體顯示,字型為Arial,字型大小大小為12,採用黑體顯示 WritableFont titleBold = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD); //產生一個儲存格樣式控制對象 WritableCellFormat titleFormate = new WritableCellFormat(titleBold); //儲存格中的內容水平方向置中 titleFormate.setAlignment(jxl.format.Alignment.CENTRE); //儲存格的內容垂直方向置中 titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); sheet.setRowView(0, 600, false);//設定行的高度 Label title0 = new Label(0, currentRow, "ID", titleFormate); Label title1 = new Label(1, currentRow, "流程名稱", titleFormate); Label title2 = new Label(2, currentRow, "流程執行個體名稱", titleFormate); Label title3 = new Label(3, currentRow, "執行個體發起人", titleFormate); Label title4 = new Label(4, currentRow, "執行個體發起時間", titleFormate); Label title5 = new Label(5, currentRow, "執行個體是否結束", titleFormate); Label title6 = new Label(6, currentRow, "待處理的節點名稱", titleFormate); sheet.addCell(title0); sheet.addCell(title1); sheet.addCell(title2); sheet.addCell(title3); sheet.addCell(title4); sheet.addCell(title5); sheet.addCell(title6); currentRow++; } //增加執行個體內容行 sheet.addCell(new Label(0, currentRow, Integer.toString(i+1))); sheet.addCell(new Label(1, currentRow, processName)); sheet.addCell(new Label(2, currentRow, processInstance_Name[i])); sheet.addCell(new Label(3, currentRow, processInstance_SenderName[i])); sheet.addCell(new Label(4, currentRow, processInstance_CreateDate[i])); sheet.addCell(new Label(5, currentRow, "未結束")); sheet.addCell(new Label(6, currentRow, processInstance_StopNodeName[i])); <span style="white-space:pre"></span>currentRow++; }} //把建立的內容寫入到輸出資料流中,並關閉輸出資料流 workbook.write(); workbook.close(); os.close(); }
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
去哪網實習總結:java讀寫excel表格(JavaWeb)