打开不同的Microsoft Excel版本文件
如何打开不同版本的Microsoft Excel文件
应用程序通常需要能够打开由不同版本创建的Microsoft Excel文件,例如Microsoft Excel 95、97或Microsoft Excel 2007/2010/2013/2016/2019和Office 365。您可能需要以多种格式之一加载文件,包括XLS、XLSX、XLSM、XLSB、SpreadsheetML、TabDelimited或TSV、CSV、ODS等。使用构造函数或指定 Workbook 类的 FileFormat 类型属性,该属性使用 FileFormatType 枚举来指定格式。
FileFormatType枚举包含许多预定义的文件格式,其中一些如下所示。
文件格式类型 | 描述 |
---|---|
Csv | 表示CSV文件 |
Excel97To2003 | 表示Excel 97-2003文件 |
Xlsx | 表示Excel 2007/2010/2013/2016/2019和Office 365 XLSX文件 |
Xlsm | 代表Excel 2007/2010/2013/2016/2019和Office 365的XLSM文件 |
Xltx | 代表Excel 2007/2010/2013/2016/2019和Office 365模板XLTX文件 |
Xltm | 代表Excel 2007/2010/2013/2016/2019和Office 365宏启用的XLTM文件 |
Xlsb | 代表Excel 2007/2010/2013/2016/2019和Office 365二进制XLSB文件 |
SpreadsheetML | 代表SpreadsheetML文件 |
Tsv | 代表分隔值文件 |
TabDelimited | 代表分隔符文本文件 |
Ods | 代表ODS文件 |
Html | 代表HTML文件 |
Mhtml | 代表MHTML文件 |
打开Microsoft Excel 95/5.0文件
要打开Microsoft Excel 95/5.0文件,请使用 LoadOptions 并为要加载的 LoadOptions 类设置相关属性。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Get the Excel file into stream | |
using (FileStream stream = new FileStream(dataDir + "Excel95_5.0.xls", FileMode.Open)) | |
{ | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions1 = new LoadOptions(LoadFormat.Excel97To2003); | |
// Create a Workbook object and opening the file from the stream | |
Workbook wbExcel95 = new Workbook(stream, loadOptions1); | |
Console.WriteLine("Microsoft Excel 95/5.0 workbook opened successfully!"); | |
} |
打开Microsoft Excel 97 - 2003文件
要打开Microsoft Excel 97 - 2003文件,请使用 LoadOptions 并为要加载的 LoadOptions 类设置相关属性。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Get the Excel file into stream | |
using (FileStream stream = new FileStream(dataDir + "Book_Excel97_2003.xls", FileMode.Open)) | |
{ | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions1 = new LoadOptions(LoadFormat.Excel97To2003); | |
// Create a Workbook object and opening the file from the stream | |
Workbook wbExcel97 = new Workbook(stream, loadOptions1); | |
Console.WriteLine("Microsoft Excel 97 - 2003 workbook opened successfully!"); | |
} |
打开Microsoft Excel 2007/2010/2013/2016/2019和Office 365 XLSX文件
要打开Microsoft Excel 2007/2010/2013/2016/2019和Office 365格式,即XLSX或XLSB,请指定文件路径。也可以使用 LoadOptions 并设置 LoadOptions 类的相关属性/选项来加载要加载的模板文件。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening Microsoft Excel 2007 Xlsx Files | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions2 = new LoadOptions(LoadFormat.Xlsx); | |
// Create a Workbook object and opening the file from its path | |
Workbook wbExcel2007 = new Workbook(dataDir + "Book_Excel2007.xlsx", loadOptions2); | |
Console.WriteLine("Microsoft Excel 2007 - Office365 workbook opened successfully!"); |
打开加密的Excel文件
可以使用Microsoft Excel创建加密的Excel文件。要打开加密文件,请使用 LoadOptions 并为要加载的模板文件设置其属性和选项(例如,给定密码)。 您可以从以下链接下载测试此功能的示例文件:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate LoadOptions | |
LoadOptions loadOptions6 = new LoadOptions(); | |
// Specify the password | |
loadOptions6.Password = "1234"; | |
// Create a Workbook object and opening the file from its path | |
Workbook wbEncrypted = new Workbook(dataDir + "encryptedBook.xls", loadOptions6); | |
Console.WriteLine("Encrypted excel file opened successfully!"); |
Aspose.Cells还支持打开受密码保护的Microsoft Excel 2007、2010、2013、2016、2019、Office 365文件。