简体   繁体   English

使用OpenXML将Word docx转换为Excel

[英]Convert Word docx to Excel using OpenXML

Is there any way to convert Word document where I have some tables into Excel file? 有没有办法将Word文档转换为Excel文件? It would be very helpful to convert tables. 转换表格会非常有帮助。

Something like that: 像这样的东西:

  • Open Word document using OpenXML 使用OpenXML打开Word文档
  • Find all tables xml-tags 找到所有表格xml-tags
  • Copy xml-tags 复制xml标签
  • Create Excel file 创建Excel文件
  • Insert xml-tags with table from Word to new Excel file 将带有Word的表格的xml-tags插入到新的Excel文件中

I mean 我的意思是

void OpenWordDoc(string filePath)
{
_documentWord = SpreadsheetDocument.Open(filePath, true);
}

List<string> GetAllTablesXMLTags()
{
//find and copy
}

List<string> CreateExcelFile(string filePath)
{
TemplateExcelDocument excelDocument = new TemplateExcelDocument();
_documentExcel = excelDocument.CreatePackage(filePath);
}

void InsertXmlTagsToExcelFile(string filePath)
{
CreateExcelFiles(filePath);
var xmlTable = GetAllTablesXMLTags();
// ... insert to _documentExcel
}

your steps are correct. 你的步骤是正确的。

I would like to share some sdk documents, hope it could help to some extent: 我想分享一些sdk文档,希望它在某种程度上有所帮助:

Open XML SDK 2.5 for Office 打开适用于Office的XML SDK 2.5

When handling the word tables: 处理单词表时:

Working with WordprocessingML tables (Open XML SDK) 使用WordprocessingML表(Open XML SDK)

When processing excel tables: 处理excel表时:

Working with the shared string table (Open XML SDK) 使用共享字符串表(Open XML SDK)

Working with SpreadsheetML tables (Open XML SDK) 使用SpreadsheetML表(Open XML SDK)

to get all tables in the docx file you can use code below : 要获取docx文件中的所有表,您可以使用以下代码:

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)
            {
                //read data
            }

        }
    }
}

And to write them into an excel file you have to do this for each cell : 要将它们写入excel文件,您必须为每个单元格执行此操作:

 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 excel function is as below : 最后excel功能如下:

    public void excel(int row, int column, string value)
    {
        worksheet.Cells[row, column] = value;
    }

Also you can use CSV or HTML format to create an excel file. 您也可以使用CSVHTML格式创建Excel文件。 to do that simply create a file example.xlsx with this content for CSV comma delmiated : 要做到这一点,只需创建一个文件example.xlsx ,其内容为CSV逗号分隔:

col1,col2,col3,col4 \\n col1,col2,col3,col4 \\ n

val1,val2,val3val4 \\n val1,val2,val3val4 \\ n

or in HTML format : 或以HTML格式:

<table>
 <tr>
  <td>col1</td>
  <td>col2</td>
  <td>col3</td>
 </tr>
 <tr>
  <td>val1</td>
  <td>val2</td>
  <td>val3</td>
 </tr>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM