public class ExportToExcel
{
public ExportToExcel()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static void ToExcel(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
//处理Gridview导出Excel表没有网格线的解决办法
sw.WriteLine("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine("<head>");
sw.WriteLine("<!--[if gte mso 9]>");
sw.WriteLine("<xml>");
sw.WriteLine(" <x:ExcelWorkbook>");
sw.WriteLine(" <x:ExcelWorksheets>");
sw.WriteLine(" <x:ExcelWorksheet>");
sw.WriteLine(" <x:WorksheetOptions>");
sw.WriteLine(" <x:Print>");
sw.WriteLine(" <x:ValidPrinterInfo />");
sw.WriteLine(" </x:Print>");
sw.WriteLine(" </x:WorksheetOptions>");
sw.WriteLine(" </x:ExcelWorksheet>");
sw.WriteLine(" </x:ExcelWorksheets>");
sw.WriteLine("</x:ExcelWorkbook>");
sw.WriteLine("</xml>");
sw.WriteLine("<![endif]-->");
sw.WriteLine("</head>");
//
HtmlTextWriter htw = new HtmlTextWriter(sw);
Table tb = gridView2Table(gv);
tb.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
protected static Table gridView2Table(GridView gv)
{
Table tb = new Table();
tb.Attributes.Add("style", "border:.5pt solid black;font-size:24px:font-family: 宋体, Arial;");
tb.Attributes.Add("align", "left");
TableRow tr_header = new TableRow();
tr_header.Attributes.Add("height", "19");
for (int k = 0; k < gv.HeaderRow.Cells.Count; k++)
{
if (gv.Columns[k].Visible)
{
if (gv.Columns[k].HeaderStyle.CssClass != "hidden")
{
TableCell tc = new TableCell();
tc.Attributes.Add("style", "border:.5pt solid black;");
tc.Attributes.Add("align", "left");
tc.Text = gv.Columns[k].HeaderText.ToString();
tr_header.Cells.Add(tc);
}
}
}
tb.Rows.Add(tr_header);//添加表头行
for (int i = 0; i < gv.Rows.Count; i++)
{
TableRow tr = new TableRow();
tr.Attributes.Add("height", "19");
for (int j = 0; j < gv.Columns.Count; j++)
{
if (gv.Columns[j].Visible)
{
if (gv.Columns[j].HeaderStyle.CssClass != "hidden")
{
TableCell tc = new TableCell();
tc.Attributes.Add("style", "border:.5pt solid black;");
tc.Attributes.Add("align", "left");
string text = handleControl(gv.Rows[i].Cells[j]);
tc.Text = text;
tr.Cells.Add(tc);
}
}
}
tb.Rows.Add(tr);
}
return tb;
}
protected static string handleControl(TableCell tc)
{
string retStr = string.Empty;
if (tc.Controls.Count == 0)
{
retStr = tc.Text;
return retStr;
}
for (int i = 0; i < tc.Controls.Count; i++)
{
Control control = tc.Controls[i];
if (control is DropDownList)
{
DropDownList ddl = control as DropDownList;
if (ddl.Visible)
retStr += ddl.SelectedItem.Text.ToString() + " ";
}
if (control is Label)
{
Label lb = control as Label;
if (lb.Visible)
retStr += lb.Text.ToString() + " ";
}
if (control is Button)
{
Button btn = control as Button;
if (btn.Visible)
retStr += btn.Text.ToString() + " ";
}
if (control is LinkButton)
{
LinkButton lbtn = control as LinkButton;
if (lbtn.Visible)
retStr += lbtn.Text.ToString() + " ";
}
if (control is CheckBox)
{
CheckBox cb = control as CheckBox;
if (cb.Visible)
retStr += (cb.Checked == true ? "yes" : "no") + " ";
}
if (control is HyperLink)
{
HyperLink hl = control as HyperLink;
if (hl.Visible)
retStr += hl.Text.ToString() + " ";
}
}
return retStr;
}
}