asp.net 实现从“SqlServer”导入/导出“Excel”
“asp.net”实现从“SqlServer”导入/导出“Excel”。问题一:怎样将“DataTable”结果集导出成为“Excel”(2003即可)?
问题二:“Excel”如何导入到“数据库表”中???这里要注意可以导入“2003、2007、2010”(确保都可以导入)。
最好有实例可以参考,谢谢各位大侠们了,感激不尽。 --------------------编程问答-------------------- 2010和2007是一样的吧。 --------------------编程问答-------------------- 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),用这个就更简单些。 --------------------编程问答-------------------- 这里是控件导出“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
}
--------------------编程问答-------------------- 这里是控件导出“Word”或者是“Excel”的方法,如果是“DataTable”应该怎么办呢? --------------------编程问答-------------------- 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”对象的? --------------------编程问答-------------------- 请易做图解答。。。 --------------------编程问答-------------------- NPOI
http://npoi.codeplex.com/
EPPLUS
http://epplus.codeplex.com/
--------------------编程问答--------------------
有实例可以参考么? --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 先读取出来,然后在excel中新建张表,用dataadapter批量添加到excel不行么? --------------------编程问答--------------------
关键是“Excel”导入数据库。 --------------------编程问答-------------------- --------------------编程问答--------------------
估计数据库管理工具有带如此功能吧
--------------------编程问答--------------------
http://blog.csdn.net/fengyarongaa/article/details/7525885
你可以适当的 改成 sql的方式 --------------------编程问答-------------------- --------------------编程问答--------------------
npoi可以的,SqlServer可以直接做到么???
补充:.NET技术 , ASP.NET