如何转换DocumentFormat.OpenXML

时间:2016-02-23 14:38:18

标签: c# xml excel

从Excel文件转换为XML文件时遇到问题。当我转换表格代码中的信息时,它会转换为<table1></table1> <table1></table1> <table1></table1>,但我需要转换<table1></table1><table2></table2>...中的每一列 代码如下。

private DataTable ReadExcelFile(string filename)
    {
        // Initialize an instance of DataTable
        DataTable dt = new DataTable();

        try
        {
            // Use SpreadSheetDocument class of Open XML SDK to open excel file
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filename, false))
            {
                // Get Workbook Part of Spread Sheet Document
                WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;

                // Get all sheets in spread sheet document 
                // IEnumerable Interface
                // Exposes an enumerator, which supports a simple iteration over a non-generic collection.
                IEnumerable<Sheet> sheetcollection = spreadsheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();

                // Get relationship Id
                string relationshipId = sheetcollection.First().Id.Value;

                // Get sheet1 Part of Spread Sheet Document
                WorksheetPart worksheetPart = (WorksheetPart)spreadsheetDocument.WorkbookPart.GetPartById(relationshipId);

                // Get Data in Excel file
                SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
                IEnumerable<Row> rowcollection = sheetData.Descendants<Row>();

                if (rowcollection.Count() == 0)
                {
                    return dt;
                }

                // Add columns
                foreach (Cell cell in rowcollection.ElementAt(0))
                {
                    dt.Columns.Add(GetValueOfCell(spreadsheetDocument, cell));
                }

                // Add rows into DataTable
                foreach (Row row in rowcollection)
                {
                    DataRow temprow = dt.NewRow();
                    int columnIndex = 0;
                    foreach (Cell cell in row.Descendants<Cell>())
                    {
                        // Get Cell Column Index
                        int cellColumnIndex = GetColumnIndex(GetColumnName(cell.CellReference));

                        if (columnIndex < cellColumnIndex)
                        {
                            do
                            {
                                temprow[columnIndex] = string.Empty;
                                columnIndex++;
                            }

                            while (columnIndex < cellColumnIndex);
                        }

                        temprow[columnIndex] = GetValueOfCell(spreadsheetDocument, cell);
                        columnIndex++;
                    }

                    // Add the row to DataTable
                    // the rows include header row
                    dt.Rows.Add(temprow);
                }
            }

            // Here remove header row
            dt.Rows.RemoveAt(0);
            return dt;
        }
        catch (IOException ex)
        {
            throw new IOException(ex.Message);
        }
    }

    private static string GetValueOfCell(SpreadsheetDocument spreadsheetdocument, Cell cell)
    {
        // Get value in Cell
        SharedStringTablePart sharedString = spreadsheetdocument.WorkbookPart.SharedStringTablePart;
        if (cell.CellValue == null)
        {
            return string.Empty;
        }

        string cellValue = cell.CellValue.InnerText;

        // The condition that the Cell DataType is SharedString
        if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
        {
            return sharedString.SharedStringTable.ChildElements[int.Parse(cellValue)].InnerText;
        }
        else
        {
            return cellValue;
        }
    }

    private string GetColumnName(string cellReference)
    {
        // Create a regular expression to match the column name of cell
        Regex regex = new Regex("[A-Za-z]+");
        Match match = regex.Match(cellReference);
        return match.Value;
    }

    private int GetColumnIndex(string columnName)
    {
        int columnIndex = 0;
        int factor = 1;

        // From right to left
        for (int position = columnName.Length - 1; position >= 0; position--)   
        {
            // For letters
            if (Char.IsLetter(columnName[position]))
            {
                columnIndex += factor * ((columnName[position] - 'A') + 1) - 1;
                factor *= 26;
            }
        }

        return columnIndex;
    }

    public string GetXML(string filename)
    {
        using (DataSet ds = new DataSet())
        {
            ds.Tables.Add(this.ReadExcelFile(filename));
            return ds.GetXml();
        }
    }

有人可以帮我解决这个问题吗? 或者任何想法如何创建自定义标签?

提前致谢。

0 个答案:

没有答案