private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear(); 
Response.Buffer= true; 
Response.Charset="GB2312";    
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls"); 
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
this.EnableViewState = false;    
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter); 
Response.Write(oStringWriter.ToString());
Response.End();
}
上面代码是我在DataGrid1的数据存放在excel中的程序,那位大哥能帮我把生成的excel文件上传到服务器文件夹里,我就奉送相送100分!!做项目急!!谢谢!!!!!!!!!!!

解决方案 »

  1.   

    帮我改成能把我生成的excel文件上传到服务器文件夹里,成为统一可运行的程序,谢谢!!!
      

  2.   

    System.Web.HttpPostedFile aa=new HttpPostedFile();
    aa.FileName=FileName.xls;//客户端路径;
    aa.SaveAs(???????)//服务器路径及名称,(相对的也可)
      

  3.   

    第二种方法:System.Net.WebClient aa=new System.Net.WebClient();
    aa.UploadFile(?????,???????)两种方法都请注意路径
      

  4.   

    public void Export2Excel(Object Sender, EventArgs E)
    {
    // 
    System.Data.SqlClient.SqlConnection SqlConn =this.sc;
       
    //   
    System.IO.StreamWriter w= new System.IO.StreamWriter(@"d:/leson_li/money.xls", false, System.Text.Encoding.Default);
    SqlConn.Open();

    try
    {
    System.Data.SqlClient.SqlCommand SqlCmd = SqlConn.CreateCommand();
    SqlCmd.CommandText = "Select * From authors"; 
    System.Data.SqlClient.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(); 
    //Response.Redirect (@"d:/leson_li/money.xls",true);
    // window.showModelessDialog('myttt.html','','dialogWidth:1px;dialogHeight:1px;dialogTop:1;dialogLeft:1;');
    //document.all.a1.focus();
    Response.Write("<script language=javascript>wop=window.open('d:/leson_li/money.xls','aaaaa')</script>");

    }
    catch
    {
    w.Close();
    SqlConn.Close();
    return;
    }   
    }
    使用你的方法会在客户端下载,上面这段代码对你有帮助,这是生成服务器端的,
    我不知你到底想要什么,两种方法你自己看看.
      

  5.   

    System.Web.HttpPostedFile aa=new HttpPostedFile();
    aa.FileName=FileName.xls;//客户端路径;
    aa.SaveAs(???????)//服务器路径及名称,(相对的也可)
      

  6.   


    卐㊣MRJQ卐㊣ 21:22:36
    WebClient.UploadFile 方法  [C#]请参见
    WebClient 类 | WebClient 成员 | System.Net 命名空间 | C++ 托管扩展编程 
    语言
    C#
    全部显示
    将本地文件上载到具有指定 URI 的资源。重载列表
    将指定的本地文件上载到具有指定 URI 的资源。[C#] public byte[] UploadFile(string, string);
    使用指定的方法将指定的本地文件上载到指定的资源。[C#] public byte[] UploadFile(string, string, string);[C#] 
    Console.Write("\nPlease enter the URL to post data to : ");
    String uriString = Console.ReadLine();// Create a new WebClient instance.
    WebClient myWebClient = new WebClient();Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URL");
    string fileName = Console.ReadLine();Console.WriteLine("Uploading {0} to {1} ...",fileName,uriString);                        
    // Upload the file to the URL using the HTTP 1.0 POST.
    byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);// Decode and display the response.
    Console.WriteLine("\nResponse Received.The contents of the file uploaded are: \n{0}",Encoding.ASCII.GetString(responseArray));
      

  7.   

    邮件发不出,不知谁的原因;SqlCmd.CommandText = "Select * From authors"; 
    System.Data.SqlClient.SqlConnection SqlConn =this.sc;
    //SQL语句和链接;System.IO.StreamWriter w= new System.IO.StreamWriter(@"d:/leson_li/money.xls", false, System.Text.Encoding.Default);
    //建立一个输出流;
    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(); //上面为写入数据;
    //下面的可不用,意为在网页上输出;
    //Response.Redirect (@"d:/leson_li/money.xls",true);
    // window.showModelessDialog('myttt.html','','dialogWidth:1px;dialogHeight:1px;dialogTop:1;dialogLeft:1;');
    //document.all.a1.focus();
    Response.Write("<script language=javascript>wop=window.open('d:/leson_li/money.xls','aaaaa')</script>");

      

  8.   

    System.Web.HttpPostedFile aa=new HttpPostedFile();
    aa.FileName=FileName.xls;
    aa.SaveAs(....)
      

  9.   

    //上传文件在服务器上存放的根目录
    string sSavePath = ConfigurationSettings.AppSettings["UploadPath"];
    //相对路径
    string sRelPath = ""; //上传文件
    if(flFile.Value.Trim() != "")
    {
    if(flFile.PostedFile == null) //验证文件是否正确的文件
    {
    this.RegisterStartupScript("jsSelFile","<script language=javascript>alert('请正确选择您要上传的月份生产计划!')</script>");
    return;
    }
    else
    {
    //设置分隔符
    char [] sepChar = new char [] {'\\'};
    string [] arrFilePath = flFile.PostedFile.FileName.Split(sepChar);
    //获得上传文件的文件名
    string sFN = arrFilePath[arrFilePath.Length - 1]; int iPos = sFN.LastIndexOf(".");
    sFN = sFN.Substring(0,iPos); //重命名文件名为当前时间
    sFN = DateTime.Now.ToString("yyyyMMddHHmmssms"); //获得上传文件的扩展名
    string sExt = System.IO.Path.GetExtension(flFile.PostedFile.FileName);

    //获得新文件名
    sFN += sExt;

    //文件在数据库中存放的相对路径
    sRelPath += "ManagementFiles" + "\\" + DateTime.Now.ToString("yyyy") + "\\"; //自动创建文件夹
    sSavePath += sRelPath; if(!Directory.Exists(sSavePath))
    {
    Directory.CreateDirectory(sSavePath);
    }
    sRelPath += sFN;
    //文件在服务器上存放的全路径
    sSavePath += sFN; //上传文件
    try
    {
    flFile.PostedFile.SaveAs(sSavePath);
    }
    catch(Exception ex)
    {
    Response.Redirect("../SysError.aspx?SysErrorInfo=" + HttpUtility.HtmlEncode(ex.Message)); return;
    } }
    }
    //-----------上传结束-----------------------