我想请教高手一个问题,我创建excel文件时碰到一个问题解决不了,不知道是不是引用的问题,还是程序的问题.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Data.SqlClient;
public partial class Excel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreatExcelTable();
}
}
private DataSet Getdata()
{
//这里是连接数据库中的表,就不写了;
}
private void CreatExcelTable()
{
DataSet ds = Getdata();
// excel= CreateOleObject("Excel.Application");
Excel.Application excel= new Excel.Application();
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
System.Data.DataTable table = ds.Tables[0];
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
excel.Visible = false;
excel.DisplayAlerts = false;
excel.Save(MapPath(ExcelDB/ExcelTable.xls));
excel.Application.Workbooks.Close();
excel.Application.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();
}
}
可是每次运行都会有错误,说EXCEL.Application是属性,而这里当作类型来使用编译不通过,希望高手帮忙啊,告诉我问题在哪
------解决方案--------------------------------------------------------
添加引用,选择COM-->选择Microsoft Word Object Library和Microsoft Excel Object Library组件)
using Excel;
Excel.Application excel = new Excel.ApplicationClass();
------解决方案--------------------------------------------------------
LZ就没引用上EXCEL的组件。。
添加引用:Microsoft.Office.Interop.Excel
在页面里 using Microsoft.Office.Interop.Excel;
------解决方案--------------------------------------------------------
引用 Microsoft.Office.Interop.Excel
------解决方案--------------------------------------------------------
添加引用,选择COM-->选择Microsoft Word Object Library和Microsoft Excel Object Library组件)
using Excel;
Excel.Application excel = new Excel.ApplicationClass();
------解决方案--------------------------------------------------------
添加引用,选择COM-->选择Microsoft Word Object Library和Microsoft Excel Object Library组件)
using Excel;
Excel.Application excel = new Excel.ApplicationClass();