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();
  }
}

相关文章:

  • 2021-06-15
  • 2022-01-17
  • 2021-07-14
  • 2022-01-29
  • 2021-11-26
  • 2021-08-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-08
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2022-12-23
  • 2021-11-06
相关资源
相似解决方案