大家好,小弟最近在搞一个web的程序,用的是vb.net2005,在网上找到下面的代码,可惜是C#的,有点不明白,它的执行效果正好是我要的,请哪位高手,教教小弟。
<%@ Page Language="C#" %><script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
// 下面采用的是DataTable,也可以采用DataSet,其中每个DataTable可以保存成一个 Sheet
// 迅雷下载时可以在下载完毕后会自动把文件名更新成 xls 或者 xml 的。
System.Data.DataTable dt = new System.Data.DataTable();
if (!Page.IsPostBack)
{
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] = "【孟子E章】" + 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);
}
} //假如每10条数据放在一个 Sheet 里面,先计算需要多少个 Sheet
int ItenCountPerSheet = 10;
int SheetCount = Convert.ToInt32(Math.Ceiling((double)dt.Rows.Count / ItenCountPerSheet));
Response.ClearContent();
Response.BufferOutput = true;
Response.Charset = "utf-8";
Response.ContentType = "application/ms-excel";
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentEncoding = System.Text.Encoding.UTF8;
//Response.AppendHeader("Content-Disposition", "attachment;filename="+Server.UrlEncode("孟宪会Excel表格测试")+".xls");
// 采用下面的格式,将兼容 Excel 2003,Excel 2007, Excel 2010。 String FileName = "孟宪会Excel表格测试";
if (!String.IsNullOrEmpty(Request.UserAgent))
{
// firefox 里面文件名无需编码。
if (!(Request.UserAgent.IndexOf("Firefox") > -1 && Request.UserAgent.IndexOf("Gecko") > -1))
{
FileName = Server.UrlEncode(FileName);
}
}
Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xml");
Response.Write("<?xml version='1.0'?><?mso-application progid='Excel.Sheet'?>");
Response.Write(@"<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(@"<DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>");
Response.Write(@"<Author>孟宪会</Author><LastAuthor>孟子E章</LastAuthor>
<Created>2010-09-08T14:07:11Z</Created><Company>mxh</Company><Version>1990</Version>");
Response.Write("</DocumentProperties>");
Response.Write(@"<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>"); //SheetCount代表生成的 Sheet 数目。
for (int i = 0; i < SheetCount; i++)
{
//计算该 Sheet 中的数据起始行和结束行。
int start = ItenCountPerSheet * i;
int end = ItenCountPerSheet * (i + 1);
if (end > dt.Rows.Count) end = dt.Rows.Count; Response.Write("<Worksheet ss:Name='Sheet" + (i + 1) + "'>");
Response.Write("<Table x:FullColumns='1' x:FullRows='1'>"); //输出标题
Response.Write("\r\n<Row ss:AutoFitHeight='1'>");
for (int j = 0; j < dt.Columns.Count; j++)
{
Response.Write("<Cell ss:StyleID='Header'><Data ss:Type='String'>" + dt.Columns[j].ColumnName + "</Data></Cell>");
}
Response.Write("\r\n</Row>");
for (int j = start; j < end; j++)
{
Response.Write("<Row>");
for (int c = 0; c < 6; c++)
{
//对于数字,采用Number数字类型
if (c > 1)
{
Response.Write("<Cell ss:StyleID='border'><Data ss:Type='Number'>" + dt.Rows[j][c].ToString() + "</Data></Cell>");
}
else
{
Response.Write("<Cell ss:StyleID='border'><Data ss:Type='String'>" + dt.Rows[j][c].ToString() + "</Data></Cell>");
}
}
Response.Write("</Row>");
}
Response.Write("</Table>");
Response.Write("</Worksheet>");
Response.Flush();
}
Response.Write("</Workbook>");
Response.End();
}
</script>
<%@ Page Language="C#" %><script runat="server"> protected void Page_Load(object sender, EventArgs e)
{
// 下面采用的是DataTable,也可以采用DataSet,其中每个DataTable可以保存成一个 Sheet
// 迅雷下载时可以在下载完毕后会自动把文件名更新成 xls 或者 xml 的。
System.Data.DataTable dt = new System.Data.DataTable();
if (!Page.IsPostBack)
{
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] = "【孟子E章】" + 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);
}
} //假如每10条数据放在一个 Sheet 里面,先计算需要多少个 Sheet
int ItenCountPerSheet = 10;
int SheetCount = Convert.ToInt32(Math.Ceiling((double)dt.Rows.Count / ItenCountPerSheet));
Response.ClearContent();
Response.BufferOutput = true;
Response.Charset = "utf-8";
Response.ContentType = "application/ms-excel";
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentEncoding = System.Text.Encoding.UTF8;
//Response.AppendHeader("Content-Disposition", "attachment;filename="+Server.UrlEncode("孟宪会Excel表格测试")+".xls");
// 采用下面的格式,将兼容 Excel 2003,Excel 2007, Excel 2010。 String FileName = "孟宪会Excel表格测试";
if (!String.IsNullOrEmpty(Request.UserAgent))
{
// firefox 里面文件名无需编码。
if (!(Request.UserAgent.IndexOf("Firefox") > -1 && Request.UserAgent.IndexOf("Gecko") > -1))
{
FileName = Server.UrlEncode(FileName);
}
}
Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xml");
Response.Write("<?xml version='1.0'?><?mso-application progid='Excel.Sheet'?>");
Response.Write(@"<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(@"<DocumentProperties xmlns='urn:schemas-microsoft-com:office:office'>");
Response.Write(@"<Author>孟宪会</Author><LastAuthor>孟子E章</LastAuthor>
<Created>2010-09-08T14:07:11Z</Created><Company>mxh</Company><Version>1990</Version>");
Response.Write("</DocumentProperties>");
Response.Write(@"<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>"); //SheetCount代表生成的 Sheet 数目。
for (int i = 0; i < SheetCount; i++)
{
//计算该 Sheet 中的数据起始行和结束行。
int start = ItenCountPerSheet * i;
int end = ItenCountPerSheet * (i + 1);
if (end > dt.Rows.Count) end = dt.Rows.Count; Response.Write("<Worksheet ss:Name='Sheet" + (i + 1) + "'>");
Response.Write("<Table x:FullColumns='1' x:FullRows='1'>"); //输出标题
Response.Write("\r\n<Row ss:AutoFitHeight='1'>");
for (int j = 0; j < dt.Columns.Count; j++)
{
Response.Write("<Cell ss:StyleID='Header'><Data ss:Type='String'>" + dt.Columns[j].ColumnName + "</Data></Cell>");
}
Response.Write("\r\n</Row>");
for (int j = start; j < end; j++)
{
Response.Write("<Row>");
for (int c = 0; c < 6; c++)
{
//对于数字,采用Number数字类型
if (c > 1)
{
Response.Write("<Cell ss:StyleID='border'><Data ss:Type='Number'>" + dt.Rows[j][c].ToString() + "</Data></Cell>");
}
else
{
Response.Write("<Cell ss:StyleID='border'><Data ss:Type='String'>" + dt.Rows[j][c].ToString() + "</Data></Cell>");
}
}
Response.Write("</Row>");
}
Response.Write("</Table>");
Response.Write("</Worksheet>");
Response.Flush();
}
Response.Write("</Workbook>");
Response.End();
}
</script>
解决方案 »
- 如何在DataGrid中选定行读取其中的某个字段到文本框中?
- web.config:项“ConnectionString1”已添加?
- 一个网页被使用showModalDialog打开,当这个网页上的某一个Click事件的时候,如何去执行打开它的网页里的JS函数?
- select 下拉框改变之后,如何触发事件
- 各位做b/s的高手,请问谁知道怎么写datagrid的三层嵌套????????
- tabcontrol控件
- 求助:出现错误:试图运行项目时出错:无法在web服务器上启动调试,你没有调试该服务器的权限。验证你是服务器上“Debugger Users”组的
- SQL怎样数据库中的加法???!
- help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!在线等
- IIS怎么发布asp.net的网站啊(神啊救救我这菜鸟吧,非常急)...
- VS中的“选择标记”和“选择标记内容”功能有什么不同吗
- 找不到类型或命名空间名称“userinfo”(是否缺少 using 指令或程序集引用?)
http://www.developerfusion.com/tools/convert/csharp-to-vb/