Converting Open XML to SpreadsheetML in Open XML SDK 2.7 C#

1.1k Views Asked by At

I have an excel workbook with a small table in Sheet 1 like this:

enter image description here

If I open the raw XML via 7zip or something, the Open XML output for the worksheet data looks like this:

   <sheetData>
    <row r="1" spans="1:5" x14ac:dyDescent="0.3">
        <c r="A1" t="s">
            <v>0</v>
        </c>
        <c r="B1" t="s">
            <v>1</v>
        </c>
        <c r="C1" t="s">
            <v>2</v>
        </c>
        <c r="D1" t="s">
            <v>3</v>
        </c>
        <c r="E1" t="s">
            <v>4</v>
        </c>
    </row>

Now, put down the pitch forks for a second. I know there are several other related files than just the worksheet data that involve formatting and layout. I've been working with the Open XML SDK Today and can access individual cell values that are human readable, etc.

What I'm trying to do is find a "easy" way to convert the OpenXML worksheet data to something similar to what you get if you were to save the file as a 2003 spreadsheet XML like this:

    <Worksheet ss:Name="Sheet1">
    <Table ss:ExpandedColumnCount="9" ss:ExpandedRowCount="5" x:FullColumns="1" x:FullRows="1" ss:DefaultRowHeight="14.4">
        <Column ss:Width="44.400000000000006"/>
        <Column ss:Width="55.800000000000004"/>
        <Column ss:Width="80.400000000000006"/>
        <Column ss:Width="58.2" ss:Span="5"/>
        <Row>
            <Cell>
                <Data ss:Type="String">Name</Data>
            </Cell>
            <Cell>
                <Data ss:Type="String">Birthday</Data>
            </Cell>
            <Cell>
                <Data ss:Type="String">Favorite Color</Data>
            </Cell>
            <Cell>
                <Data ss:Type="String">Pet Name</Data>
            </Cell>
            <Cell>
                <Data ss:Type="String">Car</Data>
            </Cell>
        </Row>
        <Row>
            <Cell>
                <Data ss:Type="String">Joe</Data>
            </Cell>
            <Cell ss:StyleID="s16">
                <Data ss:Type="DateTime">2017-01-01T00:00:00.000</Data>
            </Cell>
            <Cell>
                <Data ss:Type="String">blue</Data>
            </Cell>
            <Cell>
                <Data ss:Type="String">Bo</Data>
            </Cell>
            <Cell>
                <Data ss:Type="String">Ferrari</Data>
            </Cell>
        </Row>

Any suggestions on making this conversion? Is there a way to create a mapping table to move from one to the other? Am I going to be stuck trying to manually move over all the OpenXML files and manually build the desired XML output? That latter is what I'm really trying to avoid.

Thanks

2

There are 2 best solutions below

2
On BEST ANSWER

You can use EasyXLS library. First read the xlsx file and than convert it to SpreadSheetML.

ExcelDocument workbook = new ExcelDocument();
workbook.easy_LoadXLSXFile("Excel.xlsx");
workbook.easy_WriteXMLFile("SpreadsheetML.xml");

With OpenXML you will have to do it cell-by-cell. EasyXLS supports both file formatting.

Later edit: this will also help you:
Convert Excel to SpreadSheetML

0
On

Try just renaming the .xlsx file to .zip. If that xml format doesn't suit, then the libraries in other replies may.

EDIT: Ignore the conversion warning error.