81041
2019-01-18 cf4efde9b2ec3d661269c0346596aaf789eb54ad
gx_tieta/src/com/fgkj/servlets/ExportTableServlet.java
@@ -33,6 +33,9 @@
   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
      String pageName=req.getParameter("PageName");
      String exp_KeyWord = req.getParameter("exp_normal");         //通用到处表格标识符
      if(ServletUtils.isNotNull(pageName)){
         if("ele_statistics.jsp".equals(pageName)){
            excelTitle="电池信息统计查询";
@@ -121,9 +124,94 @@
            excelTitle="设备状态变化记录表";
            Exporttele_eleBrdwMaintTable(resp,req);
         }
      }else if(ServletUtils.isNotNull(exp_KeyWord)){
         if("1".equals(exp_KeyWord)){
            ExportGeneralExcelTable(resp,req);               //导出通用的表格
         }
      }
   }
   
   private void ExportGeneralExcelTable(HttpServletResponse resp,
         HttpServletRequest req) {
      String exp_thead_str = req.getParameter("exp_thead");
      String exp_tbody_str = req.getParameter("exp_tbody");
      String exp_page_break = "\\"+req.getParameter("exp_page_break");
      String exp_data_break = "\\"+req.getParameter("exp_data_break");
      String exp_page_names_str =  req.getParameter("exp_page_names");
      String[] exp_page_names = new String[]{};
      try {
         //定义一个工作簿
         HSSFWorkbook workbook = new HSSFWorkbook();
         if(ServletUtils.isNotNull(exp_thead_str)){
            exp_page_names = exp_page_names_str.split(exp_page_break);         //分割工作簿的名称
            String[] exp_thead_sheets = exp_thead_str.split(exp_page_break);
            String[] exp_tbody_sheets = exp_tbody_str.split(exp_page_break);
            if(!ServletUtils.isNotNull(exp_page_names_str)){
               exp_page_names = new String[]{};
               for(int s=0;s<exp_tbody_sheets.length;s++){
                  exp_page_names[s] = "sheet"+s;
               }
            }
            for (int i = 0; i < exp_thead_sheets.length; i++) {
               HSSFSheet sheet = i<exp_page_names.length?workbook.createSheet(exp_page_names[i]):workbook.createSheet();
               String[] exp_thead = exp_thead_sheets[i].split(exp_data_break);
               String[] exp_tbody = exp_tbody_sheets[i].split(exp_data_break);
               int maxColNum = exp_thead.length;         //最大列数
               int nowRowNum = 0;                     //当前行
               int nowColNum = 0;                     //当前列
               HSSFRow row = null;
               for(int j=0;j <= exp_tbody.length;j++){
                  //System.out.println("j="+j+"\t nowRowNum="+nowRowNum);
                  if(j == 0){
                     row = sheet.createRow(nowRowNum++);
                     for(int k=0;k<exp_thead.length;k++){
                        HSSFCell cell = row.createCell(k);
                        HSSFRichTextString text = new HSSFRichTextString(exp_thead[k]);
                        //System.out.print(text+"\t");
                        cell.setCellValue(text);
                     }
                     continue;
                  }else if(j%maxColNum == 1){
                     nowColNum = 0;
                     row = sheet.createRow(nowRowNum++);
                  }
                  HSSFCell cell = row.createCell(nowColNum++);
                  HSSFRichTextString text = new HSSFRichTextString(exp_tbody[j-1]);
                  //HSSFRichTextString text = new HSSFRichTextString("*****");
                  //System.out.print(text+"\t");
                  cell.setCellValue(text);
               }
            }
         }
         // 转码防止乱码
         resp.addHeader("Content-Disposition", "attachment;filename="
               + new String(excelName.getBytes("gb2312"), "ISO8859-1")
               + ".xls");
         OutputStream out = resp.getOutputStream();
         workbook.write(out);
         out.close();
      } catch (UnsupportedEncodingException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   private void Exporttele_eleBrdwMaintTable(HttpServletResponse resp,
         HttpServletRequest req) {
      String[] th_arr=req.getParameter("table_th_arr").split(",");
@@ -338,5 +426,10 @@
       
   }
   
   public static void main(String[] args) {
      String ths = "h1&h2&h3&h4$xh1&xh2&xh3&xh4";
      System.out.println(ths.indexOf("$"));
      String[] sheets = ths.split("\\&");
      System.out.println(sheets.length);
   }
}