Table Excel Sheet 中的数据
Data to Table in Excel Sheet
我的任务是解析 word 文档格式,并将其放入 Excel sheet。下面的代码很容易做到这一点......但我最近了解到我需要将该数据放入 Excel table 而不仅仅是 sheet.[=16= 的单元格]
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
我想知道我是否必须完全更改我的代码才能使其成为 table...简而言之,从
到
我是 VB.net 的新手,所以如果可以的话,请尽可能简化所有内容。
您可以使用 Add
method of WorkSheet.ListObject
创建新列表 (table)。
例子
添加对 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)
然后你会看到这个结果:
我的任务是解析 word 文档格式,并将其放入 Excel sheet。下面的代码很容易做到这一点......但我最近了解到我需要将该数据放入 Excel table 而不仅仅是 sheet.[=16= 的单元格]
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
我想知道我是否必须完全更改我的代码才能使其成为 table...简而言之,从
到
我是 VB.net 的新手,所以如果可以的话,请尽可能简化所有内容。
您可以使用 Add
method of WorkSheet.ListObject
创建新列表 (table)。
例子
添加对 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)
然后你会看到这个结果: