【问题标题】:Converting Excel XML-Spreadsheet into xlsx将 Excel XML 电子表格转换为 xlsx
【发布时间】:2017-05-28 12:36:46
【问题描述】:

我有一个 XML-Excel 文件(SpreadsheetML 格式):

<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-    microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
 <Company>My Company</Company>
 </DocumentProperties>
 <Styles>
 <Style ss:ID="Default" ss:Name="Normal">
 <Alignment ss:Vertical="Bottom"/>
 <Borders/>
 <Font/>
 <Interior/>
 <NumberFormat/>
 <Protection/>
 </Style>
 <Style ss:ID="stTxt">
<Font ss:FontName="Arial" ss:Size="8"/>
<Alignment ss:Horizontal="Left"/>
<NumberFormat ss:Format="@"/>
 </Style>
<Style ss:ID="stTopHeadline">
<Font ss:FontName="Arial" ss:Size="8" />
<Alignment ss:Horizontal="Left"/>
<NumberFormat ss:Format="@"/>
 </Style>
...

现在我需要使用 C# 将这些文件转换为 XLSX 文件。有没有办法使用 Open Xml 或其他库将其转换为 Excel 2010 XLSX 格式?

【问题讨论】:

    标签: c# xml excel openxml spreadsheetml


    【解决方案1】:

    你得到的是所谓的平面 OPC 格式。 Here 是一篇关于将其转换为常规 Open XML 格式的文章。

    【讨论】:

    • 在 Flat OPC 中,您得到如下标签:&lt;pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"&gt; SpreadsheetML 不同:link
    • @Saftpresse99 SpreadsheetML 包含在包内。
    • 文中示例代码无法将SpreadsheetML转成包。因为命名空间和属性不同。
    【解决方案2】:

    这是一个简单的示例,如何使用 Python 和已安装的 Excel 进行转换。 前提条件:必须在电脑上安装 Microsoft Excel!

    import os
    from win32com.client import Dispatch
    
    def convert_xls_to_xlsx(oldName:str, newName:str):
        oldName = os.path.abspath(oldName)
        newName = os.path.abspath(newName)
        xlApp = Dispatch("Excel.Application")
        wb = xlApp.Workbooks.Open(oldName)
        wb.SaveAs(newName,51)
        wb.Close(True)
    

    【讨论】:

    • 导入模块pywin32
    猜你喜欢
    • 2022-07-19
    • 2021-05-27
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2013-05-04
    • 2011-07-21
    相关资源
    最近更新 更多