如何使用OpenXML将Word表转换为Excel

时间:2016-02-23 10:30:34

标签: c# visual-studio

我希望使用OpenXML读取WORD表数据的内容并将它们导出到excel文件。这个词有很多页面。但是没有读到结束文档。如何将所有word表数据传输到excel。请。感谢

class Program
{
    static void Main(string[] args)
    {

        string wordFile = "D:\\Sample.docx";

        DataTable dataTable = ReadWordTable(wordFile);


        if (dataTable != null)
        {
            string sFile = "D:\\Sample.xlsx";

            ExportDataTableToExcel(dataTable, sFile);


            Console.WriteLine("Contents of word table exported to excel spreadsheet");

            Console.ReadKey();
        }
    }

    public static DataTable ReadWordTable(string fileName)
    {
        DataTable table;

        try
        {
            using (var document = WordprocessingDocument.Open(fileName, false))
            {
                var docPart = document.MainDocumentPart;

                var doc = docPart.Document;

                DocumentFormat.OpenXml.Wordprocessing.Table myTable = doc.Body.Descendants<DocumentFormat.OpenXml.Wordprocessing.Table>().FirstOrDefault();

                List<List<string>> totalRows = new List<List<string>>();
                int maxCol = 0;

                foreach (TableRow row in myTable.Elements<TableRow>())
                {
                    List<string> tempRowValues = new List<string>();
                    foreach (TableCell cell in row.Elements<TableCell>())
                    {
                        tempRowValues.Add(cell.InnerText);
                    }

                    maxCol = ProcessList(tempRowValues, totalRows, maxCol);
                }

                table = ConvertListListStringToDataTable(totalRows, maxCol);
            }

            return table;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);

            return null;
        }
    }
    private static int ProcessList(List<string> tempRows, List<List<string>> totalRows, int MaxCol)
    {
        if (tempRows.Count > MaxCol)
        {
            MaxCol = tempRows.Count;
        }

        totalRows.Add(tempRows);
        return MaxCol;
    }
    private static DataTable ConvertListListStringToDataTable(List<List<string>> totalRows, int maxCol)
    {
        DataTable table = new DataTable();
        for (int i = 0; i < maxCol; i++)
        {
            table.Columns.Add();
        }
        foreach (List<string> row in totalRows)
        {
            while (row.Count < maxCol)
            {
                row.Add("");
            }
            table.Rows.Add(row.ToArray());
        }
        return table;
    }
    private static void ExportDataTableToExcel(DataTable table, string exportFile)
    {
        try
        {

            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                Create(exportFile, SpreadsheetDocumentType.Workbook);


            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new Workbook();


            WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());


            Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
                AppendChild<Sheets>(new Sheets());


            Sheet sheet = new Sheet()
            {
                Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "mySheet"
            };
            sheets.Append(sheet);

            SheetData data = worksheetPart.Worksheet.GetFirstChild<SheetData>();


            Row header = new Row();
            header.RowIndex = (UInt32)1;

            foreach (DataColumn column in table.Columns)
            {
                Cell headerCell = createTextCell(
                    table.Columns.IndexOf(column) + 1,
                    1,
                    column.ColumnName);

                header.AppendChild(headerCell);
            }
            data.AppendChild(header);


            DataRow contentRow;
            for (int i = 0; i < table.Rows.Count; i++)
            {
                contentRow = table.Rows[i];
                data.AppendChild(createContentRow(contentRow, i + 2));
            }

            workbookpart.Workbook.Save();


            spreadsheetDocument.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    private static Cell createTextCell( int columnIndex, int rowIndex, object cellValue)
    {
        Cell cell = new Cell();

        cell.DataType = CellValues.InlineString;
        cell.CellReference = getColumnName(columnIndex) + rowIndex;

        InlineString inlineString = new InlineString();
        DocumentFormat.OpenXml.Spreadsheet.Text t = new DocumentFormat.OpenXml.Spreadsheet.Text();

        t.Text = cellValue.ToString();
        inlineString.AppendChild(t);
        cell.AppendChild(inlineString);

        return cell;
    }

    private static Row createContentRow( DataRow dataRow, int rowIndex)
    {
        Row row = new Row
        {
            RowIndex = (UInt32)rowIndex
        };

        for (int i = 0; i < dataRow.Table.Columns.Count; i++)
        {
            Cell dataCell = createTextCell(i + 1, rowIndex, dataRow[i]);
            row.AppendChild(dataCell);
        }
        return row;
    }
    private static string getColumnName(int columnIndex)
    {
        int dividend = columnIndex;
        string columnName = String.Empty;
        int modifier;

        while (dividend > 0)
        {
            modifier = (dividend - 1) % 26;
            columnName =
                Convert.ToChar(65 + modifier).ToString() + columnName;
            dividend = (int)((dividend - modifier) / 26);
        }

        return columnName;
    }
}

0 个答案:

没有答案