导出到Excel C#错误
我没有得到导出到excel代码中出现的错误。我有这个错误 无法将“System.\u ComObject”类型的COM对象强制转换为接口 键入“Microsoft.Office.Interop.Excel.Range”。此操作失败 因为接口的COM组件上的QueryInterface调用 由于以下原因,IID“{00020846-0000-0000-C000-0000000000 46}”失败 以下错误:不支持此类接口(HRESULT的异常: 0x80004002(E_NOINTERFACE)) 这是我的gridview中的代码,我的gridview的名称是dgvCommission导出到Excel C#错误,c#,excel,gridview,C#,Excel,Gridview,我没有得到导出到excel代码中出现的错误。我有这个错误 无法将“System.\u ComObject”类型的COM对象强制转换为接口 键入“Microsoft.Office.Interop.Excel.Range”。此操作失败 因为接口的COM组件上的QueryInterface调用 由于以下原因,IID“{00020846-0000-0000-C000-0000000000 46}”失败 以下错误:不支持此类接口(HRESULT的异常: 0x80004002(E_NOINTERFACE))
void worker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_elapsed);
timer.Start();
Invoke(new MyDelegate(ShowLabel), "Loading...");
BACS.DateFrom = this.dtFrom.Value.ToShortDateString(); //this.dtFrom.Text;
BACS.DateTo = this.dtTo.Value.ToShortDateString(); //this.dtTo.Text;
BACS.BrokerAgent = BrokerXML;
BACS.Payor = PayorXML;
DS = BACS.GET_SP_BACS_ComputeCommission();
ReadData(DS, this.dgvCommision);
CheckGrid();
Thread.Sleep(1);
timer.Stop();
}
catch
{ }
}
这是我在表格中导出到excel的代码
private void ExportToExcel()
{
string[] arrays = new string[19];
arrays[0] = "Type";
arrays[1] = "Broker Agent";
arrays[2] = "Payor";
arrays[3] = "Billing #";
arrays[4] = "OR No.";
arrays[5] = "OR Date";
arrays[6] = "Particulars";
arrays[7] = "Mode";
arrays[8] = "Amount Billed";
arrays[9] = "Amount Paid";
arrays[10] = "Non Comm Items";
arrays[11] = "Net";
arrays[12] = "Output VAT";
arrays[13] = "Commissionable Amount";
arrays[14] = "WTAX Rate";
arrays[15] = "WTAX";
arrays[16] = "Net Commission";
arrays[17] = "InputVAT";
arrays[18] = "For Fund Release";
SystemUtil.ExportToExel(DS, arrays);
}
ExportToExcel类代码如下:
public void ExportToExel(DataSet ds,string[] arrays)
{
Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;
//Excel.Range oRange;
oXL = new Excel.Application();
// Set some properties
oXL.Visible = true;
oXL.DisplayAlerts = false;
// Get a new workbook.
oWB = oXL.Workbooks.Add(Missing.Value);
// Get the active sheet
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name = "EXCEL";
DataTable dt2 = new DataTable();
if (ds.Tables[0] != null)
{
dt2 = ds.Tables[0];
}
int rowCount = 1;
foreach (DataRow dr in dt2.Rows)
{
rowCount += 1;
for (int i = 1; i < dt2.Columns.Count + 1; i++)
{
if (rowCount == 2)
{
oSheet.Cells[1, i] = arrays[i - 1];// dt.Columns[i - 1].ColumnName;
// oSheet.Cells[1,i].Font.Background = System.Drawing.Color.Red;
}
oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
////// Resize the columns
//oRange = oSheet.get_Range(1, 23);
////oRange.EntireColumn.AutoFit();
//oRange.Range[1, 2].AutoFit();
// Save the sheet and close
oSheet = null;
// oRange = null;
//oWB.SaveAs(@"c:\REPORTS.xls", Excel.XlFileFormat.xlWorkbookNormal,
// Missing.Value, Missing.Value, Missing.Value, Missing.Value,
// Excel.XlSaveAsAccessMode.xlExclusive,
// Missing.Value, Missing.Value, Missing.Value,
// Missing.Value, Missing.Value);
//oWB.Close(Missing.Value, Missing.Value, Missing.Value);
//oWB = null;
//oXL.Quit();
// Clean up
// NOTE: When in release mode, this does the trick
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
public void ExportToExel(数据集ds,字符串[]数组)
{
Excel.applicationoxl;
Excel.oWB工作手册;
Excel.oSheet工作表;
//Excel.Range橙色;
oXL=新的Excel.Application();
//设置一些属性
可见=真;
oXL.DisplayAlerts=false;
//获取新工作簿。
oWB=oXL.Workbooks.Add(缺少.Value);
//获取活动工作表
oSheet=(Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name=“EXCEL”;
DataTable dt2=新的DataTable();
如果(ds.表[0]!=null)
{
dt2=ds.表[0];
}
int rowCount=1;
foreach(dt2.Rows中的数据行dr)
{
行计数+=1;
对于(int i=1;i
此代码中将弹出异常错误<代码>oSheet.Cells[1,i]=数组[i-1]代码>
你们知道怎么会这样吗?我不明白我的代码出了什么问题。。请帮我做这个。提前感谢您的帮助。尝试更改
oSheet=(Excel.Worksheet)oWB.ActiveSheet
到
oSheet=(Excel.Worksheet)oXL.ActiveSheet
进一步说明:MSDN提供了以下有关工作簿接口的说明
此接口由Visual Studio Tools for Office runtime实现。它不打算在代码中实现。有关详细信息,请参阅Visual Studio Tools for Office runtime概述
这就是为什么在初始化新工作簿后必须引用应用程序接口的原因
如果这解决了您的问题,请将其标记为已回答。您确定要使用互操作库吗?它们很难调试。EPPlus、NPOI和openofficexmlsdk都更易于使用。