以下为生成excel并设置数据
你可以将数据库里的数据取出,放在数组里,然后再参照以下赋值
Excel2.Application oExcel;
Excel2.Workbook oBook;
Object oMissing = System.Reflection.Missing.Value; // Create an instance of Excel.
oExcel = new Excel2.Application(); // Add a workbook.
oBook = oExcel.Workbooks.Add(oMissing);
for (int i=1;i<=4;i++)
{
oExcel.Cells[i,1]=i.ToString();
oExcel.Cells[i,2]="'bbb2";
oExcel.Cells[i,3]="'ccc3";
oExcel.Cells[i,4]="'aaa4";
}
oExcel.Visible =true; }最后你必须做的是设置Dcomcnfg中的Microsoft Excel应用程序权限,否则Excel不能被正常调用。
你可以将数据库里的数据取出,放在数组里,然后再参照以下赋值
Excel2.Application oExcel;
Excel2.Workbook oBook;
Object oMissing = System.Reflection.Missing.Value; // Create an instance of Excel.
oExcel = new Excel2.Application(); // Add a workbook.
oBook = oExcel.Workbooks.Add(oMissing);
for (int i=1;i<=4;i++)
{
oExcel.Cells[i,1]=i.ToString();
oExcel.Cells[i,2]="'bbb2";
oExcel.Cells[i,3]="'ccc3";
oExcel.Cells[i,4]="'aaa4";
}
oExcel.Visible =true; }最后你必须做的是设置Dcomcnfg中的Microsoft Excel应用程序权限,否则Excel不能被正常调用。
解决方案 »
- 如何给这个存储过程(带参数的)传递参数
- 有谁知道asp.net开发的网页能通过机顶盒在电视上浏览吗?
- |M| 如何让<a href="dd.htm">下载</a> 点击的时候不是打开而是下载下来
- 想用 asp.net2.0 做一个学校用的成绩输入系统,但新手上路,想参考一下相关的源代码,主要是在页面能实现连续输入分数
- 怎么能重复利用DataGrid?
- 请大家看看怎么没有反映 自定义的事件!
- ASP.NET 应用程序中的服务器错误!
- .net QR二维码生成越界问题
- 求助:asp.net开发问题
- 如果更改DATAGRID的某个列的宽度?
- ASP。NET下如何上传图片到数据库,并且读出图片(高分相送)
- 页面有150个文本框,用submit提交通不过,各位大虾进来看看!!
<%@ 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 }
} //查看以下这篇文章!
http://www.wimdows.net/articles/article.aspx?aid=15
会帮助你解决这个问题!