简体   繁体   English

在Open XML SDK 2.7 C#中将Open XML转换为SpreadsheetML

[英]Converting Open XML to SpreadsheetML in Open XML SDK 2.7 C#

I have an excel workbook with a small table in Sheet 1 like this: 我有一张excel工作簿,在Sheet 1中有一张小桌子,像这样:

在此处输入图片说明

If I open the raw XML via 7zip or something, the Open XML output for the worksheet data looks like this: 如果我通过7zip或其他方式打开原始XML,则工作表数据的Open XML输出如下所示:

   <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. 我一直在使用Open XML SDK今天,并且可以访问人类可读的单个单元格值,等等。

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: 我想做的是找到一种“简单”的方法,将OpenXML工作表数据转换为类似于将文件另存为2003电子表格XML时所得到的东西:

    <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? 我会在尝试手动移动所有OpenXML文件并手动构建所需的XML输出时遇到麻烦吗? That latter is what I'm really trying to avoid. 后者是我真正想要避免的。

Thanks 谢谢

You can use EasyXLS library. 您可以使用EasyXLS库。 First read the xlsx file and than convert it to SpreadSheetML. 首先阅读xlsx文件,然后将其转换为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. 使用OpenXML,您必须逐个单元地进行操作。 EasyXLS supports both file formatting. EasyXLS支持两种文件格式。

Later edit: this will also help you: 以后编辑:这也将帮助您:
Convert Excel to SpreadSheetML 将Excel转换为SpreadSheetML

Try just renaming the .xlsx file to .zip. 尝试仅将.xlsx文件重命名为.zip。 If that xml format doesn't suit, then the libraries in other replies may. 如果该xml格式不适合,则其他回复中的库可能会。

EDIT: Ignore the conversion warning error. 编辑:忽略转换警告错误。

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

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