2011-09-22 78 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(); 
     } 
    }