高分分提问,excel 导出,提示CSS丢失问题
DataTable ds = GetDataTable(); StringBuilder sbContent = new StringBuilder(); if (ds.Rows.Count > 0) { for (int i = 0; i < ds.Rows.Count; i++) { sbContent.Append("<tr style=\"height:30px\">"); sbContent.Append("<td style='text-align:left;vnd.ms-excel.numberformat: @'>" + Convert.ToString(i + 1) + "</td>"); sbContent.Append("<td style='text-align:left;vnd.ms-excel.numberformat: @'>" + ds.Rows[i]["LR_NAME"].ToString() + "</td>"); sbContent.Append("<td style='text-align:left;vnd.ms-excel.numberformat: @'>" + ds.Rows[i]["LR_SEX"].ToString() + "</td>"); sbContent.Append("<td style='text-align:left;vnd.ms-excel.numberformat: @'>" + ds.Rows[i]["LR_SFZ"].ToString() + "</td>"); sbContent.Append("<td style='text-align:left;vnd.ms-excel.numberformat: @'>" + ds.Rows[i]["LR_ZZ"].ToString() + "</td>"); sbContent.Append("<td style='text-align:left;vnd.ms-excel.numberformat: @'>" + ds.Rows[i]["LR_TEL"].ToString() + "</td>"); sbContent.Append("<td style='text-align:left;vnd.ms-excel.numberformat: @'>" + ds.Rows[i]["LR_TEL2"].ToString() + "</td>"); sbContent.Append("<td style='text-align:left;vnd.ms-excel.numberformat: @'>" + ds.Rows[i]["LR_SJ"].ToString() + "</td>"); sbContent.Append("<td style='text-align:left;vnd.ms-excel.numberformat: @'>" + ds.Rows[i]["LR_RY"].ToString() + "</td>"); sbContent.Append("<td style='text-align:left;vnd.ms-excel.numberformat: @'>" + ds.Rows[i]["LR_BZ"].ToString() + "</td>"); sbContent.Append("</tr>"); } } StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" > "); sbHtml.Append("<tr>"); sbHtml.Append("<th width=50 >序号</th>"); sbHtml.Append("<th width=100>姓名</th>"); sbHtml.Append("<th width=70 >性别</th>"); sbHtml.Append("<th width=200 >身份证号码</th>"); sbHtml.Append("<th width=200 >家庭住址</th>"); sbHtml.Append("<th width=200 >手机号码</th>"); sbHtml.Append("<th width=200 >固定电话</th>"); sbHtml.Append("<th width=200 >录入时间</th>"); sbHtml.Append("<th width=110 >录入人员</th>"); sbHtml.Append("<th width=200 >备注</th>"); sbHtml.Append("</tr>"); sbHtml.Append(sbContent); sbContent.Append(" </table>"); sbHtml.Append("</table>"); Response.Clear(); Response.ContentType = "application nd.ms-excel"; Response.Charset = "gb2312"; Page.EnableViewState = false; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("客户录入名单") + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");//设置输出流为简体中文 Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\"><title> </title></head><body><center>"); Response.Write(sbHtml.ToString()); Response.Write("</center></body></html>"); Response.End(); }
public static void ExportToTable(System.Data.DataTable dtData, string filename) { XlsDocument xls = new XlsDocument(); Worksheet sheet = xls.Workbook.Worksheets.AddNamed(filename);//状态栏标题名称 int rowNum = dtData.Rows.Count; int columnNum = dtData.Columns.Count; int rowIndex = 1; int columnIndex = 0; //生成格式列 ColumnInfo colInfo = new ColumnInfo(xls, sheet); colInfo.ColumnIndexStart = 0; colInfo.ColumnIndexEnd = Convert.ToUInt16(columnNum); colInfo.Width = 20 * 256; sheet.AddColumnInfo(colInfo); //绑定标题 Cell cell = null; foreach (DataColumn dc in dtData.Columns) { columnIndex++; cell = sheet.Cells.AddValueCell(rowIndex, columnIndex, dc.ColumnName); //cell.HorizontalAlignment = HorizontalAlignments.Centered; //cell.Font.Weight = FontWeight.Bold; //cell.PatternColor = Colors.Default30; //cell.RightLineColor = Colors.Black; //cell.RightLineStyle = 1; //cell.BottomLineColor = Colors.Black; //cell.BottomLineStyle = 1; //cell.TopLineColor = Colors.Black; //cell.TopLineStyle = 1; cell.HorizontalAlignment = HorizontalAlignments.Centered; cell.VerticalAlignment = VerticalAlignments.Centered; cell.Pattern = 1; cell.PatternColor = Colors.EgaBlue; cell.UseBorder = true; cell.TopLineStyle = 1; cell.TopLineColor = Colors.Black; cell.BottomLineStyle = 1; cell.BottomLineColor = Colors.Black; cell.LeftLineStyle = 1; cell.LeftLineColor = Colors.Black; cell.RightLineStyle = 1; cell.RightLineColor = Colors.Black; cell.Font.Bold = true; cell.Font.Height = 11 * 20; cell.Font.ColorIndex = 1; } //绑定子项数据 for (int i = 0; i < rowNum; i++) { rowIndex++; columnIndex = 0; for (int j = 0; j < columnNum; j++) { columnIndex++; cell = sheet.Cells.AddValueCell(rowIndex, columnIndex, dtData.Rows[i][j].ToString()); cell.HorizontalAlignment = HorizontalAlignments.Centered; cell.TextWrapRight = true; cell.VerticalAlignment = VerticalAlignments.Top; if (j == 0) { cell.LeftLineColor = Colors.Black; cell.LeftLineStyle = 1; } cell.RightLineColor = Colors.Black; cell.RightLineStyle = 1; cell.BottomLineColor = Colors.Black; cell.BottomLineStyle = 1; } } //导出 xls.FileName = filename; xls.Send(); }