if you are ok with csv or tab-delimited data file, seeHOW TO: Use ASP.NET to Query and Display Database Data in Excel by Using Visual C# .NET
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q311194&
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q311194&
解决方案 »
- 页面上点击QQ就弹出个QQ聊天框??
- 出现Operator '==' incompatible with operand types 'Int32' and 'Object'
- 请问 datalist 用RepeatColumns="5" 以后读出的信息为什么左右的
- 急!!从一个远程数据库(mssql)提取数据,数据量20W左右,再存储到另一个远程数据库中
- vb.net進行web開發,請問用vb.net怎樣編輯對某一文件進行下載
- 自己对电子优惠券的设计思路----大家PP
- 如何在asp.net中实现文件上传的功能,要求不用Html控件,请各位大虾指教
- 请问下面这种常见的HTML展示方式叫什么?有现成的插件吗?
- 请教一个存储过程
- 求教(急)
- 请问web.config文件里的authentication项是干什么的?
- 为什么用response.Redirect,网址中的中文字都变了UTF-8?
Using intelli-sense you can now explore the classes, interfaces, delegates and enums of the OWC ‘namespace’.
To manually create the wrapper we use the tlbimp.exe utility. We can find the OWC COM DLL here: \Program Files\Microsoft Office\Office\MSOWC.DLL.
We open up a command prompt and navigate to the directory where MSOWC.DLL resides. Then we issue the following command:
tlbimp msowc.dll /out:dotnetmsowc.dll /namespace:OWC The /namespace option defines which namespace the wrapper will be known by.
Now we need to move the dotnetmsowc.dll assembly, which contains our .NET wrapper class to the web application’s bin directory.
To find out which classes, interfaces etc. are available (assuming we’re not using VS.NET), we can use the Intermediary Language Disassembler (ildasm.exe). Open ildasm (Start, Run, ildasm) and open the dotnetmsowc.dll assembly. We can now explore all classes, their constructors, methods, attributes etc. The web form and codebehind file
Our web form will show the data in the DataGrid control. Additionally a textbox for the Excel file name to be exported and a button to perform the export will be displayed at the top of the page.
Have a look at the ASPX file:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="cominterop.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Exporting Data To Excel, using COM Interop and OWC</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
Export File Name:
<asp:TextBox ID="xlfile" Runat="server"></asp:TextBox>
<asp:Button ID="export2excel" Runat="server" Text="Export to Excel" /><br />
<br />
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
</form>
</body>
</HTML>
No rocket science there. Note that there is no OnClick or OnServerClick attribute for our ‘Export to Excel’ button. This is because VS.NET adds its own EventHandler for the button’s onclick event, as we can see when we have a look at the codebehind file.using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OWC;namespace cominterop
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private SqlCommand sql;
protected System.Web.UI.WebControls.Button export2excel;
protected System.Web.UI.WebControls.TextBox xlfile;
private SqlConnection cnn; private void Page_Load(object sender, System.EventArgs e)
{
this.BindDataGrid();
} private void BindDataGrid() {
cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;uid=sa;pwd=");
sql = new SqlCommand("select * from products",cnn);
cnn.Open();
SqlDataReader reader = sql.ExecuteReader();
this.DataGrid1.DataSource = reader;
this.DataGrid1.DataBind();
reader.Close();
cnn.Close();
} private void WriteDataGrid2Excel() {
SpreadsheetClass xlsheet = new SpreadsheetClass();
cnn.Open();
SqlDataReader reader = this.sql.ExecuteReader();
int numbercols = reader.FieldCount;
int row=1;
while (reader.Read()) {
for (int i=0;i<numbercols;i++)
{
xlsheet.ActiveSheet.Cells[row,i+1] = reader.GetValue(i).ToString();
}
row++;
}
reader.Close();
cnn.Close();
xlsheet.ActiveSheet.Export(Server.MapPath(".")+"\\"+this.xlfile.Text,OWC.SheetExportActionEnum.ssExportActionNone);
} private void export2excel_Click(object sender, System.EventArgs e)
{
if (this.xlfile.Text.Trim()!="")
{
this.WriteDataGrid2Excel();
}
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.export2excel.Click += new System.EventHandler(this.export2excel_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion }
}