利用EXCEL XML方式导出EXCEL,速度超快
2010-11-04 10:15
274 查看
之前博客中也有其他的导出方式,但是最终发现,利用XML方式速度超快,下面是转载的代码
<%@ Page Language="C#" EnableViewState="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));
System.Random rd = new System.Random();
for (int i = 0; i < 88; i++)
{
dr = dt.NewRow();
dr[0] = "班级" + i.ToString();
dr[1] = "" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 0);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 0);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 0);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 0);
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//假如每10条数据放在一个 Sheet 里面,先计算需要多少个 Sheet
int ItenCountPerSheet = 10;
int SheetCount = Convert.ToInt32(Math.Ceiling((double)GridView1.Rows.Count / ItenCountPerSheet));
Response.ClearContent();
Response.BufferOutput = true;
Response.Charset = "utf-8";
Response.ContentType = "text/xml";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename="+Server.UrlEncode("Excel表格测试")+".xls");
Response.Write("<?xml version='1.0'?><?mso-application progid='Excel.Sheet'?>");
Response.Write(@"/r/n<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'
xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel'
xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet' xmlns:html='http://www.w3.org/TR/REC-html40'>");
Response.Write(@"/r/n<DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>");
Response.Write(@"/r/n<<Author>会</Author><LastAuthor>E</LastAuthor>
<Created>2010-09-08T14:07:11Z</Created><Company>mxh</Company><Version>1990</Version>");
Response.Write("/r/n</DocumentProperties>");
Response.Write(@"/r/n<Styles><Style ss:ID='Default' ss:Name='Normal'><Alignment ss:Vertical='Center'/>
<Borders/><Font ss:FontName='宋体' x:CharSet='134' ss:Size='12'/><Interior/><NumberFormat/><Protection/></Style>");
//定义标题样式
Response.Write(@"<Style ss:ID='Header'><Borders><Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/></Borders>
<Font ss:FontName='宋体' x:CharSet='134' ss:Size='18' ss:Color='#FF0000' ss:Bold='1'/></Style>");
//定义边框
Response.Write(@"<Style ss:ID='border'><NumberFormat ss:Format='@'/><Borders>
<Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/></Borders></Style>");
Response.Write("</Styles>");
for (int i = 0; i < SheetCount; i++)
{
//计算该 Sheet 中的数据起始行和结束行。
int start = ItenCountPerSheet * i;
int end = ItenCountPerSheet * (i + 1);
if (end > GridView1.Rows.Count) end = GridView1.Rows.Count;
Response.Write("/r/n<Worksheet ss:Name='Sheet" + (i+1) + "'>");
Response.Write("/r/n<Table x:FullColumns='1' x:FullRows='1'>");
//输出标题
Response.Write("/r/n<Row ss:AutoFitHeight='1'>");
for (int j = 0; j < GridView1.HeaderRow.Cells.Count; j++)
{
Response.Write("<Cell ss:StyleID='Header'><Data ss:Type='String'>" + GridView1.HeaderRow.Cells[j].Text + "</Data></Cell>");
}
Response.Write("/r/n</Row>");
for (int j = start; j < end; j++)
{
Response.Write("/r/n<Row>");
for (int c = 0; c < GridView1.HeaderRow.Cells.Count; c++)
{
//对于数字,采用Number数字类型
if (c > 1)
{
Response.Write("<Cell ss:StyleID='border'><Data ss:Type='Number'>" + GridView1.Rows[j].Cells[c].Text + "</Data></Cell>");
}
else
{
Response.Write("<Cell ss:StyleID='border'><Data ss:Type='String'>" + GridView1.Rows[j].Cells[c].Text + "</Data></Cell>");
}
}
Response.Write("/r/n</Row>");
}
Response.Write("/r/n</Table>");
Response.Write("/r/n</Worksheet>");
Response.Flush();
}
Response.Write("/r/n</Workbook>");
Response.End();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出测试" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
<input type=button onclick="opens()" value="open"/>
<input type=button onclick="closes()" value="close"/>
<script>
var opentest;
function opens(){
opentest=window.open('test1.html');
}
function closes(){
opentest.close();
}
</script>
</body>
</html>
<%@ Page Language="C#" EnableViewState="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));
System.Random rd = new System.Random();
for (int i = 0; i < 88; i++)
{
dr = dt.NewRow();
dr[0] = "班级" + i.ToString();
dr[1] = "" + i.ToString();
dr[2] = System.Math.Round(rd.NextDouble() * 100, 0);
dr[3] = System.Math.Round(rd.NextDouble() * 100, 0);
dr[4] = System.Math.Round(rd.NextDouble() * 100, 0);
dr[5] = System.Math.Round(rd.NextDouble() * 100, 0);
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//假如每10条数据放在一个 Sheet 里面,先计算需要多少个 Sheet
int ItenCountPerSheet = 10;
int SheetCount = Convert.ToInt32(Math.Ceiling((double)GridView1.Rows.Count / ItenCountPerSheet));
Response.ClearContent();
Response.BufferOutput = true;
Response.Charset = "utf-8";
Response.ContentType = "text/xml";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename="+Server.UrlEncode("Excel表格测试")+".xls");
Response.Write("<?xml version='1.0'?><?mso-application progid='Excel.Sheet'?>");
Response.Write(@"/r/n<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'
xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel'
xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet' xmlns:html='http://www.w3.org/TR/REC-html40'>");
Response.Write(@"/r/n<DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>");
Response.Write(@"/r/n<<Author>会</Author><LastAuthor>E</LastAuthor>
<Created>2010-09-08T14:07:11Z</Created><Company>mxh</Company><Version>1990</Version>");
Response.Write("/r/n</DocumentProperties>");
Response.Write(@"/r/n<Styles><Style ss:ID='Default' ss:Name='Normal'><Alignment ss:Vertical='Center'/>
<Borders/><Font ss:FontName='宋体' x:CharSet='134' ss:Size='12'/><Interior/><NumberFormat/><Protection/></Style>");
//定义标题样式
Response.Write(@"<Style ss:ID='Header'><Borders><Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/></Borders>
<Font ss:FontName='宋体' x:CharSet='134' ss:Size='18' ss:Color='#FF0000' ss:Bold='1'/></Style>");
//定义边框
Response.Write(@"<Style ss:ID='border'><NumberFormat ss:Format='@'/><Borders>
<Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/>
<Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/></Borders></Style>");
Response.Write("</Styles>");
for (int i = 0; i < SheetCount; i++)
{
//计算该 Sheet 中的数据起始行和结束行。
int start = ItenCountPerSheet * i;
int end = ItenCountPerSheet * (i + 1);
if (end > GridView1.Rows.Count) end = GridView1.Rows.Count;
Response.Write("/r/n<Worksheet ss:Name='Sheet" + (i+1) + "'>");
Response.Write("/r/n<Table x:FullColumns='1' x:FullRows='1'>");
//输出标题
Response.Write("/r/n<Row ss:AutoFitHeight='1'>");
for (int j = 0; j < GridView1.HeaderRow.Cells.Count; j++)
{
Response.Write("<Cell ss:StyleID='Header'><Data ss:Type='String'>" + GridView1.HeaderRow.Cells[j].Text + "</Data></Cell>");
}
Response.Write("/r/n</Row>");
for (int j = start; j < end; j++)
{
Response.Write("/r/n<Row>");
for (int c = 0; c < GridView1.HeaderRow.Cells.Count; c++)
{
//对于数字,采用Number数字类型
if (c > 1)
{
Response.Write("<Cell ss:StyleID='border'><Data ss:Type='Number'>" + GridView1.Rows[j].Cells[c].Text + "</Data></Cell>");
}
else
{
Response.Write("<Cell ss:StyleID='border'><Data ss:Type='String'>" + GridView1.Rows[j].Cells[c].Text + "</Data></Cell>");
}
}
Response.Write("/r/n</Row>");
}
Response.Write("/r/n</Table>");
Response.Write("/r/n</Worksheet>");
Response.Flush();
}
Response.Write("/r/n</Workbook>");
Response.End();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出测试" />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
<input type=button onclick="opens()" value="open"/>
<input type=button onclick="closes()" value="close"/>
<script>
var opentest;
function opens(){
opentest=window.open('test1.html');
}
function closes(){
opentest.close();
}
</script>
</body>
</html>
相关文章推荐
- 导出Excel方式汇总-- 第二种利用oledb进行读取,需要安装AccessDatabaseEngine插件。
- 导出Excel方式汇总-- 第三种 利用 Npoi 插件,只需要在项目里引用DLL即可,无需在服务器安装任何插件。
- Jxls导出excel的若干方式总结(九)-- 通过excel模板,直观的利用输入输出流导出excel
- 利用OleDb方式对DataSet 和 Excel 数据快速导入导出
- 利用OleDb方式对DataSet 和 Excel 数据快速导入导出
- 利用OleDb方式对DataSet 和 Excel 数据快速导入导出
- 导出Excel方式汇总-- 第一种 利用office插件进行读取
- spingmvc 导出excel,浏览器进行下载的解决方式
- 快速导出数据到Excel(三):利用Excel内置功能
- EXCEL导出方式之HEADER导出HTML
- 利用注解反射生成导出excel文件
- java 项目利用POI导出excel总结
- Excel模板导出(针对复杂报表的一种解决方式)
- 利用SQL Server的DTS操作EXCEL、Access等数据表的导入导出
- java利用JXL导出/生成 EXCEL【my】
- javaweb 利用poi 导出excel
- C#中将数据导出为EXCEL方式汇总
- 利用phpExcel实现Excel数据的导入导出(全步骤详细解析)(转载粘贴的)
- jsp利用POI生成Excel并在页面中导出的示例
- 利用JXLS根据模板填充数据导出Excel实例