从Windows窗体写入Excel 2007时出现COMException

时间:2012-01-10 12:26:36

标签: vb.net excel-2007 com-interop excel-interop

我的Windows窗体应用程序从用户输入和数学计算中收集数据,并将一组结果输出到Excel电子表格。

这是我的代码(为方便起见,我只包含了与问题相关的代码):

        Dim exc As New Excel.Application
        Dim book As Excel.Workbook
        Dim sheet As Excel.Worksheet

        book = exc.Workbooks.Add
        sheet = book.Sheets.Add

        Dim dc As System.Data.DataColumn
        Dim dr As System.Data.DataRow
        Dim colIndex As Integer = 0
        Dim rowIndex As Integer = 1

        exc.Cells(0, 0) = "Clearspan Cladding Sheet"
        exc.Cells(0, 1) = dtpDate.Value
        exc.Cells(0, 2) = "Job No. " & txtJob.Text
        exc.Cells(0, 3) = "By " & txtName.Text

        For Each dc In dtTotals.Columns
            colIndex = colIndex + 1
            exc.Cells(1, colIndex) = dc.ColumnName
        Next

当我尝试Cell(0, 0)时发生错误。以下是我可以获得的最详细的错误信息。 For...Next循环起作用(在先前的调试会话中测试过)。据我所知,我正在做同样的事情。

System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at Microsoft.Office.Interop.Excel.Range.set__Default(Object RowIndex, Object ColumnIndex, Object Param)
   at Cladding.frmCladdingMain.Button1_Click(Object sender, EventArgs e) in C:\Users\logan.HO\Documents\Visual Studio 2010\Projects\Clearspan\Cladding\frmCladdingMain.vb:line 240

与往常一样,提前感谢所提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

点击此链接:http://support.microsoft.com/kb/301982 第一排& col值为1,1(即A1 = cell(1,1))。