2013-05-09 4 views
6

Gibt es eine Möglichkeit, Word-Dokument zu konvertieren, wo ich einige Tabellen in Excel-Datei habe? Es wäre sehr hilfreich, Tabellen zu konvertieren.Konvertieren von Word DOCX zu Excel mit OpenXML

So etwas:

  • öffnen Word-Dokument OpenXML mit
  • Alle Tabellen XML-Tags
  • Copy xml-Tags
  • erstellen Excel
  • Insert XML-Tags mit Tabellendatei von Word zu neuer Excel-Datei

ich meine

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 
} 

Antwort

1

alle Tabellen in der docx-Datei erhalten Sie folgenden Code verwenden:

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 
      } 

     } 
    } 
} 

Und schreiben sie in eine Excel-Datei das Sie tun müssen, um für jede Zelle:

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(); 

und schließlich Funktion treffen ist wie folgt:

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

Sie können auch das Format CSV oder HTML verwenden, um eine Excel-Datei zu erstellen. zu tun, dass einfach eine Datei erstellen example.xlsx mit diesem Inhalt für CSV Komma delmiated:

col1, col2, col3, col4 \ n

val1, val2, val3val4 \ n

oder 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> 
+0

Leider brauche ich ähnliche Funktion, aber mit OpenXML –

Verwandte Themen