2011-09-22 74 views
1

我需要將表格中的數據下載到excel電子表格中。我目前可以在網站上創建excel電子表格,但是如何將數據導入電子表格?如何將數據從ASP.Net網頁下載到Excel文件?

protected void btn_ExcelDownload_Click(object sender, EventArgs e) 
{ 
    string path = Server.MapPath(""); 
    path = path + @"\Resources\MessageStatus.xlsx"; 
    string name = Path.GetFileName(path); 
    Response.AppendHeader("content-disposition", "attachment; filename=" + name); 
    Response.ContentType = "Application/msword"; 
    Response.WriteFile(path); 
    Response.End(); 
} 
+0

你要的內容,以Excel表格導出做到了這一點。如果你使用網格,你可以輕鬆地做到這一點,否則你必須編寫自己的出口邏輯。 – Asdfg

回答

1

我已經使用這個類

void WriteToXls(string fromfilePath, string targetFileName) 
    { 
     if (!String.IsNullOrEmpty(fromfilePath)) 
     { 
      HttpResponse response = HttpContext.Current.Response; 
      response.Clear(); 
      response.Charset = "utf-8"; 
      response.ContentType = "text/xls"; 
      response.AddHeader("content-disposition", string.Format("attachment; filename={0}", targetFileName)); 
      response.BinaryWrite(File.ReadAllBytes(fromfilePath)); 
      response.End(); 
     } 
    }