您的位置:首页 > 编程语言 > ASP

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

2012-05-25 15:16 417 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: