将Excel XML-Spreadsheet转换为xlsx

时间:2017-01-13 07:36:14

标签: c# xml excel openxml spreadsheetml

我有一个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格式?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这是一个简单的示例,说明如何使用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)
相关问题