关于POI把数据库结果集导出到EXCEL和WORD的方法

来源:互联网 发布:js kendo treeview 编辑:程序博客网 时间:2024/05/18 00:39

                        这个文章是把网页上的数据导出到本地,就涉及到了导出WORD和EXCEL 不足之处请指出来,共同进步,谢谢!

       废话不多述,进入正题!

  1,将数据库结果集导出到EXCEL

在查询结果集的方法中加上this.excelResule(参数)即可调用。

                  

public void excelResule(List list, SurveysMVO newsurveyvo) {
  HSSFWorkbook wb = new HSSFWorkbook();
  HSSFSheet sheet = wb.createSheet("sheet名称");//sheet名称
  HSSFRow row = sheet.createRow((int) 0);
  HSSFCellStyle style = wb.createCellStyle();
  style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

  HSSFCell cell = row.createCell((short) 0);
  cell.setCellValue("问卷标题");
  cell.setCellStyle(style);
  cell = row.createCell((short) 1);
  cell.setCellValue("题目");
  cell.setCellStyle(style);
  cell = row.createCell((short) 2);
  cell.setCellValue("统计结果");
  cell.setCellStyle(style);
  // 下面两行是设置标题的显示位置 因为循环中设置了显示位置为i+1 所以row =
  // sheet.createRow(1)这样写才不会覆盖“标题”这四个字 而且问卷标题是不循环的 故这样写
  row = sheet.createRow(1);
  row.createCell((short) 0).setCellValue(newsurveyvo.getSurveyTitle());

  for (int i = 0; i < list.size(); i++) {//循环结果集LIST

   row = sheet.createRow((int) i + 1);
   ResultAnalyzeMVO resultAnalyze = (ResultAnalyzeMVO) list.get(i);
   // 第四步,创建单元格,并设置值

   row.createCell((short) 1).setCellValue(
     resultAnalyze.getQuestionTitle());
   row.createCell((short) 2).setCellValue(resultAnalyze.getStr());

  }

  try {
   FileOutputStream fout = new FileOutputStream("C:/结果分析.xls");

   wb.write(fout);
   fout.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 

 

2.导出WORD ,这个不像导出EXCEL那样简单易懂,网上的资源也比较少,这个整整弄了一天才弄出来。下面与大家共勉!

原作者文档位置:http://zhidao.baidu.com/link?url=XqVU70FGYpRWBiTss4KHIWH3lnP5q1coGmFJi7pUGfcHzFwmgCZ1eLgChjwOhWV-IsnrGFTApefVBSOhcfDZqa

经过修改之后

public void exportWenjuanWord(SurveysMVO survey) throws SysException {
   createFile("C:/"+survey.getSurveyTitle()+".doc",survey);
 }
 
 
 public static boolean createFile(String destDirName,SurveysMVO survey) {
   try { 
    File file = new File(destDirName);
     if (file.exists()) {
      file.delete();// 如果存在就删除该文本
     }
     file.createNewFile();
     BufferedWriter output = new BufferedWriter(new FileWriter(file));
     output.write(survey.getSurveyTitle());
     output.newLine();// 换行   可以多次使用的       O(∩_∩)O~ ---------
     output.newLine();// 换行
     output.newLine();// 换行
     output.newLine();// 换行
     List questionpageList=survey.getQuestionpageList();
     for(int i=0;i<questionpageList.size();i++){
      PageMVO pageMvo=(PageMVO) questionpageList.get(i);
      List questionList=pageMvo.getQuestionList();
      for(int j=0;j<questionList.size();j++){
       QuestionMVO questionMvo=(QuestionMVO)questionList.get(j);
       output.write("Q"+(j+1)+":"+questionMvo.getQtitle());
       output.newLine();// 换行
       List optionList=questionMvo.getOptionList();
       List  matrixrowList=questionMvo.getMatrixrowList();
       for(int k=0;k<optionList.size();k++){
        OptionMVO optionMvo=(OptionMVO) optionList.get(k);
        String QtId=  questionMvo.getQtId();
        if(QtId.equals("2")){
         output.write("●   "+optionMvo.getQcName());
          output.newLine();// 换行
        }
        if(QtId.equals("50")){
         output.write(optionMvo.getQcName()+"_____分");
          output.newLine();// 换行
        }
        if(QtId.equals("95")){
         output.write(optionMvo.getQcName()+"_____");
          output.newLine();// 换行
        }
        if(QtId.equals("3")){
         output.write("●   "+optionMvo.getQcName());
          output.newLine();// 换行
        }

        if(QtId.equals("6")){
         output.write("_______________________________");
          output.newLine();// 换行
        }
        
        if(QtId.equals("60")){
         output.write("【        】"+optionMvo.getQcName());
          output.newLine();// 换行
        }
        
        if(QtId.equals("4")){
               output.write("  "+optionMvo.getQcName()+"   ");
               output.newLine();// 换行
               for(int l=0;l<matrixrowList.size();l++){
          MatrixrowMVO matrixrowMvo=(MatrixrowMVO) matrixrowList.get(l);
          output.write(matrixrowMvo.getRowName()+"● ");
         }
               output.newLine();// 换行
        }
        if(QtId.equals("5")){
               output.write("  "+optionMvo.getQcName()+"   ");
               output.newLine();// 换行
               for(int l=0;l<matrixrowList.size();l++){
          MatrixrowMVO matrixrowMvo=(MatrixrowMVO) matrixrowList.get(l);
          output.write(matrixrowMvo.getRowName()+"● ");
         }
               output.newLine();// 换行
        } 
        if(QtId.equals("100")){
               output.write("  "+optionMvo.getQcName()+"   ");
               output.newLine();// 换行
               for(int l=0;l<matrixrowList.size();l++){
          MatrixrowMVO matrixrowMvo=(MatrixrowMVO) matrixrowList.get(l);
          output.write(matrixrowMvo.getRowName()+"_______ ");
         }
               output.newLine();// 换行
        }  
        if(QtId.equals("7")){
               output.write("  "+optionMvo.getQcName()+"   ");
               output.newLine();// 换行
               for(int l=0;l<matrixrowList.size();l++){
          MatrixrowMVO matrixrowMvo=(MatrixrowMVO) matrixrowList.get(l);
          output.write(matrixrowMvo.getRowName()+"☆☆☆☆☆ ");
         }
               output.newLine();// 换行
        } 

        
       }
       output.newLine();// 换行
       output.newLine();// 换行
      }
     }
    
     
     output.close();
     file = null;
    } catch (Exception e) {
     e.printStackTrace();
    }
   return true;
 }

 3.以下是POI导出PPT 为转载,不是原创,原文章地址为:

1.http://www.iteye.com/topic/566798


0 0
原创粉丝点击