0

我被困在这里,我想将数据从 5 个文本框插入到现有的 excel 文件中。我找到了代码,但它是按行插入的。我在下面的代码中找到了最后一个非空行,我想从该行移动到下一行并在那里插入数据,例如最后一行是 A2,我想将新数据插入 A3、B3、C3、D3 ,E3。

我无法为此获得正确的循环。

Dim lRow As Long = 0
    Call OpenExcelFile("C:\Users\PB\Desktop\BookRecords.xlsx", 1)

    With xlWorkSheet
        If EXL.WorksheetFunction.CountA(.Cells) <> 0 Then
            lRow = .Cells.Find(What:="*", _
                          After:=.Range("A2"), _
                          LookAt:=Excel.XlLookAt.xlPart, _
                          LookIn:=Excel.XlFindLookIn.xlFormulas, _
                          SearchOrder:=Excel.XlSearchOrder.xlByRows, _
                          SearchDirection:=Excel.XlSearchDirection.xlPrevious, _
                          MatchCase:=False).Row
        Else
            lRow = 1


        End If
        lRow += 1

    End With
    MessageBox.Show("The last row in Sheet1 which has data is " & lRow)
    Dim j As Integer
    j = 3
    For i As Integer = j To 8
        xlWorkSheet.Range(xlWorkSheet.Cells(lRow + 1, j).item).Value = txtTitle.Text
        xlWorkSheet.Range(xlWorkSheet.Cells(lRow + 1, j).item).Value = txtAuthor.Text
        xlWorkSheet.Range(xlWorkSheet.Cells(lRow + 1, j).item).Value = txtEdition.Text
        xlWorkSheet.Range(xlWorkSheet.Cells(lRow + 1, j).item).Value = txtPublisher.Text
        xlWorkSheet.Range(xlWorkSheet.Cells(lRow + 1, j).item).Value = txtISBN.Text
    Next
    j += 1

    Call SaveAndCloseExcelSub()
4

2 回答 2

2

这有帮助吗:

Dim j As Integer
j = 3
For i As Integer = j To 8
MsgBox("Cell column: " & ColumnNumberToName(i))
Next

将数字转换为 Excel 列名(AZ、AA 等)的代码:

Public Shared Function ColumnNumberToName(columnNumber As Int32) As String
Dim dividend As Int32 = columnNumber
Dim columnName As [String] = [String].Empty
Dim modulo As Int32
While dividend > 0
    modulo = (dividend - 1) Mod 26
    columnName = Convert.ToChar(65 + modulo).ToString() + columnName
    dividend = DirectCast(((dividend - modulo) / 26), Int32)
End While

Return columnName
End Function

Public Shared Function ColumnNameToNumber(columnName As [String]) As Int32
If [String].IsNullOrEmpty(columnName) Then
    Throw New ArgumentNullException("columnName")
End If
Dim characters As Char() = columnName.ToUpperInvariant().ToCharArray()
Dim sum As Int32 = 0
Dim i As Int32 = 0
While i < characters.Length
    sum *= 26
    sum += (characters(i) - "A"C + 1)
    System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
Return sum
End Function
于 2013-03-09T06:00:30.903 回答
2

实际上,您的所有值都被写入同一个单元格;然后你向下移动一行,然后再做一次。单元格被寻址为 (row, column) - 所以你需要像这样更改你的代码:

Dim j As Integer
j = 3
For i As Integer = j To 8
    xlWorkSheet.Cells(lRow + i, 1).Value = txtTitle.Text
    xlWorkSheet.Cells(lRow + i, 2).Value = txtAuthor.Text
    xlWorkSheet.Cells(lRow + i, 3).Value = txtEdition.Text
    xlWorkSheet.Cells(lRow + i, 4).Value = txtPublisher.Text
    xlWorkSheet.Cells(lRow + i, 5).Value = txtISBN.Text
Next

不确定是否需要开始i = 3添加lRow,但我相信你可以解决这个问题。

如果您想颠倒顺序,请执行此操作(此示例仅复制大量文本...我开始怀疑不需要循环)

dim i as Integer
i = 1; ' if you want column A
With xlWorkSheet
    .Cells(lRow    , i).Value = txtTitle.Text
    .Cells(lRow + 1, i).Value = txtAuthor.Text
    .Cells(lRow + 2, i).Value = txtEdition.Text
    .Cells(lRow + 3, i).value = txtPublisher.Text
    .Cells(lRow + 4, i).Value = txtISBN.Text
End With
于 2013-03-09T06:03:53.540 回答