簡體   English   中英

數據到Excel工作表中的表

[英]Data to Table in Excel Sheet

我被分配了解析Word文檔格式並將其放入Excel工作表的任務。 下面的代碼很容易做到這一點..但是我最近了解到我需要將數據放入Excel表中,而不僅僅是表格中的單元格。

For row = 1 To theTable.Rows.Count   `until end of rows
                isHeaderOrNot = theTable.Cell(row, 0).Range.Text `if first field in row is a header
                If isHeaderOrNot.Contains("Section") Or isHeaderOrNot.Contains("Field") Then Continue For
                keyText = theTable.Cell(row, 2).Range.Text    `gets key text
                valueText = theTable.Cell(row, 3).Range.Text  `gets value text
                cleanStringKey = Regex.Replace(keyText, Chr(7), "") `clean strings
                cleanStringValue = Regex.Replace(valueText, Chr(7), "")


                xlWorkSheet.Cells(1, column) = cleanStringKey `puts key in row 1 and column n
                xlWorkSheet.Cells(2, column) = cleanStringValue `puts value in row 2 column n

                column = column + 1 `increment column
            Next

我想知道是否必須完全更改我的代碼才能使其成為表格...總之

在此處輸入圖片說明

在此處輸入圖片說明

我對VB.net完全陌生,因此,如果可能的話,請盡可能使所有內容變得愚蠢。

您可以使用WorkSheet.ListObject Add方法創建一個新列表(表)。

添加對Microsoft.Office.Interop.Excel引用后,將此導入添加到表單:

Imports XL = Microsoft.Office.Interop.Excel

然后使用這樣的代碼創建一個列表:

Dim Application = New XL.Application()
Application.Visible = True
Dim book = Application.Workbooks.Add()
Dim sheet = DirectCast(book.Worksheets(1), XL.Worksheet)
sheet.Cells(1, 1) = "Product"
sheet.Cells(1, 2) = "Price"
sheet.Cells(2, 1) = "Product 1"
sheet.Cells(2, 2) = "100"
sheet.Cells(3, 1) = "Product 2"
sheet.Cells(3, 2) = "200"
sheet.Cells(4, 1) = "Product 3"
sheet.Cells(4, 2) = "300"
Dim list = sheet.ListObjects.Add( _
                XL.XlListObjectSourceType.xlSrcRange, _
                sheet.Range(sheet.Cells(1, 1), sheet.Cells(4, 2)), _
                Type.Missing, XL.XlYesNoGuess.xlYes)

然后,您將看到以下結果:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM