/** * 系統數據導出Excel 生成器 * @version 1.0 */ package com.tranb.common.tools; import java.io.OutputStream; import java.util.ArrayList; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; public class ExcelFileGenerator { private final int SPLIT_COUNT = 60000; //Excel每一個工做簿的行數 private ArrayList<String> fieldName = null; //excel標題數據集 private ArrayList<ArrayList<String>> fieldData = null; //excel數據內容 private HSSFWorkbook workBook = null; /** * 構造器 * @param fieldName 結果集的字段名 * @param data */ public ExcelFileGenerator(ArrayList<String> fieldName, ArrayList<ArrayList<String>> fieldData) { this.fieldName = fieldName; this.fieldData = fieldData; } /** * 建立HSSFWorkbook對象 * @return HSSFWorkbook */ public HSSFWorkbook createWorkbook() { workBook = new HSSFWorkbook();//建立一個工做薄對象 int rows = fieldData.size();//總的記錄數 int sheetNum = 0; //指定sheet的頁數 if (rows % SPLIT_COUNT == 0) { sheetNum = rows / SPLIT_COUNT; } else { sheetNum = rows / SPLIT_COUNT + 1; } for (int i = 1; i <= sheetNum; i++) {//循環2個sheet的值 HSSFSheet sheet = workBook.createSheet("Page " + i);//使用workbook對象建立sheet對象 HSSFRow headRow = sheet.createRow((short) 0); //建立行,0表示第一行(本例是excel的標題) for (int j = 0; j < fieldName.size(); j++) {//循環excel的標題 HSSFCell cell = headRow.createCell( j);//使用行對象建立列對象,0表示第1列 /**************對標題添加樣式begin********************/ //設置列的寬度/ sheet.setColumnWidth(j, 6000); HSSFCellStyle cellStyle = workBook.createCellStyle();//建立列的樣式對象 HSSFFont font = workBook.createFont();//建立字體對象 //字體加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //字體顏色變紅 font.setColor(HSSFColor.RED.index); //若是font中存在設置後的字體,並放置到cellStyle對象中,此時該單元格中就具備了樣式字體 cellStyle.setFont(font); /**************對標題添加樣式end********************/ //添加樣式 cell.setCellType(HSSFCell.CELL_TYPE_STRING); if(fieldName.get(j) != null){ //將建立好的樣式放置到對應的單元格中 cell.setCellStyle(cellStyle); cell.setCellValue((String) fieldName.get(j));//爲標題中的單元格設置值 }else{ cell.setCellValue("-"); } } //分頁處理excel的數據,遍歷全部的結果 for (int k = 0; k < (rows < SPLIT_COUNT ? rows : SPLIT_COUNT); k++) { if (((i - 1) * SPLIT_COUNT + k) >= rows)//若是數據超出總的記錄數的時候,就退出循環 break; HSSFRow row = sheet.createRow( k + 1);//建立1行 //分頁處理,獲取每頁的結果集,並將數據內容放入excel單元格 ArrayList<String> rowList = (ArrayList<String>) fieldData.get((i - 1) * SPLIT_COUNT + k); for (int n = 0; n < rowList.size(); n++) {//遍歷某一行的結果 HSSFCell cell = row.createCell( n);//使用行建立列對象 if(rowList.get(n) != null){ cell.setCellValue((String) rowList.get(n).toString()); }else{ cell.setCellValue(""); } } } } return workBook; } public void expordExcel(OutputStream os) throws Exception { workBook = createWorkbook(); workBook.write(os);//將excel中的數據寫到輸出流中,用於文件的輸出 os.close(); } } /** * 用戶數據導出模塊- 數據導出 */ public String exportData(ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { String startTime = request.getParameter("startTime"); String endTime = request.getParameter("endTime"); String status = request.getParameter("status"); String uids = request.getParameter("uids"); ArrayList<String> fieldName = new ArrayList<String>(); fieldName.add("uid"); fieldName.add("姓名"); fieldName.add("行業"); fieldName.add("供應鏈身份"); fieldName.add("帳號"); fieldName.add("註冊時間"); //fieldName.add("最後登陸時間"); fieldName.add("最後登陸日期"); fieldName.add("當前版本"); fieldName.add("帳號狀態"); fieldName.add("會員等級"); fieldName.add("性別"); fieldName.add("城市"); fieldName.add("電話號碼"); fieldName.add("發佈採購次數"); fieldName.add("被報價總數"); fieldName.add("報價數"); fieldName.add("動態數"); fieldName.add("商品數"); ArrayList<ArrayList<String>> fieldData = new ArrayList<ArrayList<String>>(); String headFileName=""; JSONObject jsonDate=null; if("0".equals(status)){ jsonDate = DaoFactory.getUserInfoDao().getpurchuse_user_report(true,null, uids, startTime, endTime); headFileName="按發佈採購統計"; }else if("1".equals(status)){ jsonDate=DaoFactory.getUserInfoDao().getproduct_user_report(true,null, uids, startTime, endTime); headFileName="按發佈商品統計"; }else if("2".equals(status)){ jsonDate=DaoFactory.getUserInfoDao().getnewCast_user_report(true,null, uids, startTime, endTime); headFileName="按發佈動態統計"; }else if("3".equals(status)){ jsonDate = DaoFactory.getUserInfoDao().getLastActive_user_report(true,null, uids, startTime, endTime); headFileName="按最後登陸時間統計"; }else if("-1".equals(status)){ jsonDate = DaoFactory.getUserInfoDao().getUid_report(true,null, uids, startTime, endTime); headFileName="按UID統計"; } List<JSONObject> dataList= (List<JSONObject>) jsonDate.get("rows"); if(dataList!=null&&dataList.size()>0){ for (JSONObject jsonObject : dataList) { ArrayList<String> temp = new ArrayList<>(); temp.add(jsonObject.getString("uid")); temp.add(jsonObject.has("chineseName")?jsonObject.getString("chineseName"):""); temp.add(jsonObject.has("personIUCode")?jsonObject.getString("personIUCode"):""); temp.add(jsonObject.has("identityType")?jsonObject.getString("identityType"):""); temp.add(jsonObject.has("account")?jsonObject.getString("account"):""); temp.add(jsonObject.has("registerTime")?jsonObject.getString("registerTime"):""); //temp.add(jsonObject.has("lastActive")?jsonObject.getString("lastActive"):""); temp.add(jsonObject.has("logDate")?jsonObject.getString("logDate"):""); temp.add(jsonObject.has("curVersion")?jsonObject.getString("curVersion"):""); temp.add(jsonObject.has("type")?jsonObject.getString("type"):""); String member=""; if(jsonObject.has("member")){ String memberStr = jsonObject.getString("member"); if("0".equals(memberStr)){ member="非會員"; }else if("1".equals(memberStr)){ member="贈送會員"; }else if("2".equals(memberStr)){ member="正式會員"; } temp.add(member); }else{ temp.add(member); } temp.add(jsonObject.has("sex")?(jsonObject.getString("sex").equals("1")?"男":"女"):""); temp.add(jsonObject.has("city")?jsonObject.getString("city"):""); temp.add(jsonObject.has("mobilePhone")?jsonObject.getString("mobilePhone"):""); temp.add(jsonObject.has("purchaseCount")?jsonObject.getString("purchaseCount"):""); temp.add(jsonObject.has("bidSumCount")?jsonObject.getString("bidSumCount"):""); temp.add(jsonObject.has("bidSum")?jsonObject.getString("bidSum"):""); temp.add(jsonObject.has("castCount")?jsonObject.getString("castCount"):""); temp.add(jsonObject.has("productCount")?jsonObject.getString("productCount"):""); fieldData.add(temp); } } ExcelFileGenerator excelFileGenerator = new ExcelFileGenerator(fieldName,fieldData); OutputStream os = response.getOutputStream(); response.reset();//導出excel建議加上重置輸出流 String fileName = headFileName+"("+new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss").format(new Date())+")"; fileName = new String(fileName.getBytes("gbk"),"iso-8859-1"); response.setContentType("application/msexcel"); response.setHeader("Content-disposition", "attachment;filename="+fileName+".xls"); response.setBufferSize(1024); //導出excel的操做 excelFileGenerator.expordExcel(os); return null; }