2016-06-17 2 views
0

Excel 스프레드 시트의 특정 범위로 보내려고하는 VB.net의 데이터 테이블이 있습니다. 다음 하위에VB.NET에서 Excel로 데이터 테이블을 전송하는 방법

An exception of type 'System.Runtime.InteropServices.COMException' occurred in MeasurementFinder.dll but was not handled in user code

Additional information: Exception from HRESULT: 0x800A03EC

오류 알림 :

Private Sub WriteDataTableToRng(targetWs As Excel.Worksheet, anchor As Excel.Range, tbl As System.Data.DataTable) 
    'This sub writes the given tbl to the targetWs as a range with its top left cell acting as anchor 
    Dim wRange As Excel.Range = anchor 'wRange = write range. This range represents the cell being written to over every iteration 

    For Each colm As DataColumn In tbl.Columns 'This loop writes the column names into the target ws 
     targetWs.Range(wRange).Value2 = colm.ColumnName '**THIS LINE IS CALLED OUT BY THE ERROR 
     wRange = wRange.Offset(0, 1) 
    Next colm 
    wRange = anchor.Offset(1, 0) 
    For Each row As DataRow In tbl.Rows 
     For Each col As DataColumn In tbl.Columns 
      targetWs.Range(wRange).Value2 = tbl.Rows.Item(tbl.Rows.IndexOf(row)).Item(tbl.Columns.IndexOf(col)) '**THIS LINE IS CALLED OUT BY THE SAME ERROR IF THE PREVIOUS LOOP IS COMMENTED OUT 
     Next col 
    Next row 
End Sub 

이전이 호출하는 서브 : 그러나, 프로그램을 실행시 나는 오류가

Private Sub ReportOnTube(TubeID As Integer) 
    'This sub creates an Excel workbook that acts as a report on a tube, given its ID 
    'The report has a worksheet for each measurement tied to the tube (From the Gauge DB) 

    'Verify the tube is in the DB 
    Dim TubeExists As Boolean 
    TubeExists = VerifyTube(TubeID) 
    If TubeExists Then 
     'Create a new excel workbook and name/time stamp it 
     Dim wb As Excel.Workbook = Me.Application.Workbooks.Add() 
     wb.SaveAs("C:\Gauge Reports\Tube " & TubeID & System.DateTime.Now.ToString(" HH_mm_ss dd-MM-yyyy")) 
     'Add a worksheet for each measurement tied to the tube 
     Dim ws As Excel.Worksheet 
     ws = wb.Worksheets.Add 
     Dim aRng As Range 
     aRng = ws.Range("B2") 
     TubesConn.Close() 
     TubesConn.Open() 
     Dim selectTbl As New SqlCommand("SELECT * FROM [Tubes]", TubesConn) 
     Dim rdr As SqlDataReader 
     Dim aTbl As New System.Data.DataTable 
     rdr = selectTbl.ExecuteReader() 
     aTbl.Load(rdr) 
     Call WriteDataTableToRng(ws, aRng, aTbl) 
     TubesConn.Close() 
    End If 
End Sub 

I 다음 수입품을 사용하고 있습니다 :

Imports System.Data.Sql 
Imports System.Data.SqlClient 
Imports System.Data 
Imports System.IO 
Imports Microsoft.Office.Interop.Excel 

내가하려는 것은 주어진 데이터 테이블을 반복하고 표의 값을 스프레드 시트의 "앵커"범위 변수에 의해 주어진 왼쪽 상단 모서리에있는 범위에 쓰는 것입니다. Visual Stuio의 IntelliSense에 대한 경고가 없으므로 어디서부터 시작해야할지 모릅니다.

미리 감사드립니다.

+0

디버거는 매우 유용합니다. 문제의 코드를 단계별로 실행하면 무엇이 알려 집니까? –

답변

1

targetWs.Range(wRange).Value2 = colm.ColumnName은 중복되거나 잘못되었습니다. wRange에 저장된 Excel 범위 개체는 이미 포함 된 워크 시트의 속성입니다.

다른 말로하면 wRange.Parent.Name을 인쇄하면 범위가있는 워크 시트를 얻을 수 있습니다. 두 개의 다른 워크 시트에 범위를 지정할 수는 없습니다 (범위 통합과 같은 방식 일 수 있습니다. 시도하지,하지만 누가 대신에, 단지 사용 어쨌든, 당신은 아마 ... </streamOfConciousness>)

을 할 수없는 것을 할 것입니다 결코 : 문제의이 종류를 추적 할 때

wrange.value = colm.columnName 
관련 문제