您的位置:首页 > 博客中心 > 数据库 >

word 进行数据库 获取数据

时间:2022-03-14 04:00

 /**
     * 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;

      }


gxlsystem.com,布布扣

热门排行

今日推荐

热门手游