利用ASP.NET 导出excel(2007版)

http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

// In this example, I have a defined a List of my Employee objects.
class Employee;
List<Employee> listOfEmployees = new List<Employee>();

...

// The following code gets run when I click on my "Export to Excel" button.
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
  string filename = "\\\\MikesServer\\ExcelFiles\\Employees.xlsx";
  if (CreateExcelFile.CreateExcelDocument(listOfEmployees, filename))
  {
    // We successfully managed to export to an Excel file.
    // Now, get the ASP.Net application to open this Excel file, ready for the user to view.
    Response.ClearContent();
    FileStream fs1 = new FileStream(filename, FileMode.Open, FileAccess.Read);
    byte[] data1 = new byte[fs1.Length];
    fs1.Read(data1, 0, data1.Length);
  
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", filename));
    Response.BinaryWrite(data1);
    Response.End();
  }
}

原文地址:https://www.cnblogs.com/mqingqing123/p/2518074.html