一个dataSet有两个表,如何把两个表导入excel的两个sheet中?

xie168518 2012-09-20 04:48:01
asp.net

要求点击导入时 输入excel文件名,excel的两个sheet名称就是datatable的表名,

找了很多资料都搞不定
...全文
227 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
五百年前,咱们是一家啊,哈哈....顶一个....
xie168518 2012-09-21
  • 打赏
  • 举报
回复
太感谢了
xie168518 2012-09-21
  • 打赏
  • 举报
回复
太狠了,就这样,相信很多朋友都会碰到这个问题,

只是有一点,如果表的字段类型为int的话,就要把下面的这个去掉,不然excel打不开

//对于数字,采用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>");
//}
孟子E章 2012-09-21
  • 打赏
  • 举报
回复
修正完整的版本
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
System.Data.DataRow dr;
dataTable1.Columns.Add(new System.Data.DataColumn("UserId", typeof(System.Int32)));
dataTable1.Columns.Add(new System.Data.DataColumn("UserName", typeof(System.String)));
dataTable1.PrimaryKey = new System.Data.DataColumn[] { dataTable1.Columns["UserId"] };
for (int i = 0; i < 8; i++)
{
dr = dataTable1.NewRow();
dr[0] = i;
dr[1] = "用户姓名【孟子E章】测试" + i.ToString();
dataTable1.Rows.Add(dr);
}
System.Data.DataTable dataTable2 = new System.Data.DataTable("BlogArticle");
dataTable2.Columns.Add(new System.Data.DataColumn("ArticleId", typeof(System.Int32)));
dataTable2.Columns.Add(new System.Data.DataColumn("Title", typeof(System.String)));
dataTable2.Columns.Add(new System.Data.DataColumn("UserId", typeof(System.Int32)));
dataTable2.PrimaryKey = new System.Data.DataColumn[] { dataTable1.Columns["ArticleId"] };
Random rd = new Random();
for (int i = 0; i < 20; i++)
{
dr = dataTable2.NewRow();
dr[0] = i;
dr[1] = "文章标题例子" + i.ToString();
dr[2] = rd.Next(0, 7);
dataTable2.Rows.Add(dr);
}
ds.Tables.Add(dataTable1);
ds.Tables.Add(dataTable2);

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;
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 + ".xls");
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 < ds.Tables.Count; i++)
{
System.Data.DataTable dt = ds.Tables[i];
Response.Write("<Worksheet ss:Name='Sheet" + (i + 1) + "_" + ds.Tables[i].TableName + "'>");
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 = 0; j < dt.Rows.Count; j++)
{
Response.Write("<Row>");
for (int c = 0; c < dt.Columns.Count; 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.Write("</Workbook>");
Response.End();
}
孟子E章 2012-09-21
  • 打赏
  • 举报
回复
DataSet这样
  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 + ".xls");
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 < DataSet1.Tables.Count; i++)
{
DataTable dt = DataSet1.Tables[i];
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 = 0; j < dt.Rows.Count; j++)
{
Response.Write("<Row>");
for (int c = 0; c < dt.Columns.Count; 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();
wangyizhi58 2012-09-21
  • 打赏
  • 举报
回复
使用NPOI比较简单些,先创建文档,再创建sheet,再创建行。再创建列。从网上下载个。。。挺好用的也很容易理解
xie168518 2012-09-21
  • 打赏
  • 举报
回复
代码能看懂,不过改起来很是非常吃力的
孟子E章 2012-09-21
  • 打赏
  • 举报
回复
你看没看代码啊?
里面的思路你了解吗?

你循环
for(int i=0;i<DataSet.Tables.Count;i++)
不就可以了吗

循环DataSet,更简单
xie168518 2012-09-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
导出到 Excel 中多个工作表(Sheet) 的方法

如果是DataTable,DataSet,可以直接导出成文件。下面是完整的源代码:
C# 代码

C# code

<%@ Page Language="C#" %>

<script runat="server">

protected void Page_Load(object sender, EventArg……
[/Quote]

孟大侠,有没有可以采用DataSet,其中每个DataTable可以保存成一个 Sheet

H_Gragon 2012-09-21
  • 打赏
  • 举报
回复
我有Gridview到处成excel表的实例:地址
xie168518 2012-09-21
  • 打赏
  • 举报
回复
DataSet,可以直接导出成文件。下面是完整的源代码:

有没有现成的,是不是让我 对照这上面改?
孟子E章 2012-09-21
  • 打赏
  • 举报
回复
导出到 Excel 中多个工作表(Sheet) 的方法

如果是DataTable,DataSet,可以直接导出成文件。下面是完整的源代码:
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 + ".xls");
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>

xie168518 2012-09-21
  • 打赏
  • 举报
回复
这么大的论坛,怎么会没有人碰到过呢? 求高手现身
xie168518 2012-09-20
  • 打赏
  • 举报
回复
难道大家都没有碰到过这样的需求?
.从头再来 2012-09-20
  • 打赏
  • 举报
回复
没试过这样做
xie168518 2012-09-20
  • 打赏
  • 举报
回复
没有人?

62,164

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧