Some time ago, a friend. To complete this feature, the data is imported into the DataGrid and exported to Excel. This is really superfluous.
Solution:
read out data through LINQ and write directly to the data stream
The code is as follows:
Copy Code code as follows:
public partial class DataToExcel:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Dataaccess.dataclassesdatacontext db = new Dataclassesdatacontext ();
var qu = from T in db. Txlinfos
Select T;
Response.appendheader ("Content-disposition", "Attachment;filename=result.xls");
Response.ContentType = "Application/ms-excel";
Response.Charset = "gb2312";
response.contentencoding = encoding.getencoding ("gb2312");
System.IO.StringWriter writer = new System.IO.StringWriter ();
foreach (Txlinfo item in Qu)
{
Writer. Write (item. Gqname);
Writer. Write ("T");
Writer. Write (item. GQID);
Writer. WriteLine ();
}
Response.Write (writer. ToString ());
Response.End ();
}
}
Note: "\ t" defaults to the separator between two columns in Excel