根據選擇的欄位名進行動態匯出Excel表

來源:互聯網
上載者:User

publicclassExportExcel3Action { @Resource TotalQueryService totalQueryService;
privateString fileName; publicInputStream getInputStream() throwsException{ HSSFWorkbook wb = newHSSFWorkbook(); HSSFSheet sheet = wb.createSheet("sheet1"); HSSFRow row = sheet.createRow(0); HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //設定對齊 style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //設定邊框 style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); String city=ServletActionContext.getRequest().getParameter("city"); //擷取選取到的值 intexpSize=city.split(",").length; //計算擷取到多少個欄位 //設定標題列 HSSFCell cell = row.createCell(0); cell.setCellStyle(style); cell.setCellValue("序號"); sheet.setColumnWidth(0, 2000); for(intj = 1; j <= expSize; j++) { String[] biaozhi = city.split(","); String lieming = biaozhi[j - 1].split("~")[1].toString(); // String liezhi = biaozhi[j - 1].split("~")[0].toString(); cell = row.createCell(j); sheet.setColumnWidth(j, 6000); cell.setCellStyle(style); cell.setCellValue(lieming); } UserInfo userInfo =(UserInfo)ServletActionContext.getRequest().getSession().getAttribute(Constants.SessionUser); String startYear = ServletActionContext.getRequest().getParameter("startyearbkxh"); String endYear = ServletActionContext.getRequest().getParameter("endyearbkxh"); String projectTypeName = ServletActionContext.getRequest().getParameter("projectTypeNamebkxh"); String departmentName = ServletActionContext.getRequest().getParameter("unitNamebkxh"); String departmentId = ServletActionContext.getRequest().getParameter("unitIdbkxh"); String state = ServletActionContext.getRequest().getParameter("statebkxh"); String dir =(String)ServletActionContext.getRequest().getSession().getAttribute("dir1"); String sort =(String)ServletActionContext.getRequest().getSession().getAttribute("sort1"); //擷取資料 List<ProjectApply> list2=newArrayList<ProjectApply>(); if(startYear==null&&endYear==null&&projectTypeName==null&&departmentId==null&&state==null){ list2 = totalQueryService.findProjectApplyList(userInfo.getDepBarIds().split(","),userInfo,sort, dir); }else{ list2 = totalQueryService.fingProAppBySta(userInfo.getDepBarIds().split(","),userInfo,startYear, endYear, projectTypeName, departmentId, state, sort, dir); } for(inti = 0;i<list2.size();i++){ ProjectApply pro = list2.get(i); row = sheet.createRow(i + 1); cell = row.createCell(0); cell.setCellValue(i + 1); Map paa = POJO2Map.testReflect(pro); for(intj = 1; j <= expSize; j++) { String[] biaozhi = city.split(","); String liezhi = biaozhi[j - 1].split("~")[0].toString(); cell = row.createCell(j); Object object = paa.get((biaozhi[j - 1]).split("~")[0]); String cellValueString = null; if(object != null) { cellValueString = object.toString(); } if(cellValueString != null&& !cellValueString.equals("")) cell.setCellValue(cellValueString); } } Date date = newDate(); //定義以隨機時間命名的檔案名稱 SimpleDateFormat sd = newSimpleDateFormat("yyyyMMddHHmmss"); fileName = sd.format(date); File file = newFile("d:/aaa.xls"); //建立一個File 拿來當緩衝用.也就是先將記憶體中的excel寫入File中.然後再將File轉換成輸出資料流 try{ FileOutputStream os = newFileOutputStream(file); wb.write(os); os.close(); }catch(Exception e) { e.printStackTrace(); } FileInputStream inputStream = null;; try{ inputStream = newFileInputStream(file); System.out.println(inputStream); }catch(Exception e) { e.printStackTrace(); } returninputStream; } publicString execute() throwsException{ return"success"; } publicString getFileName() { returnfileName; } publicvoidsetFileName(String fileName) { this.fileName = fileName; } publicTotalQueryService getTotalQueryService() { returntotalQueryService; } publicvoidsetTotalQueryService(TotalQueryService totalQueryService) { this.totalQueryService = totalQueryService; }}


配置資訊

< actio n name = "ExportExcel3" class = "cn.sina.andywuchuanlong.exportExcel3Action" > < result type = "stream" > < param name ="contentType">application/vnd.ms-excel</ param > < param name = "inputName" >inputStream</ param > < param name = "contentDisposition" >attachement;filename="${fileName}.xls"</ param > < param name = "bufferSize" >1024</ param > </ result > </ action >


本文出自 “想要跨越,就要付出更多” 部落格,請務必保留此出處http://andywuchuanlong.blog.51cto.com/4898493/1303529

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.