1\我需要在导出excel时候参数化的文件名
String filename="aa";
response.setheader("content-disposition", 
"attachment; filename="+filename)
这样为什么不行呢?2\一个将数据库里的数据分行导出到txt文件,中间的WriteToTxt函数该怎么写啊,怎么整合到一个javabean中,其中SQL和路径为参数?~由于是初次写JAVABEAN 好多地方不明白.(中间单独将数据写入TXT的代码没有问题)
package com;import java.sql.*;
import java.io.*;
public class writetotxt{
private static Connection con;
private Statement stmt;
private ResultSet rs;

private static final String drivername="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=db_check;user=sa;passwor=;";

public static synchronized Connection getCon() throws Exception{
try{
Class.forName(drivername);
con=DriverManager.getConnection(url);
return con;

}catch (SQLException e){
System.err.println(e.getMessage());
throw e;
}
}

public ResultSet executeQuery(String sql){
try{
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}catch(SQLException er){
System.err.print(er.getMessage());
}
return rs;
}
public  boolean WriteToTxt(String sqlstr,String path)//输出为txt格式
{

   Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sqlstr);

 //String path=request.getRealPath(".");
FileWriter fw=new FileWriter(path);//(path + "\\\\download\\WriteData.txt");//建立FileWriter对象,并实例化fw  int num=rs.getMetaData().getColumnCount();
 
   while (rs.next())
   {
String content="";
for (int j=1;j<=num;j++)
{   
content+=(rs.getString(j) + "\t");
}
fw.write(content);
fw.write("\r\n");

   }fw.close();

return true;
}
public void close(){
try{
if (rs!=null){
rs.close();
rs=null;
}
}catch(Exception e){

  System.err.println(e.getMessage());
  e.printStackTrace();
 }
 
try {
if(con!=null){
con.close();
con=null;
}
}catch(Exception e){

  System.err.println(e.getMessage());
  e.printStackTrace();
 }
}

}多谢多谢!~~

解决方案 »

  1.   

    1.要改保存时的默认文件名,只要改html的title值就可以了2public boolean WriteToTxt(String sqlstr,String path){ 
    boolean success=false;
    try{
    Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 
    rs=stmt.executeQuery(sqlstr); 
    FileWriter fw=new FileWriter("c:\\WriteData.txt");
    int num=rs.getMetaData().getColumnCount();  while (rs.next()) { 
    String content=""; 
    for (int j=1;j <=num;j++) {  
    content+=(rs.getString(j) + "\t"); 

    fw.write(content); 
    fw.write("\r\n"); 
    }  fw.close(); 
    success=true;
    }catch(Exception e){
    ///异常处理
    }
    return success; 

      

  2.   

    //String path=request.getRealPath("."); 
    FileWriter fw=new FileWriter(path);//(path + "\\\\download\\WriteData.txt");//建立FileWriter对象,并实例化fw
    我个人认为:path
    需要转码,否则IE会认不出path
    new String(path.getbitys("iso-8859-1"),gbk);
      

  3.   

    一:这是我以前写过的一个导出到Excel表格的代码,你试着看看,以前特意加了注释。
    二:建议将数据库中需要读取的数据一次读取完成,保存在ArrayList 类中,然后分批次写入txt文件中。package resultdata;import java.io.*;
    import jxl.*;
    import jxl.write.*;
    public class Excel{
     
    Workbook rwb = null;
    WritableWorkbook wwb= null;
    WritableSheet ws = null;
    String[]title={"id","Item1","Item2","Item3","Item4","Item5","Item6","Item7","Item8","Item9","Item10","Item11","Item12","Item13","Item14","Item15","Item16","Item17"};
    String[]result=null; 
    String path=""; 
     //构造函数  
     public Excel(){
     }
      /*========================================================
       === 函数功能:生成excel文件
       === 参    数
       ===     title:字段名称
       ===     path:文件生成路径
       ==========================================================*/  public void writeExcel(String[]title, String[] result,String path){    int i=0;
        int j=0;
       try {
         File f = new File(path);
        //如果path指定的文件不存在 调用创建它的方法
        if(!f.exists())
        createExcel(path);
        //  从输入流中创建Workbook
        InputStream is = new FileInputStream(path);
        rwb = Workbook.getWorkbook(is);
        Sheet st = rwb.getSheet(0);
        //获取已经写入的测试成绩行数
        int rsRows = st.getRows();
        /*
         * <p>description:获取保存成绩的列数 
         * 也就是有多少道题目  
         *  暂时确定为17道题目  
         *  所以暂时不需要读取</p>
         * @param int rsColumns = st.getColumns();
        */
        
         //读取已经存在的workbook  
         wwb = Workbook.createWorkbook(new File(path),rwb);
         ws = wwb.getSheet(0);
         
         if(rsRows==0){
      for( i=0;i<title.length;i++){
           WritableFont wf = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD, false);
           WritableCellFormat wcfF = new WritableCellFormat(wf);
           Label labelCF = new Label(i, 0, title[i],wcfF);
           ws.addCell(labelCF);
          }
      rsRows+=1; //已经写入的测试成绩数据行+1 
         }
         //写入本次测试成绩的数据 实际每次写入一次      for(j=0;j<result.length;j++)
          {
           //Label label = new Label()
           Label labelC = new Label(j,rsRows, result[j]);
           ws.addCell(labelC);
          }  
        //写入Exel工作表
        wwb.write();
        //关闭Excel工作薄对象
        wwb.close();
      }
      catch (Exception e) {
        System.out.println(e.toString());
      }
    }   public void beginWrite(String result,String path)
       {
         //将传递进来的string分解成string数组
         for(int i=0;i<result.length();i++)
         this.result[i]=result.substring(i,i+1);
         this.path=setPath(path);
         
         //调用写入本次测试成绩的方法
         writeExcel(this.title,this.result,this.path); 
       }
       //设置正确文件路径 为类文件主目录下
       public String setPath(String path)
    {
    String stemp=path;
    int i=0;
    //如果传入的目录名中有空格 则过滤掉该空格代码
     if((i=stemp.indexOf("%20"))!=-1)
    {
      stemp=stemp.replaceAll("%20", " ");
    }
    //设置文档名
    //取得真正的无转化文件物理路径
    this.path=stemp;
    this.path=this.path.replaceAll("file:/", "");
    this.path=this.path+"result.csv";
    return this.path;
    }
       //不存在 被调用创建它 令表名为sheet1
       private void createExcel(String path)
       {
        try {
    wwb = Workbook.createWorkbook(new FileOutputStream(path));
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
         ws = wwb.createSheet("sheet1", 0);
         try {
    wwb.write();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
         //关闭Excel工作薄对象
         try {
    wwb.close();
    } catch (WriteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       }public void setTitle(String[] title) {
    this.title = title;
    }
    }