我在SQL Server 2000中获取数据,并且有一个HyperLink转至传递表单,其代码隐藏将数据输出到Excel文件。我一直在关注这个教程:将DataReader行写入Excel文件
http://www.dzone.com/links/r/export_gridview_to_excelcsv_in_net_using_c.html
我已经成功地从DataReader的输出一些样本值。我遇到的第一个问题是1.1中没有DataTable Load方法。我有数据回来通过DataReader的,但我需要的是如何创建的标题和输出它们,与数据行一起,到Excel文件帮助什么...
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
string attachment
= "attachment;filename=Report_" + DateTime.Now.ToString() + ".xls";
Response.AddHeader("content-disposition", attachment);
Response.Charset = string.Empty;
Response.Cache.SetCacheability(System.Web.HttpCacheability.Public);
Response.ContentType = "application/ms-excel";
DataTable dt = new DataTable();
dt.Columns.Add("Company");
dt.Columns.Add("Address1");
dt.Columns.Add("Address2");
dt.Columns.Add("City");
dt.Columns.Add("State");
dt.Columns.Add("ZipCode");
SqlConnection con = new SqlConnection();
SqlCommand com = new SqlCommand();
con.ConnectionString = "myconnstring";
com.Connection = con;
com.CommandText
= "SELECT DISTINCT Company, Address1, Address2, City, State, ZipCode" +
" FROM Vendor_View";
con.Open();
SqlDataReader dr = com.ExecuteReader();
while(dr.Read())
{
// how to grab and output data to Excel?
}
那么标题(列名称)呢? – IrishChieftain 2012-02-02 21:34:57
这将是您排出的第一行... – NotMe 2012-02-02 21:36:19
@IrishChieftain:查看更新 – NotMe 2012-02-08 17:03:04