ASP 最简单导出excel的方法
2017-11-27 10:59
387 查看
protected void lbt_Export_Click(object sender, EventArgs e)
{
Export("application/excel", DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
}
private void Export(string FileType, string FileName)
{
StringBuilder htmltable = new StringBuilder();
htmltable.Append("<table border='1' cellspacing='0' cellpadding='0'>");//添加黑色边框 (选用)
htmltable.AppendFormat("<tr><td rowspan='2' style='width: 100px;'>" + Label5.Text + "</td><td align='center' style='width: 300px;' colspan='3'>" + Label6.Text + "</td></tr><tr><td style='width: 100px;'>市台</td><td style='width: 100px;'>中央台</td><td style='width:
100px;'>省台</td></tr><tr><td style='width: 100px;'>平均</td><td align='left'>" + Label7.Text + "</td><td align='left'>" + Label8.Text + "</td><td align='left'>" + Label9.Text + "</td></tr>");//添加表头
foreach (RepeaterItem item in rpData_Result.Items) //遍历表格
{
Label Label1 = (Label)item.FindControl("Label1");
Label Label2 = (Label)item.FindControl("Label2");
Label Label3 = (Label)item.FindControl("Label3");
Label Label4 = (Label)item.FindControl("Label4");
htmltable.AppendFormat("<tr><td align=\"left\">{0}</td><td align=\"left\">{1}</td><td align=\"left\">{2}</td><td align=\"left\">{3}</td></tr>", Label1.Text, Label2.Text, Label3.Text, Label4.Text);
}
htmltable.Append("</table>");
Response.Clear();
Response.BufferOutput = true;
Response.Charset = "utf-8";
//Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write(AddExcelHead());//显示网格线(选用)
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
string content = htmltable.ToString();
Response.Write("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns:m='http://schemas.microsoft.com/office/2004/12/omml' xmlns='http://www.w3.org/TR/REC-html40'><head></head><body
lang=ZH-CN>" + content);
Response.End();
}
public static string AddExcelHead()//显示网格线(选用)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
sb.Append(" <head>");
sb.Append(" <!--[if gte mso 9]><xml>");
sb.Append("<x:ExcelWorkbook>");
sb.Append("<x:ExcelWorksheets>");
sb.Append("<x:ExcelWorksheet>");
sb.App
4000
end("<x:Name></x:Name>");
sb.Append("<x:WorksheetOptions>");
sb.Append("<x:Print>");
sb.Append("<x:ValidPrinterInfo />");
sb.Append(" </x:Print>");
sb.Append("</x:WorksheetOptions>");
sb.Append("</x:ExcelWorksheet>");
sb.Append("</x:ExcelWorksheets>");
sb.Append("</x:ExcelWorkbook>");
sb.Append("</xml>");
sb.Append("<![endif]-->");
sb.Append(" </head>");
sb.Append("<body>");
return sb.ToString();
}
{
Export("application/excel", DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
}
private void Export(string FileType, string FileName)
{
StringBuilder htmltable = new StringBuilder();
htmltable.Append("<table border='1' cellspacing='0' cellpadding='0'>");//添加黑色边框 (选用)
htmltable.AppendFormat("<tr><td rowspan='2' style='width: 100px;'>" + Label5.Text + "</td><td align='center' style='width: 300px;' colspan='3'>" + Label6.Text + "</td></tr><tr><td style='width: 100px;'>市台</td><td style='width: 100px;'>中央台</td><td style='width:
100px;'>省台</td></tr><tr><td style='width: 100px;'>平均</td><td align='left'>" + Label7.Text + "</td><td align='left'>" + Label8.Text + "</td><td align='left'>" + Label9.Text + "</td></tr>");//添加表头
foreach (RepeaterItem item in rpData_Result.Items) //遍历表格
{
Label Label1 = (Label)item.FindControl("Label1");
Label Label2 = (Label)item.FindControl("Label2");
Label Label3 = (Label)item.FindControl("Label3");
Label Label4 = (Label)item.FindControl("Label4");
htmltable.AppendFormat("<tr><td align=\"left\">{0}</td><td align=\"left\">{1}</td><td align=\"left\">{2}</td><td align=\"left\">{3}</td></tr>", Label1.Text, Label2.Text, Label3.Text, Label4.Text);
}
htmltable.Append("</table>");
Response.Clear();
Response.BufferOutput = true;
Response.Charset = "utf-8";
//Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write(AddExcelHead());//显示网格线(选用)
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
string content = htmltable.ToString();
Response.Write("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns:m='http://schemas.microsoft.com/office/2004/12/omml' xmlns='http://www.w3.org/TR/REC-html40'><head></head><body
lang=ZH-CN>" + content);
Response.End();
}
public static string AddExcelHead()//显示网格线(选用)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
sb.Append(" <head>");
sb.Append(" <!--[if gte mso 9]><xml>");
sb.Append("<x:ExcelWorkbook>");
sb.Append("<x:ExcelWorksheets>");
sb.Append("<x:ExcelWorksheet>");
sb.App
4000
end("<x:Name></x:Name>");
sb.Append("<x:WorksheetOptions>");
sb.Append("<x:Print>");
sb.Append("<x:ValidPrinterInfo />");
sb.Append(" </x:Print>");
sb.Append("</x:WorksheetOptions>");
sb.Append("</x:ExcelWorksheet>");
sb.Append("</x:ExcelWorksheets>");
sb.Append("</x:ExcelWorkbook>");
sb.Append("</xml>");
sb.Append("<![endif]-->");
sb.Append(" </head>");
sb.Append("<body>");
return sb.ToString();
}
相关文章推荐
- ASP.NET中导出excel/word等类型文件的简单方法
- ASP导出为Word或Excel的最简单方法
- ASP.net中导出Excel的简单方法介绍
- ASP.NET中导出excel/word等类型文件的简单方法
- asp导出excel文件最简单方便的方法
- ASP.NET中导出excel/word等类型文件的简单方法
- ASP.NET中导出excel/word等类型文件的简单方法
- asp导出excel最简单最有效的方法
- ASP.NET中导出excel/word等类型文件的简单方法
- asp.net导出excel的简单方法实例
- asp.net导出excel的简单方法实例
- asp导出excel文件最简单方便的方法
- asp导出excel文件最简单方便的方法
- ASP.net中导出Excel的简单方法
- [转]asp.net导出数据到Excel的三种方法
- asp.net中导出Excel的几种方法(示例)
- asp.net导出Excel文件之方法比较
- ASP导出Excel数据的四种方法
- asp.net导出数据到EXCEL简单有效
- asp生成和导出excel和word数据源码和代码,简单好用(已经测试可以用)