Excel表中的数据表

我被赋予了parsingWord文档表单的任务,并将其放入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方法来创build一个新的列表(表)。

添加对Microsoft.Office.Interop.Excel引用后,将此导入添加到窗体中:

 Imports XL = Microsoft.Office.Interop.Excel 

然后使用这样的代码来创build一个列表:

 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) 

然后你会看到这个结果:

在这里输入图像说明