word 進行資料庫 擷取資料

來源:互聯網
上載者:User

標籤:資料庫   doc   

 /**
     * Word匯出
     * @throws DocumentException 
     */
      public void exeportWord() throws DocumentException{
    //放到tomcat下
      String formName="word";
      String pathName=ServletActionContext.getRequest().getSession().getServletContext().getRealPath("")+"\\export\\"+formName+".doc";
 
      //4.1定義表頭
      String tableName="T_USER";
      String tableCName = selectDataService.getString("select comments from user_tab_comments where table_name =‘"+tableName+"‘");
        


      //4.2擷取表的中文注釋
      List<Map> list=selectDataService.queryForList("select t.column_name,t.comments from user_col_comments t where table_name =‘"+tableName+"‘");
        //4.2 建立一個list
      List<String> columns=new ArrayList<String>();
      Map<String, String> colmap=new TreeMap<String, String>();
      for (Map map:list) {
 
      //把資料庫裡的注釋取出來,放到columns裡
      columns.add(map.get("COMMENTS").toString());
      //把數據庫的中文注釋放到map集合中
      colmap.put(map.get("COMMENTS").toString(), map.get("COLUMN_NAME").toString());
     
  }
     
      FileOutputStream outputStream=null;
      Document document=null;
      try {
     
         
          //建立輸出資料流
  outputStream=new FileOutputStream(pathName);
 
  //建立document對象 
  document=new Document(PageSize.A4);
 
  //輸出資料流 與document 進行關聯
  RtfWriter2.getInstance(document, outputStream);
 
  //開啟document
  document.open();
 
  //定義字型
  BaseFont createFont = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
  Font font=new Font(createFont,10,Font.BOLD);
 
  //寫入字型
  Paragraph paragraph=new Paragraph("使用者管理",font);
  //把他放到中間
  paragraph.setAlignment("center");
  //儲存到document
  document.add(paragraph);
 
  //換行
  document.add(new Paragraph("\n",font));
 
 
  // 從資料庫裡取出所有資料
      List<Map> dataList=selectDataService.queryForList("select * from "+tableName);
     
  //寫表格
 
  PdfPTable pdf= new PdfPTable(columns.size());
 
  PdfPCell cell=null;
  //建立表格
  for(int i=0;i<11;i++){
 
     String zhang = columns.get(i);
  cell=new PdfPCell(new Phrase(zhang, font));
  pdf.addCell(cell);
  }
 
 
 
  for (int i = 0; i < dataList.size(); i++) {
     
  //擷取一行一行的資料
      Map cols=dataList.get(i);
     
      //j迴圈是迴圈一條資料裡的每個值
      for (int j = 0; j < cols.size(); j++) {
 
      //放到每個單元格裡
      String fangyuange = columns.get(j);
     
      //去英文欄位
      String ing = colmap.get(fangyuange);
     
      //放到列種
      Object value = cols.get(ing);
     
      //建立單元格(通過行)放進單元格
      cell = new PdfPCell(new Phrase(""+value,font));
  pdf.addCell(cell);
     
  }
  }
 
 
 
  document.add(pdf);
 
//   ServletActionContext.getResponse().getWriter().write("ok");
 
 
  } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }finally{
  document.close();
  try {
  outputStream.close();
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  }
    FileUtil.downloadFile(ServletActionContext.getRequest(),  ServletActionContext.getResponse(), pathName, formName+".doc");  //看圖片
//      return null;

      }



word 進行資料庫 擷取資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.