protected void CreateExcel2() { Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("商品编码.xls", Encoding.UTF8).ToString()); Response.ContentType = "application/ms-excel"; //PageInfo<VW_CLN_LendAfter_CustDetail> PageToPtint = ViewEntitiy.VW_CLN_LendAfter_CustDetail.Query(0, 15, "select * from VW_CLN_LendAfter_CustDetail where 1=1 ", ToList); //DataSoucre = (DataSet)PageToPtint.DataList; //DataTable dt = DataSoucre.Tables[0]; //foreach (string key in TotableDC.Keys) //{ // if (dt.Columns.Contains(key)) // { // dt.Columns[key].ColumnName = TotableDC[key]; // } //} StringBuilder strexcel = new StringBuilder(); strexcel.Append(@"<table><tr>"); //导出标题
for (int i = 0; i < master.Columns.Count; i++) { { strexcel.Append(@"<th>" + (master.Columns[i] as GridViewDataColumn).FieldName + "</th>"); } }
strexcel.Append(@"</tr>"); //导出列值 for (int r = 0; r < master.VisibleRowCount; r++) { strexcel.Append(@"<tr>"); for (int i = 0; i < master.Columns.Count; i++) { string data = ""; if (master.GetRowValues(r, (master.Columns[i] as GridViewDataColumn).FieldName) != null) data = Convert.ToString(master.GetRowValues(r, (master.Columns[i] as GridViewDataColumn).FieldName)); strexcel.Append(@"<td style='vnd.ms-excel.numberformat:@'>" + data + @"</td>"); //if ((master.Columns[i] as GridViewDataColumn).FieldName == "条形码") //{ // strexcel.Append(@"<td style='vnd.ms-excel.numberformat:@'>" + data + @"</td>");//防止导出来的时候变成科学计数法,变成文本格式 //} //else { // strexcel.Append(@"<td>" + data + @"</td>"); //} } strexcel.Append(@"</tr>"); } strexcel.Append(@"<table>"); Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=utf-8\"/>" + strexcel); Response.End(); }
|
请发表评论