asp.net 实现从“SqlServer”导入/导出“Excel”

starryprof 2012-04-13 11:16:33
“asp.net”实现从“SqlServer”导入/导出“Excel”。

问题一:怎样将“DataTable”结果集导出成为“Excel”(2003即可)?

问题二:“Excel”如何导入到“数据库表”中???这里要注意可以导入“2003、2007、2010”(确保都可以导入)。


最好有实例可以参考,谢谢各位大侠们了,感激不尽。
...全文
321 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
starrycheng 2012-05-13
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]
http://blog.csdn.net/fengyarongaa/article/details/7525885

你可以适当的 改成 sql的方式
[/Quote]

npoi可以的,SqlServer可以直接做到么???
ycproc 2012-05-01
  • 打赏
  • 举报
回复

http://blog.csdn.net/fengyarongaa/article/details/7525885

你可以适当的 改成 sql的方式
evelyn_green 2012-05-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
NPOI
http://npoi.codeplex.com/
EPPLUS
http://epplus.codeplex.com/
[/Quote]
估计数据库管理工具有带如此功能吧
starrycheng 2012-04-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

先读取出来,然后在excel中新建张表,用dataadapter批量添加到excel不行么?
[/Quote]

关键是“Excel”导入数据库。
  • 打赏
  • 举报
回复
先读取出来,然后在excel中新建张表,用dataadapter批量添加到excel不行么?
starrycheng 2012-04-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

NPOI
http://npoi.codeplex.com/
EPPLUS
http://epplus.codeplex.com/
[/Quote]

有实例可以参考么?
chen_ya_ping 2012-04-15
  • 打赏
  • 举报
回复
NPOI
http://npoi.codeplex.com/
EPPLUS
http://epplus.codeplex.com/
starryprof 2012-04-15
  • 打赏
  • 举报
回复
请大师解答。。。
TikYang 2012-04-13
  • 打赏
  • 举报
回复
1、Excel”如何导入到“数据库表”中,如果你会LINQ的话,直接用LINQ to Excel 吧(http://www.codeplex.com/site/search?query=linq%20excel)
2、导出要简单些,直接用 Microsoft.Office.Interop.Excel,当然导入也可用它,只是没LINQ to Excel 好用而已,网上还有种直接用HttpResponse写入的更简单,不需要其它类型库支持

如果要用就得装(非完全安装NET4时):Microsoft Access Database Engine 2010(http://www.microsoft.com/downloads/zh-cn/details.aspx?familyid=c06b8369-60dd-4b64-a44b-84b371ede16d),安装正确的版本就行,如果你会动态数据类型(Dynamic),用这个就更简单些。
starrycheng 2012-04-13
  • 打赏
  • 举报
回复
2010和2007是一样的吧。
starrycheng 2012-04-13
  • 打赏
  • 举报
回复
public void ExpertControl(System.Web.UI.Control source, DocumentType type,string FileName)
{
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter);
System.Web.HttpResponse Response = new System.Web.HttpResponse(htmlWriter);
//设置Http的头信息,编码格式
if (type == DocumentType.Excel)
{
//Excel
Response.AppendHeader("Content-Disposition", "attachment;filename=Report" + FileName + ".xls");
Response.ContentType = "application/ms-excel";
}
else if (type == DocumentType.Word)
{
//Word
Response.AppendHeader("Content-Disposition", "attachment;filename=Report" + FileName + ".doc");
Response.ContentType = "application/ms-word";
}
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

//关闭控件的视图状态
source.Page.EnableViewState = false;



//输出
Response.Write(writer.ToString());
Response.End();
}

//文档类型
public enum DocumentType
{
Word,
Excel
}

能追加几个问题么?
问题一:导出“DataTable”必须先将“DataTable”作为数据源放入“GridView”中吗?然后再导出“GridView”,请问还有其它办法么?
问题二:“System.Web.HttpResponse”和“System.IO.StringWriter”之间是什么关系,为什么实例化“System.Web.HttpResponse”前,要先用“System.IO.StringWriter”实例化?
问题三:“asp.net页面”默认有“Response”对象么,都不用单独实例化“System.Web.HttpResponse”对象的?
starrycheng 2012-04-13
  • 打赏
  • 举报
回复
这里是控件导出“Word”或者是“Excel”的方法,如果是“DataTable”应该怎么办呢?
starrycheng 2012-04-13
  • 打赏
  • 举报
回复
这里是控件导出“Word”或者是“Excel”的方法,如果是“Excel”应该怎么办呢?


/// <summary>
/// 将Web控件导出
/// </summary>
/// <param name="source">控件实例</param>
/// <param name="type">类型:Excel或Word</param>
public void ExpertControl(System.Web.UI.Control source, DocumentType type)
{
//设置Http的头信息,编码格式
if (type == DocumentType.Excel)
{
//Excel
Response.AppendHeader("Content-Disposition", "attachment;filename=Report" + E_ID + ".xls");
Response.ContentType = "application/ms-excel";
}
else if (type == DocumentType.Word)
{
//Word
Response.AppendHeader("Content-Disposition", "attachment;filename=Report" + E_ID + ".doc");
Response.ContentType = "application/ms-word";
}
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

//关闭控件的视图状态
source.Page.EnableViewState = false;

//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter);

//输出
Response.Write(writer.ToString());
Response.End();
}

//文档类型
public enum DocumentType
{
Word,
Excel
}

62,046

社区成员

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

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

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

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