我现在把dataset从streamwriter写到excel上,但是只能保存在服务器端,怎么让它可以保存在客户端上?help
例子是: dbObject db = new dbObject();
SqlConnection SqlConn = new SqlConnection(db.ConnectString);
StreamWriter w = new StreamWriter("C:\\vipcust_circuit".XLS", false,Encoding.Default);
SqlConn.Open();
            try
{
SqlCommand SqlCmd = SqlConn.CreateCommand();
SqlCmd.CommandText = Session["query"].ToString();
SqlDataReader Reader = SqlCmd.ExecuteReader();
for (int i = 0; i < Reader.FieldCount; ++i)
{
w.Write(Reader.GetName(i));
w.Write('\t');
}
w.Write("\r\n");
object[] values = new object[Reader.FieldCount];
while (Reader.Read())
{
Reader.GetValues(values);
for (int i = 0; i < values.Length; ++i)
{
w.Write(values[i].ToString());
w.Write('\t');
}
w.Write("\r\n");
}
w.Flush(); 
w.Close();
Reader.Close();
SqlConn.Close();
}
catch
{
w.Close();
SqlConn.Close();
return;
}
这个只能写到服务器端的c盘上

解决方案 »

  1.   

    /// <summary>
    /// 将DataTable中的数据导出到指定的Excel文件中
    /// </summary>
    /// <param name="page">Web页面对象</param>
    /// <param name="tab">包含被导出数据的DataTable对象</param>
    /// <param name="FileName">Excel文件的名称</param>
    public static void Export(System.Web.UI.Page page,System.Data.DataTable tab,string FileName)
    {
    System.Web.HttpResponse httpResponse = page.Response;
    System.Web.UI.WebControls.DataGrid dataGrid=new System.Web.UI.WebControls.DataGrid();
    dataGrid.DataSource=tab.DefaultView;
    dataGrid.AllowPaging = false;
    dataGrid.HeaderStyle.BackColor = System.Drawing.Color.Green;
    dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
    dataGrid.HeaderStyle.Font.Bold = true;
    dataGrid.DataBind();
    httpResponse.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8)); //filename="*.xls";
    httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
    httpResponse.ContentType ="application/ms-excel";
    System.IO.StringWriter  tw = new System.IO.StringWriter() ;
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
    dataGrid.RenderControl(hw);

    string filePath = page.Server.MapPath("..")+"\\Files\\" +FileName;
    System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
    sw.Write(tw.ToString());
    sw.Close(); DownFile(httpResponse,FileName,filePath);

    httpResponse.End();
    }private static bool DownFile(System.Web.HttpResponse Response,string fileName,string fullPath)
    {
    try
    {
    Response.ContentType = "application/octet-stream";

    Response.AppendHeader("Content-Disposition","attachment;filename=" + 
    HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + ";charset=GB2312");
    System.IO.FileStream fs= System.IO.File.OpenRead(fullPath);
    long fLen=fs.Length;
    int size=102400;//每100K同时下载数据 
    byte[] readData = new byte[size];//指定缓冲区的大小 
    if(size>fLen)size=Convert.ToInt32(fLen);
    long fPos=0;
    bool isEnd=false;
    while (!isEnd) 

    if((fPos+size)>fLen)
    {
    size=Convert.ToInt32(fLen-fPos);
    readData = new byte[size];
    isEnd=true;
    }
    fs.Read(readData, 0, size);//读入一个压缩块 
    Response.BinaryWrite(readData);
    fPos+=size;

    fs.Close(); 
    System.IO.File.Delete(fullPath);
    return true;
    }
    catch
    {
    return false;
    }
    }
      

  2.   

    参考::
    http://dotnet.aspx.cc/ShowDetail.aspx?id=8A4CBF47-B888-4832-3389-ED3A3A3C8AAB
      

  3.   


    你可以dataset 导入 excel参考
    http://community.csdn.net/Expert/topic/3077/3077526.xml?temp=.8746912
    http://www.dev-club.com/club/bbs/showEssence.asp?id=26350http://dev.csdn.net/Develop/article/18/18623.shtm
    http://community.csdn.net/Expert/topic/3112/3112296.xml?temp=.926861
    http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50
    http://expert.csdn.net/Expert/TopicView1.asp?id=2928057www.foxhis.com/powermjtest/
    原文代码:
    private void Button1_Click(object sender, System.EventArgs e)
    {
      //写入Excel的方法:
      //定义需要参数。
      string SourceFile="Data.XLS";                                //源文件名称。
      string TemplatePath=Server.MapPath("ExcelTemplate");    //存放源文件的文件夹路径。
      string DownloadPath=Server.MapPath("ExcelDownload");    //副本的文件夹路径。
      //副本的文件名。
      string TempFileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".XLS";  
      object missing = System.Reflection.Missing.Value;
      Excel.Application myExcel=new Excel.Application();
      //打开新文件
      myExcel.Application.Workbooks.Open(TemplatePath+"\\"+SourceFile,missing,missing,missing,missing,
    missing,missing,missing,missing,missing,missing, missing,missing); 
      Excel.Workbook myBook=myExcel.Workbooks[1];
      Excel.Worksheet curSheet = (Excel.Worksheet)myBook.Sheets[2];

      string DownloadFilePath=DownloadPath+"\\"+TempFileName;

      int i=0;
      while (i<=10)
      {
        myExcel.Cells[4+i,2]=i.ToString();
        myExcel.Cells[4+i,3]=i.ToString();
        myExcel.Cells[4+i,4]=i.ToString();
        myExcel.Cells[4+i,5]=i.ToString();
        myExcel.Cells[4+i,6]=i.ToString();
        i++;
      }   

      myBook.Saved=true;
      //myBook.SaveAs(DownloadFilePath,missing,"","",false,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,missing,missing);

      myBook.PrintPreview(0);
      //myBook.PrintOut(missing,missing,missing,missing,missing,missing,missing,missing);
      myBook.Close(false, null,null);
      myExcel.Quit();
      System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
      myBook = null;
      myExcel = null;
      GC.Collect();
      //Response.Redirect("ExcelDownload//"+TempFileName); //下载文件
    }