to get all the tables in the docx file, you can use the following code:
using System; using Independentsoft.Office; using Independentsoft.Office.Word; using Independentsoft.Office.Word.Tables; namespace Sample { class Program { static void Main(string[] args) { WordDocument doc = new WordDocument("c:\\test.docx"); Table[] tables = doc.GetTables(); foreach (Table table in tables) {
And to write them to an excel file, you have to do this for each cell:
app.Visible = false; workbooks = app.Workbooks; workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet); sheets = workbook.Worksheets; worksheet = (_Worksheet)sheets.get_Item(1); excel(row, column, "value"); workbook.Saved = true; workbook.SaveAs(output_file); app.UserControl = false; app.Quit();
and finally, the excel function is as follows:
public void excel(int row, int column, string value) { worksheet.Cells[row, column] = value; }
You can also use the CSV or HTML format to create an excel file. to do this, simply create an example.xlsx file with this content for the CSV comma:
col1, col2, col3, col4 \ n
val1, val2, val3val4 \ n
or in HTML format:
<table> <tr> <td>col1</td> <td>col2</td> <td>col3</td> </tr> <tr> <td>val1</td> <td>val2</td> <td>val3</td> </tr> </table>
source share