2012-09-27 21 views
0

我有一个结果集,我必须将结果集中可用的所有数据写入文本文件并填充到用户下载。将结果集导出到java中的对话框中的文本文件

我已经做了下面的代码导出到excel使用poi,同样的方式如何做文本文件。

if(exportTo.equals("excel")) 
     { 
      response.setContentType("application/vnd.ms-excel"); 
      response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".xls\""); 
      try { 
       HSSFWorkbook hwb = new HSSFWorkbook(); 
       HSSFSheet sheet = hwb.createSheet(reportName); 
       HSSFRow row = null; 

       HSSFHeader header = sheet.getHeader(); 
       header.setCenter("POC"); 
       header.setLeft("POC"); 
       header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") + 
           HSSFHeader.fontSize((short) 16) + reportName); 

       //to add water mark 
       /*HSSFPatriarch dp = sheet.createDrawingPatriarch(); 
       HSSFClientAnchor anchor = new HSSFClientAnchor 
        (0, 0, 1023, 255, (short) 2, 4, (short) 13, 26); 
       HSSFTextbox txtbox = dp.createTextbox(anchor); 
       HSSFRichTextString rtxt = new HSSFRichTextString("POC"); 
       HSSFFont draftFont = hwb.createFont(); 
       draftFont.setColor((short) 27); 
       draftFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); 
       draftFont.setFontHeightInPoints((short) 192); 
       draftFont.setFontName("Verdana"); 
       rtxt.applyFont(draftFont); 
       txtbox.setString(rtxt); 
       txtbox.setLineStyle(HSSFShape.LINESTYLE_NONE); 
       txtbox.setNoFill(true);*/ 

       HSSFCellStyle style = hwb.createCellStyle(); 
       style.setBorderTop((short) 6); // double lines border 
       style.setBorderBottom((short) 1); // single line border 
       style.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index); 

       HSSFFont font = hwb.createFont(); 
       font.setBoldweight((short) 700); 

       // Create Styles for sheet. 
       HSSFCellStyle headerStyle = hwb.createCellStyle(); 
       headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); 
       headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
       headerStyle.setFont(font); 
       headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); 
       headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); 
       headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); 
       headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); 
       headerStyle.setAlignment((short) 2); 

       // create Title for the sheet 
       HSSFCellStyle titleStyle = hwb.createCellStyle(); 

       HSSFFont titleFont = hwb.createFont(); 
       titleFont.setFontName(HSSFFont.FONT_ARIAL); 
       titleFont.setFontHeightInPoints((short) 15); 
       titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
       titleFont.setColor(HSSFColor.BLUE.index); 
       titleStyle.setFont(titleFont); 
       titleStyle.setAlignment((short)2); 

       row = sheet.createRow((short)1); 
       HSSFCell secondCell = row.createCell((short) 0); 
       secondCell.setCellValue(new HSSFRichTextString(reportName).toString()); 
       secondCell.setCellStyle(titleStyle);  
       sheet.addMergedRegion(new Region(1, (short)0, 1, (short)headerCount)); 

       int sno=0; 
       HSSFRow rowhead = sheet.createRow((short)4); 
       for (Iterator it = headerMap.keySet().iterator(); it.hasNext();) { 
        String headerName = (String) headerMap.get(it.next()); 
        HSSFCell headerCell = rowhead.createCell((short)sno); 
        headerCell.setCellStyle(headerStyle); 
        headerCell.setCellValue(headerName); 
        sno++; 
       } 

       HSSFCellStyle rowStyle=hwb.createCellStyle(); 
       rowStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); 
       rowStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); 
       rowStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); 
       rowStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); 
       rowStyle.setAlignment((short) 2); 

       row = custDAO.creadExcelTable(query, sheet, row,rowStyle); 
       hwb.write(response.getOutputStream()); 
       response.flushBuffer(); 

      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 

     } 
public HSSFRow creadExcelTable(String query,HSSFSheet sheet,HSSFRow row,HSSFCellStyle rowStyle){ 
     int numberOfColumns=0,sno=0,index=5,iterator=1; 
     Connection connection = getConnection(); 
     if (connection != null) { 
      try { 
       PreparedStatement reportTablePS = connection.prepareStatement(query); 
       ResultSet reportTable_rst = reportTablePS.executeQuery(); 
       ResultSetMetaData reportTable_rsmd = reportTable_rst.getMetaData(); 
       numberOfColumns = reportTable_rsmd.getColumnCount(); 
       int i =0; 
       while (reportTable_rst.next()) { 
        row = sheet.createRow((short)index); 
        sheet.setColumnWidth((short)index, (short)100); 

        /* if(i == 0){ 
         i = 1; 
         rowStyle.setFillForegroundColor(HSSFColor.BLUE_GREY.index); 
         rowStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
         System.out.println("BLUE_GREY"); 
        } 
        else { 
         i = 0; 
         System.out.println("LEMON"); 
         rowStyle.setFillForegroundColor(HSSFColor.LEMON_CHIFFON.index); 
         rowStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
        }*/ 

        HSSFCell serialCell = row.createCell((short)sno); 
        serialCell.setCellStyle(rowStyle); 
        serialCell.setCellValue(iterator); 

          for (int columnIterator = 1; columnIterator <= numberOfColumns; columnIterator++) { 
           String column = reportTable_rst.getString(columnIterator); 
           sheet.setColumnWidth((short)columnIterator, (short)3000); 
           HSSFCell rowCell = row.createCell((short)columnIterator); 
           rowCell.setCellStyle(rowStyle); 
           rowCell.setCellValue(column); 
          } 
          index++; 
          iterator++; 
      } 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      }finally { 
       try { 
        closeConnection(connection, null, null); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
      } 
     } 
     return row; 
    } 

现在我已经updto这个工作真不知道如何去

if(exportTo.equals("text")){ 
      response.setContentType("text/plain"); 
      response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".txt\""); 
      try { 

      } catch (Exception e) { 
       // TODO: handle exception 
      } 

     } 

这一个指定位置

Writer writer = null; 

try { 
    String text = "This is a text file"; 

    File file = new File("write.txt"); 
    writer = new BufferedWriter(new FileWriter(file)); 
    writer.write(text); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} finally { 
    try { 
     if (writer != null) { 
      writer.close(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

创建文件,但我想用对话框导出文件,请帮助我如何去做。

问候

+0

“使用对话框导出文件”的含义是什么,它与Excel导出的功能有什么不同? –

+0

我从数据库中获取数据并使用poi编写它们并导出它,当用户单击导出到excel时,保存/打开对话框随文件打开。我也想对文本文件做同样的处理,我的问题是如何将数据写入数据流并将其设置为导出 –

+0

将content-type设置为“text/plain”。你知道“export to excel”代码的工作原理吗? –

回答

1

我想你没有得到一个对话框,因为你的浏览器可以自行处理的文本文件。 浏览器读取http响应的MIME类型(已使用response.setContentType("text/plain");设置) 大多数浏览器自己打开html,图像和文本,并将其他文件类型(如音频,pdf或Office文档)重定向到其他应用程序。 因此,您可能需要调整浏览器设置。

相关问题