![]() ![]() 'Copy content from a Word table cell to an Excel cell Private Shared Function ExportTableInExcel(ByVal worksheet As Worksheet, ByVal row As Integer, ByVal table As Table) As IntegerĬell.BorderAround(LineStyleType.Thin, Color.Black) 'Export data from Word table to Excel cells Wb.SaveToFile("WordToExcel.xlsx", ExcelVersion.Version2013) 'Copy paragraph from Word to a specific cellĭim table As Table = documentObject as Tableĭim currentRow As Integer = ExportTableInExcel(worksheet,row,table) If TypeOf documentObject Is Paragraph Thenĭim cell As CellRange = worksheet.Range(row,column)ĭim paragraph As Paragraph = documentObject as Paragraph 'Loop through the document object under a certain sectionįor Each documentObject In 'Loop through the sections in the Word document Switch ()Ĭ = HorizontalAlignType.Left Ĭ = HorizontalAlignType.Center Ĭ = HorizontalAlignType.Right ĭoc.LoadFromFile("C:\Users\Administrator\Desktop\Invoice.docx")ĭim worksheet As Worksheet = wb.CreateEmptySheet("WordToExcel") RichText.SetFont(startIndex, endIndex, font) ĭocPicture picture = documentObject as DocPicture Ĭ(cell.Row, cell.Column, picture.Image) Ĭ(cell.Row, 1, ) String fontName = īool isBold = Ĭolor textColor = įloat fontSize = ĮxcelFont font = () TextRange textRange = documentObject as TextRange Private static void CopyTextAndStyle(CellRange cell, Paragraph paragraph)įoreach (DocumentObject documentObject in paragraph.ChildObjects) Copy text and style of a paragraph to a cell Paragraph newPara = new Paragraph(tbCell.Document) įor (int i = 0 i < i++)ĭocumentObject documentObject = tbCell.ChildObjects įoreach (DocumentObject cObj in paragraph.ChildObjects) Private static void Cop圜ontentInTable(TableCell tbCell, CellRange cell) Copy content from a Word table cell to an Excel cell Private static int ExportTableInExcel(Worksheet worksheet, int row, Table table)įoreach (TableCell tbCell in tbRow.Cells)Ĭell.BorderAround(LineStyleType.Thin, Color.Black) Export data from Word table to Excel cells Int currentRow = ExportTableInExcel(worksheet, row, table) Copy paragraph from Word to a specific cell Paragraph paragraph = documentObject as Paragraph Loop through the document object under a certain sectionįoreach (DocumentObject documentObject in )ĬellRange cell = worksheet.Range Loop through the sections in the Word documentįoreach (Section section in doc.Sections) Worksheet worksheet = wb.CreateEmptySheet("WordToExcel") Save the workbook to an Excel file using Workbook.SaveToFile() method. ![]() Auto fit the row height and column width in Excel so that the data within a cell will not exceed the bound of the cell.If the document object is a table, export the table data from Word to Excel cells using ExportTableInExcel() method.If the document object is a paragraph, write the paragraph in a specified cell in Excel using CoypTextAndStyle() method.Traverse though all the sections in the Word document, traverse through all the document objects under a certain section, and then determine if a document object is a paragraph or a table.Create a Worbbook object and add a worksheet named "WordToExcel" to it.Create a Document object to load a Word file.The following steps demonstrate how to export data from an entire Word document to a worksheet using Spire.Office for. CopyTextAndStyle() - Copy text with formatting from a Word paragraph to an Excel cell.Cop圜ontentInTable() - Copy content from a table cell in Word to an Excel cell.ExportTableInExcel() - Export data from a Word table to specified Excel cells.To make this code example easy to understand, we created the following three custom methods that preform specific functions. The former is used to read and extract content from a Word document, and the latter is used to create an Excel document and write data in the specific cells. This scenario actually uses two libraries in the Spire.Office package. PM> Install-Package Spire.Office Convert Word to Excel in C# and VB.NET ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |