2012-01-10 36 views
0

私のWindowsフォームアプリケーションは、ユーザー入力と数学計算からデータを収集し、一連の結果をExcelスプレッドシートに出力します。WindowsフォームからExcel 2007に書き込むときのCOMException

はここ(便宜上、私が問題に関連すると思われるコードのみを含めました)私のコードです:私はCell(0, 0)にしようとすると、

 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 

エラーが発生します。以下は、私がエラーで得ることができる最も詳細な情報です。 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 

いつもありがとうございました。

+2

AFAIK細胞コレクションは1ベースです。 (1,1)がA1 –

+0

です@Logan - 試してみてください.Cells()リファレンス - http://msdn.microsoft.com/en-us/library/ff194567.aspx – adatapost

答えて

0

このリンクを確認してください:http://support.microsoft.com/kb/301982 最初の行& col値は1,1です(つまりA1 = cell(1,1))。

+0

はい.. exc.Cells(1) 、1)= "Clearspanクラッドシート" exc.Cells(1、2)= dtpDate.Value exc.Cells(1、3)= "ジョブ番号" &txtJob.Text exc.Cells(1、4) = "By"&txtName.Text – Harsh

+0

コレクションを1ベースとして使用するとエラーはなくなりますが、そこに入っている情報はスプレッドシートに書き込まれていません – Ortund

+0

exc.Cells(1、colIndex).value = dc.ColumnName。このステートメントの後に列インデックスを増やします。 – Harsh

関連する問題