I usually use MS Excel to store and process large amounts of data, but sometimes I often encounter a problem-the data i need is stored in a Word table, not in Excel, which is cumbersome to handle, especially when the data is huge.
At this point, I urgently need to import the data from the Word table into Excel. I believe you have encountered the same problem, I would like to share with you in C # How to use the free control to achieve this function. Here, I used two free APIs,
Docx and Spire.xls.
Friends who need it can be downloaded and used. :
Docx:codeplex official website
Spire.xls:e-iceblue official website
Note: After you create the project, you need to add the relevant. dll files for both controls as a reference to the project.
Here are the detailed steps:
First I use the docx API to get the data in the Word table and then import the data into the System.Data.DataTable object .
Code snippet:
Steps 1: load Word document;
DocX document = Docx.load ("e:\\program files\\data.docx");
Steps 2 : Gets the table from the document. As an example, I only get the first table here;
// get the first table of a document = document. tables[0];
Steps 3 : Create a DataTable object and import the data from the Word table;
//Create a Datable object and name the orderDataTable DT=NewDataTable ("Order");//import data from a Word table into datableDataColumn column; for(inti =0; I < table. ColumnCount; i++) {column=NewDataColumn (); column. ColumnName= table. rows[0]. Cells[i]. paragraphs[0]. Text;dt. Columns.Add (column);} DataRow NewRow; for(inti =1; I < table. RowCount; i++) {NewRow=dt. NewRow (); Row Row=table. Rows[i]; for(intj =0; J < table. ColumnCount; J + +) {Newrow[j]= row. CELLS[J]. paragraphs[0]. Text;} Dt. Rows.Add (NewRow);}
As follows:
Original Word Document:
Datable:
Second, I used the Spire.xls API to create a workbook object, insert a DataTable into workbook, and then save the file as an. xlsx file.
Code snippet:
Steps 1 : Create a workbook instance and a new empty worksheet;
// Create a workbook example New Workbook (); // Create a new empty worksheet workbook. Createemptysheets (1); // get the first worksheet = workbook. worksheets[0];
Steps 2 : import data from a DataTable into the worksheet;
// inserting data from a DataTable into worksheet, 1 for the first row and the first column true11);
Steps 3 : save to an Excel file;
Workbook. SaveToFile ("data.xlsx", excelversion.version2007);
The generated Excel file:
All code:
usingSystem.Data;usingSpire.xls;usingNovacode;//Docxnamespaceword_table_to_excel{classProgram {Static voidMain (string[] args) {DocX document= Docx.load ("E:\\program Files\\data.docx"); Table Table= document. tables[0]; DataTable DT=NewDataTable ("Order"); DataColumn column; for(inti =0; I < table. ColumnCount; i++) {column=NewDataColumn (); Column. ColumnName= table. rows[0]. Cells[i]. paragraphs[0]. Text; Dt. Columns.Add (column); } DataRow NewRow; for(inti =1; I < table. RowCount; i++) {NewRow=dt. NewRow (); Row Row=table. Rows[i]; for(intj =0; J < table. ColumnCount; J + +) {Newrow[j]= row. CELLS[J]. paragraphs[0]. Text; } dt. Rows.Add (NewRow); } Workbook Workbook=NewWorkbook (); Workbook. Createemptysheets (1); Worksheet sheet= Workbook. worksheets[0]; Sheet. insertdatatable (DT,true,1,1); Workbook. SaveToFile ("data.xlsx", excelversion.version2007); } }}
Summarize:
This is a free two independent software that does not require Ms Office to be installed when used. Save me a lot of time. If you have good ideas or suggestions, you may want to share them with each other here.
I hope this article will bring you some help.
How to use free controls to import data from a Word table into Excel