2013-07-08 48 views
0

我在窗體上有兩個文本框;一個持有這個詞,另一個持有它的數值,這個數值是根據用戶輸入的內容計算出來的。我有一個分開每個句子,並顯示在Excel中的每個句子的總和。這是從下面列出的vb.net代碼生成的輸出。所有單詞都在A列中,B列中的所有值。如何修改代碼,以便將每個句子及其值分別插入兩列中?在到達字符串「Total for sentence 1」之後。它應該開始將字和它的值插入列C & D等等。 句子一及其值應顯示在列A & B中,句子二應將其值顯示在列C & D等中。 請參閱附件圖片。紅色文本是我目前所擁有的文本,並且在它下面是期望的結果。謝謝! enter image description here在vb.net的Excel工作表單元格中顯示文本框結果

我在vb.net代碼是:

Private Sub Export_Click(sender As System.Object, e As System.EventArgs) Handles btnCreate.Click 

    xlWorkBook = xlApp.Workbooks.Add 

    xlApp.Visible = True 

    xlWorkSheet = xlWorkBook.Sheets("Sheet1") 
    Dim columnANum As Integer 
    Dim columnBNum 
    columnBNum = 2 
    columnANum = 2 
    With xlWorkSheet 
     .Range("A1").Value = "Word" 
     For Each cellA As String In txtWord.Text.Split(vbLf) 

      .Range("A" & columnANum.ToString).Value = cellA 
      columnANum += 1 
     Next 
     .Range("B1").Value = "Value" 
     For Each cellB As String In txtValue.Text.Split(vbLf) 
      .Range("B" & columnBNum.ToString).Value = Convert.ToInt32(cellB) 
      columnBNum += 1 
     Next 


    End With 
End Sub 

回答

1

編輯:我對此代碼進行了重大更改並進行了測試;它應該做你正在尋找的

Sub testExcelColumns() 
    Dim xlApp As Object = CreateObject("Excel.Application") 
    Dim xlWorkBook As Object 
    Dim xlWorkSheet As Object 

    xlWorkBook = xlApp.Workbooks.Add 
    xlApp.Visible = True 

    xlWorkSheet = xlWorkBook.Sheets("Sheet1") 
    Dim RowNum As Integer = 1 
    Dim ColNum As Integer = 1 

    'use .cells(row, column) instead of .range 
    xlWorkSheet.cells(RowNum, ColNum).Value = "Word" 

    For Each cellA As String In txtWord.Text.Split(vbLf) 

     'increment the row 
     RowNum += 1 

     'set the value 
     xlWorkSheet.cells(RowNum, ColNum).value = cellA 

     If cellA.StartsWith("Total") Then 
      ColNum += 2 
      RowNum = 1 
      xlWorkSheet.cells(RowNum, ColNum).Value = "Word" 
     End If 

    Next 

    'increment to the second column and first row 
    ColNum = 2 
    RowNum = 1 

    'set title 
    xlWorkSheet.cells(RowNum, ColNum).Value = "Value" 

    For Each cellB As String In txtValue.Text.Split(vbLf) 

     'increment the row 
     RowNum += 1 

     'set the value 
     xlWorkSheet.cells(RowNum, ColNum).value = cellB 

     'get value of cell to the left 
     Dim t As String = xlWorkSheet.cells(RowNum, ColNum).offset(0, -1).value 
     If Not t Is Nothing AndAlso t.StartsWith("Total") Then 
      ColNum += 2 
      RowNum = 1 
      xlWorkSheet.cells(RowNum, ColNum).Value = "Value" 
     End If 

    Next 

End Sub 
+0

當我運行代碼時,我得到了「if not.t.StartWith(」Total「Then。這些值僅插入到B列中,它們不會循環並轉到B,E等下一列。此外,標題詞不會像C一樣轉移到下一列。標題值根本不填充。謝謝! – user2536008

+0

糟糕,錯誤「對象引用未設置爲實例的一個對象vb.net「是因爲有時t不會是什麼。測試這種情況是這樣的:如果不是沒有,並且還有t.StartsWith(「Total」)那麼 –

0

根據需要使用

Dim RowNum as Integer = 1 
Dim ColNum as Integer = 1 

使用.cells代替.range

xlWorkSheet.Cells(RowNum, ColNum).value = "Word" 
xlWorkSheet.Cells(RowNum, ColNum).value = "Value" 

更新的行和列的嘗試

RowNum += 1 
ColNum += 1 
+0

我試過上面的代碼,它沒有循環通過列。 – user2536008

+0

xlWorkSheet.Cells(1,1)。價值= 「字」 xlWorkSheet.Cells(1,2)。價值= 「值」 對於每個CELLA作爲字符串在txtWord.Text.Split(vbLf) xlWorkSheet.Cells( RowNum,1).value = cellA 如果列A中的單元格值=「總計」,iMove將移動到C&D的下一列? ROWNUM + = 1 ColNum + = 2 接着 ROWNUM = 2 對於每個cellB作爲字符串在txtValue.Text.Split(vbLf) xlWorkSheet.Cells(ROWNUM,2)。價值= cellB ROWNUM + = 1 ColNum + = 2 Next – user2536008

相關問題