在线等,如何将C#中的DataTable 导入到Excel中.顶者有分. 我在网上找了很多资料,我在C#中添加了Excel引用,可在这个引用中Excel.Application是一个接口不能生成对象,在我查的资料中Excel.Application都是一个类,请求朋友们帮忙解决.希望有完整的代码以及引用的命名空间的说明.谢了. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 DataTable导入Excel的方法有很多种,调用Excel类库只是其中一种~不同版本的Excel类库稍有不同,如果Excel.Application是接口,那么试试Excel.ApplicationClass http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asphttp://aspalliance.com/518 导出方法真的很多,你可以把网页改成excel格式你也可以把datatable绑定到datagrid然后导出也可以用脚本创建excel.application对象也可以用剪贴板 <%@ Page language="c#" Src="lw_cgd_excel.cs" AutoEventWireup="false" Inherits="DataGrid_import_WordExcel.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>OutPutExcel</title> </HEAD> <link rel="stylesheet" href="Style.css" type="text/css"> <body> <form width="120%" id="Form1" method="post" runat="server"> <table width="120%"><tr><td> <asp:datagrid id="DataGrid1" bgcolor="#efefef" HeaderStyle-BackColor="#718BD6" HeaderStyle-ForeColor="#FFFF66" AlternatingItemStyle-BackColor="#FFFFFF" itemstyle-backcolor="#FFFFFF" runat="server"> <ItemStyle HorizontalAlign="center" Height="20"></ItemStyle> <Columns> </Columns> </asp:datagrid> </td></tr></table> <P> <asp:button id="Btn_Import_Excel" runat="server" Text="轉到EXCEL"></asp:button></P> </form> </body> </HTML>-------------------------using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; using System.Data.SqlClient ; using System.Text; using System.Configuration;namespace DataGrid_import_WordExcel { public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button BtnImportWord; protected System.Web.UI.WebControls.Button Btn_Import_Excel; protected System.Web.UI.WebControls.DataGrid DataGrid1;public DataRow dr;private DataSet myDS =new DataSet();private void Page_Load(object sender, System.EventArgs e) { //CreateDataSet(); Data_Load();if(!IsPostBack) {//DataBind(); }} #region Web 敦极扢數?汜傖腔測鎢 override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.Btn_Import_Excel.Click += new System.EventHandler(this.Btn_Import_Excel_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void ExportDataGrid(string FileType, string FileName) //植DataGrid絳堤 { Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState =false; StringWriter tw = new StringWriter(); HtmlTextWriter hw =new HtmlTextWriter(tw); DataGrid1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } private void Btn_Import_Excel_Click(object sender, System.EventArgs e) { ExportDataGrid("application/ms-excel", "Excel.xls"); //絳善Excel } private void Data_Load() { SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["datadata2"]); SqlCommand cmd=new SqlCommand("lw_cgd_excel",myConnection); cmd.CommandType=CommandType.StoredProcedure; myConnection.Open(); DataSet ds=new DataSet(); SqlDataAdapter da=new SqlDataAdapter(); da.SelectCommand=cmd; da.Fill(ds); DataGrid1.DataSource=ds; DataGrid1.DataBind(); } DataView CreateDataSource() { string nowDSN=ConfigurationSettings.AppSettings["datadata2"]; SqlConnection myConnection=new SqlConnection(nowDSN); SqlCommand cmd=new SqlCommand("lw_cgd_excel",myConnection); cmd.CommandType=CommandType.StoredProcedure; DataSet ds=new DataSet(); SqlDataAdapter da=new SqlDataAdapter(); da.SelectCommand=cmd; da.Fill(ds); DataGrid1.DataSource=ds; DataGrid1.DataBind(); return ds.Tables["pur"].DefaultView; myConnection.Close(); Page.DataBind(); } void DataBind() { DataView source=CreateDataSource(); if(!IsPostBack) { } DataGrid1.DataSource = source; DataGrid1.DataBind(); } } } write a xml as Excel will be better. 请参考:生成/读取(反向更新数据库) Excel文件(示例代码下载):http://blog.csdn.net/ChengKing/archive/2005/11/29/539514.aspx 有没有C#的小例子发给我,谢了E-Mail: [email protected] 可用vbscript 但有個限制就是客戶端瀏覽器設置的 option->security->custom level->initialize and script activex controls not ed as safe 設成非 disabled.<script language="vbscript"> Sub exportbutton_onclick Dim sHTML, oExcel, oBook ,sHTML2,sHTML3,sHTML4 sHTML = document.all.item("DataGrid1").outerhtml sHTML2 = document.all.item("DataGrid2").outerhtml sHTML3 = document.all.item("DataGrid3").outerhtml Set oExcel = CreateObject("Excel.Application") Set oBook = oExcel.Workbooks.Add oBook.HTMLProject.HTMLProjectItems("Sheet1").Text = sHTML oBook.HTMLProject.HTMLProjectItems("Sheet2").Text = sHTML2 oBook.HTMLProject.HTMLProjectItems("Sheet3").Text = sHTML3 oBook.HTMLProject.RefreshDocument oExcel.Visible = true oExcel.UserControl = true End Sub </script>記得在codebehind 中綁定數據到 datagrid. http://www.codeproject.com/aspnet/ExportClassLibrary.asphttp://blog.csdn.net/AloneSword/archive/2006/02/25/609375.aspxhttp://www.codeproject.com/dotnet/DataGridCopyHelper.asp 下拉框联动选项卡,怎么做 求导出Word文档【分不是问题】 session 保存对象 网站自己进行URLencode解码吗? 里面是个怎么样的机制呢? 关于aspx中调用js脚本的顺序,请指点 ●●●百思不得其解●●● QQ群〔 15757119 ---.Net〕力求为大家创建一个.Net 方便快捷的空间,感觉.Net的浩瀚,欢迎加入 解决有分送哈! 大家分析下这两种方法的写法有何不同? 求基于.net的网络考试平台的源码和论文 ASPNET 帐户的访问 SQL server权限 question?? 谁有asp.net2.0的书目介绍点?我想买几本书学学。
http://aspalliance.com/518
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<link rel="stylesheet" href="Style.css" type="text/css">
<body>
<form width="120%" id="Form1" method="post" runat="server">
<table width="120%"><tr><td>
<asp:datagrid
id="DataGrid1"
bgcolor="#efefef"
HeaderStyle-BackColor="#718BD6"
HeaderStyle-ForeColor="#FFFF66"
AlternatingItemStyle-BackColor="#FFFFFF"
itemstyle-backcolor="#FFFFFF"
runat="server">
<ItemStyle HorizontalAlign="center" Height="20"></ItemStyle>
<Columns> </Columns>
</asp:datagrid>
</td></tr></table>
<P>
<asp:button id="Btn_Import_Excel" runat="server" Text="轉到EXCEL"></asp:button></P>
</form>
</body>
</HTML>
-------------------------using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient ;
using System.Text;
using System.Configuration;namespace DataGrid_import_WordExcel
{ public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button BtnImportWord;
protected System.Web.UI.WebControls.Button Btn_Import_Excel;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public DataRow dr;
private DataSet myDS =new DataSet();private void Page_Load(object sender, System.EventArgs e)
{
//CreateDataSet();
Data_Load();
if(!IsPostBack)
{
//DataBind();
}
} #region Web 敦极扢數?汜傖腔測鎢
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
} private void InitializeComponent()
{
this.Btn_Import_Excel.Click += new System.EventHandler(this.Btn_Import_Excel_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion private void ExportDataGrid(string FileType, string FileName) //植DataGrid絳堤
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState =false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw =new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
private void Btn_Import_Excel_Click(object sender, System.EventArgs e)
{
ExportDataGrid("application/ms-excel", "Excel.xls"); //絳善Excel
} private void Data_Load()
{ SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["datadata2"]);
SqlCommand cmd=new SqlCommand("lw_cgd_excel",myConnection);
cmd.CommandType=CommandType.StoredProcedure; myConnection.Open(); DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
} DataView CreateDataSource()
{
string nowDSN=ConfigurationSettings.AppSettings["datadata2"];
SqlConnection myConnection=new SqlConnection(nowDSN); SqlCommand cmd=new SqlCommand("lw_cgd_excel",myConnection);
cmd.CommandType=CommandType.StoredProcedure; DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
return ds.Tables["pur"].DefaultView;
myConnection.Close();
Page.DataBind();
}
void DataBind()
{
DataView source=CreateDataSource();
if(!IsPostBack)
{
}
DataGrid1.DataSource = source;
DataGrid1.DataBind();
} }
}
生成/读取(反向更新数据库) Excel文件(示例代码下载):
http://blog.csdn.net/ChengKing/archive/2005/11/29/539514.aspx
initialize and script activex controls not ed as safe 設成非 disabled.<script language="vbscript">
Sub exportbutton_onclick
Dim sHTML, oExcel, oBook ,sHTML2,sHTML3,sHTML4
sHTML = document.all.item("DataGrid1").outerhtml
sHTML2 = document.all.item("DataGrid2").outerhtml
sHTML3 = document.all.item("DataGrid3").outerhtml
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add
oBook.HTMLProject.HTMLProjectItems("Sheet1").Text = sHTML
oBook.HTMLProject.HTMLProjectItems("Sheet2").Text = sHTML2
oBook.HTMLProject.HTMLProjectItems("Sheet3").Text = sHTML3
oBook.HTMLProject.RefreshDocument
oExcel.Visible = true
oExcel.UserControl = true
End Sub
</script>
記得在codebehind 中綁定數據到 datagrid.
http://blog.csdn.net/AloneSword/archive/2006/02/25/609375.aspx
http://www.codeproject.com/dotnet/DataGridCopyHelper.asp