您的位置:首页 > 编程语言 > C#

用C#生成Excel文件的方法和Excel.dll组件生成的方法

2009-02-18 15:15 615 查看
NET Framework1.0和1.1 都没有实现OleContainer组件。查遍MSDN,最后得了一个提示:可以使用IEBrowser来模拟OleContainer。这绝对是一个超重的实现,不过,最目前情况下,却是一个最省事的方法。本文就简单的说明一下,如何使用IEBrowser控件来嵌入Excel表格。

1. 首先,需要在工具栏中导入“Microsoft WEB 浏览器”。可以在工具栏上点右键,选择“添加/移除项”。然后在出现的自定义工具箱中选择“COM组件”,最后在组件中找到“Microsoft Web 浏览器”,勾选并确定。见下图:
2. 建立一个Form,在工具栏中选择“Microsoft Web浏览器”组件,放到Form中。

3. 使用以下的代码,以便导入一个Excel表格:
axWebBrowser1.Navigate(@"c:/test/test.xls");

4. 加入axWebBrowser1的DocumentComplete事件。并在事件中获取Excel的Ole对象:
public Excel.Workbook wb;

private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
wb = (Excel.Workbook)axWebBrowser1.Document;
}

5. 通过wb,就可以直接访问Excel表格了。

asp.net里导出excel表方法汇总‏
1、由dataset生成

public void CreateExcel(DataSet ds,string typeid,string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders= "", ls_item="";
int i=0;

//定义表对象与行对像,同时用DataSet对其值进行初始化
DataTable dt=ds.Tables[0];
DataRow[] myRow=dt.Select("");
// typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
if(typeid=="1")
{
//取得数据表各列标题,各标题之间以/t分割,最后一个列标题后加回车符
for(i=0;i colHeaders+=dt.Columns[i].Caption.ToString()+"/t";
colHeaders +=dt.Columns[i].Caption.ToString() +"/n";
//向HTTP输出流中写入取得的数据信息
resp.Write(colHeaders);
//逐行处理数据
foreach(DataRow row in myRow)
{
//在当前行中,逐列获得数据,数据之间以/t分割,结束时加回车符/n
for(i=0;i ls_item +=row[i].ToString() + "/t";
ls_item += row[i].ToString() +"/n";
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
resp.Write(ls_item);
ls_item="";
}
}
else
{
if(typeid=="2")
{
//从DataSet中直接导出XML数据并且写到HTTP输出流中
resp.Write(ds.GetXml());
}
}
//写缓冲区中的数据到HTTP头文件中
resp.End();

}

2、使用微软的C++写的ACTIVEX控件:http://download.microsoft.com/download/OfficeXPDev/sample/1.0/WIN98MeXP/EN-US/Dsoframerctl.exe
3、由datagrid生成:

public void ToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}

用法:ToExcel(datagrid1);

4、这个用dataview ,代码好长

public void OutputExcel(DataView dv,string str)
{
//
// TODO: 在此处添加构造函数逻辑
//
//dv为要输出到Excel的数据,str为标题名称
GC.Collect();
Application excel;// = new Application();
int rowIndex=4;
int colIndex=1;

_Workbook xBk;
_Worksheet xSt;

excel= new ApplicationClass();

xBk = excel.Workbooks.Add(true);

xSt = (_Worksheet)xBk.ActiveSheet;

//
//取得标题
//
foreach(DataColumn col in dv.Table.Columns)
{
colIndex++;
excel.Cells[4,colIndex] = col.ColumnName;
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
}

//
//取得表格中的数据
//
foreach(DataRowView row in dv)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dv.Table.Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else
if(col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
//
//加载一个合计行
//
int rowSum = rowIndex + 1;
int colSum = 2;
excel.Cells[rowSum,2] = "合计";
xSt.get_
Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
//
//设置选中的部分的颜色
//
xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
//
//取得整个报表的标题
//
excel.Cells[2,2] = str;
//
//设置整个报表的标题格式
//
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
//
//设置报表表格为最适应宽度
//
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
//
//设置整个报表的标题为跨列居中
//
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
//
//绘制边框
//
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
//
//显示效果
//
excel.Visible=true;

//xSt.Export(Server.MapPath(".")+"//"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
xBk.SaveCopyAs(Server.MapPath(".")+"//"+this.xlfile.Text+".xls");

ds = null;
xBk.Close(false, null,null);

excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect();
string path = Server.MapPath(this.xlfile.Text+".xls");

System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", file.Length.ToString());

// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";

// 把文件流发送到客户端
Response.WriteFile(file.FullName);
// 停止页面的执行

Response.End();
}
ASP.NET2.0中将GridView导出到Excel文件中
下面代码实现将 GridView 导出到 Excel文件中。
  值得注意的是VerifyRenderingInServerForm重载方法:
  MSDN上的 VerifyRenderingInServerForm 方法的描述:
  必须位于 <form runat=server> 标记中的控件可以在呈现之前调用此方法,以便在控件被置于标记外时显示错误信息。发送回或依赖于注册的脚本块的控件应该在 Control.Render 方法的重写中调用此方法。呈现服务器窗体元素的方式不同的页可以重写此方法以在不同的条件下引发异常。
  如果回发或使用客户端脚本的服务器控件没有包含在 HtmlForm 服务器控件 (<form runat="server">) 标记中,它们将无法正常工作。这些控件可以在呈现时调用该方法,以在它们没有包含在 HtmlForm 控件中时提供明确的错误信息。
  开发自定义服务器控件时,通常在为任何类型的输入标记重写 Render 方法时调用该方法。这在输入控件调用GetPostBackEventReference 或发出客户端脚本时尤其重要。复合服务器控件不需要作出此调用。
  没有这个方法,程序将报错。
  C# 代码
<%...@ Page Language="C#" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">...
ICollection CreateDataSource( )
...{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow dr;
dt.Columns.Add(new System.Data.DataColumn("id", typeof(Int32)));
dt.Columns.Add(new System.Data.DataColumn("PkID", typeof(string)));
dt.Columns.Add(new System.Data.DataColumn("Title", typeof(string)));
for (int i = 0; i < 6; i++)
...{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "123456789123456789123456789";
dr[2] = "<a href='http://dotnet.aspx.cc/'>欢迎光临【孟宪会之精彩世界】</a>";
dt.Rows.Add(dr);
}
System.Data.DataView dv = new System.Data.DataView(dt);
return dv;
}
protected void Page_Load( object sender, EventArgs e )
...{
if (!IsPostBack)
...{
GridView1.BorderWidth = Unit.Pixel(2);
GridView1.BorderColor = System.Drawing.Color.DarkOrange;
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
}
protected void Button1_Click( object sender, System.EventArgs e )
...{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
}
public override void VerifyRenderingInServerForm( Control control )
...{ }
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )
...{
if (e.Row.RowType == DataControlRowType.DataRow)
...{
e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@;");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>将 GridView 导出到 Excel 文件中</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="序号" DataField="id" />
<asp:BoundField HeaderText="身份证号" DataField="PkID" />
<asp:BoundField HeaderText="网址" DataField="Title" ReadOnly="true" HtmlEncode="false" />
</Columns>
</asp:GridView>
<asp:Literal ID="HiddenOut" runat="server" />
<asp:Button ID="Button1" runat="server" Text="导出" OnClick="Button1_Click" />
</form>
</body>
</html>

使用 Visual C# .NET 向 Excel 工作簿传输数据

/* Copyright all(c) 2005 ZhongFeng, http://blog.csdn.net/SW515 */
public class ValidateCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
this.CreateCheckCodeImage(GenerateCheckCode());
}
#region web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 asp.NET web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for(int i=0; i<5; i++)
{
number = random.Next();
if(number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if(checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for(int i=0; i<25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for(int i=0; i<100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
}

使用 Visual C# .NET 向 Microsoft Excel 2002 传输 XML 数据

从数据集生成在 Excel 2002 或 Excel 2003 中使用的 XML
本节说明如何创建 DataSet 对象,以及如何使用 WriteXML 方法将该对象包含的数据导出到 XML 文件中。生成的 XML 文件可以直接在 Excel 中打开。为便于说明,使用 Jet OLEDB 提供程序从 Microsoft Access Northwind 示例数据库创建了 DataSet 对象。但是,类似的代码可与您使用 Visual C# .NET 创建的任何 DataSet 对象一起使用。 1. 启动 Microsoft Visual Studio .NET。在文件菜单上,单击新建,然后单击项目。从 Visual C# 项目类型中选择 Windows 应用程序。默认情况下创建 Form1。
2. 在视图菜单上,选择工具箱以显示“工具箱”,然后向 Form1 中添加一个按钮。
3. 双击 Button1。将出现该窗体的代码窗口。
4. 将下面的 using 指令添加到 Form1.cs 顶部:
using System.Data.OleDb;
using System.Xml;

5. 将下面的私有成员变量添加到 Form1 类中:
private string strConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ " C://Program Files//Microsoft Office//Office10//Samples//"
+ "Northwind.mdb;";

注意:您可能需要修改连接字符串中 Northwind.mdb 的路径,以便与您安装的位置相匹配。

6. 在 button1_Click 处理程序中添加以下代码:
//Connect to the data source.
OleDbConnection objConn = new OleDbConnection (strConn);
try
{
objConn.Open();

//Fill a dataset with records from the Customers table.
OleDbCommand objCmd = new OleDbCommand(
"Select CustomerID, CompanyName, ContactName, "
+ "Country, Phone from Customers", objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmd;
DataSet objDataset = new DataSet();
objAdapter.Fill(objDataset);

//Create the FileStream to write with.
System.IO.FileStream fs = new System.IO.FileStream(
"C://Customers.xml", System.IO.FileMode.Create);

//Create an XmlTextWriter for the FileStream.
System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(
fs, System.Text.Encoding.Unicode);

//Add processing instructions to the beginning of the XML file, one
//of which indicates a style sheet.
xtw.WriteProcessingInstruction("xml", "version='1.0'");
//xtw.WriteProcessingInstruction("xml-stylesheet",
// "type='text/xsl' href='customers.xsl'");

//Write the XML from the dataset to the file.
objDataset.WriteXml(xtw);
xtw.Close();

//Close the database connection.
objConn.Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}

7. 按 F5 键生成并运行程序。
8. 单击 Button1 以创建 XML 文件,然后关闭 Form1 以结束该程序。
9. 启动 Excel 2002 或 Excel 2003 并打开 C:/Customers.xml 输出文件。
10. 在您看到已将 XML 分析成新工作簿中的行和列后,请关闭文件并退出 Excel。
返回页首
使用样式表格式化 XML
该步骤介绍如何使用样式表 (XSL) 来转换 XML 数据在 Excel 工作簿中的格式和排列方式。 1. 使用任何 HTML 编辑器或文本编辑器(例如 Notepad.exe),将下面的 XSL 另存为 C:/Customers.xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<HTML>
<HEAD>
<STYLE>
.HDR { background-color:bisque;font-weight:bold }
</STYLE>
</HEAD>
<BODY>
<TABLE>
<COLGROUP WIDTH="100" ALIGN="CENTER"></COLGROUP>
<COLGROUP WIDTH="200" ALIGN="LEFT"></COLGROUP>
<COLGROUP WIDTH="200" ALIGN="LEFT"></COLGROUP>
<COLGROUP WIDTH="100" ALIGN="LEFT"></COLGROUP>
<COLGROUP WIDTH="100" ALIGN="LEFT"></COLGROUP>
<TD CLASS="HDR">Customer ID</TD>
<TD CLASS="HDR">Company</TD>
<TD CLASS="HDR">Contact</TD>
<TD CLASS="HDR">Country</TD>
<TD CLASS="HDR">Phone</TD>
<xsl:for-each select="NewDataSet/Table">
<TR>
<TD><xsl:value-of select="CustomerID"/></TD>
<TD><xsl:value-of select="CompanyName"/></TD>
<TD><xsl:value-of select="ContactName"/></TD>
<TD><xsl:value-of select="Country"/></TD>
<TD><xsl:value-of select="Phone"/></TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

2. 在 button1_Click 处理程序中取消对以下代码行的注释:
xtw.WriteProcessingInstruction("xml-stylesheet",
"type='text/xsl' href='customers.xsl'");

此行代码向 XML 文件写入了一个处理指令,Excel 使用该指令来查找样式表 (Customers.xsl)。

3. 按 F5 键生成并运行程序。
4. 单击 Button1 以创建 XML 文件,然后关闭 Form1 以结束该程序。
5. 启动 Excel 2002 或 Excel 2003 并打开 C:/Customers.xml 输出文件。
6. 因为从 Excel 可以看到 XML 中样式表的处理指令,所以您在打开文件时会收到一个对话框提示。在导入 XML 对话框中,选择打开该文件,应用以下样式表。在列表中,选择 Customers.xsl 并单击 确定。注意,XML 数据已格式化,并已经根据样式表排列各个列。
7. 关闭该文件并退出 Excel。
返回页首
使用代码打开转换的 XML
此时,您已经使用 Excel 中的用户界面打开了 XML 文件。本节介绍如何以编程方式使 Excel 自动打开工作簿。下面的示例说明如何通过首先将 DataSet 对象中的 XML 转换成 HTML 来打开转换的 XML,而不需要用户干预。

用C#生成Excel文件的方法和Excel.dll组件生成的方法

一个示例:
class AppTest
{
private Excel.ApplicationClass _x;
public static void Main0()
{
AppTest a = new AppTest();
a._x = new Excel.ApplicationClass();
a._x.UserControl = false;
for (int i = 0 ;i < 4; i++)
{

a.SaveToXls("D://test//" + i + ".xls"); // 本例是在D盘下建立的test文件夹
}
a._x.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject((object) a._x);
System.GC.Collect();
}
private void SaveToXls(string filename)
{
Excel.WorkbookClass wb = (Excel.WorkbookClass) this._x.Workbooks.Add(System.Reflection.Missing.Value);
for(int i = 1;i <= 4; i++)
{
this._x.Cells[i,1]=i.ToString();
this._x.Cells[i,2]="’bbb2";
this._x.Cells[i,3]="’ccc3";
this._x.Cells[i,4]="’aaa4";
}

wb.Saved = true;
this._x.ActiveWorkbook.SaveCopyAs(filename);
}
}
【注:在VS.Net中运行是要添加Excel.dll组件的,Excel组件VS.Net本身是没有的,下面是生成Excel.dll的方法。】
1.要保证机器本身要安装OFFICE.
2.把[C:/Program Files/Microsoft Office/Office:默认安装路径]下的EXCEL9.OLB文件拷贝到[C:/Visual Studio.Net/SDK/v1.1/Bin:VS.Net安装路径]路径下。
3.打开Visual Studio .Net2003命令提示,运行TlbImp Excel9.olb Excel.dll ,就会在[C:/Visual Studio.Net/SDK/v1.1/Bin]下生成Excel.dll组件。
4.在项目中添加Excel.dll引用就OK了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: