打开不同格式的文件
开发人员使用 Aspose.Cells 打开文件用于不同目的。例如,打开文件以从中检索数据,或使用预定义的设计器电子表格文件来加快开发过程。 Aspose.Cells 允许开发人员打开不同种类的源文件。这些源文件可以是 Microsoft Excel 报告、SpreadsheetML、逗号分隔值 (CSV)、制表符分隔或制表符分隔值 (TSV) 文件。本文讨论使用 Aspose.Cells 打开这些不同的源文件。
如果您需要了解所有支持的文件格式,请参阅以下页面: 支持的文件格式
打开 Excel 文件的简单方法
通过路径打开
要使用文件路径打开 Microsoft Excel 文件,请在创建实例时将文件路径作为参数传递**工作簿**班级。以下示例代码演示了使用文件路径打开 Excel 文件。
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningFilesThroughPath.class) + "files/"; | |
// Opening from path. | |
// Creating an Workbook object with an Excel file path | |
Workbook workbook1 = new Workbook(dataDir + "Book1.xlsx"); | |
// Print message | |
System.out.println("Workbook opened using path successfully."); |
通过流打开
有时,您要打开的 Excel 文件存储为流。在这种情况下,类似于使用文件路径打开文件,在实例化时将流作为参数传递**工作簿**班级。以下示例代码演示了使用流打开 Excel 文件。
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningFilesThroughStream.class) + "loading_saving/"; | |
// Opening workbook from stream | |
// Create a Stream object | |
FileInputStream fstream = new FileInputStream(dataDir + "Book2.xls"); | |
// Creating an Workbook object with the stream object | |
Workbook workbook2 = new Workbook(fstream); | |
fstream.close(); | |
// Print message | |
System.out.println("Workbook opened using stream successfully."); |
打开不同 Microsoft Excel 版本的文件
用户可以使用**加载选项**类来指定 Excel 文件的格式,使用**加载格式**枚举。
这**加载格式**枚举包含许多预定义的文件格式,其中一些在下面给出。
格式类型 | 描述 |
---|---|
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 文件 |
Xlx | 表示 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文件 |
HTML | 代表一个HTML文件 |
MHTML | 代表一个MHTML文件 |
打开 Microsoft Excel 95/5.0 文件
要打开 Microsoft Excel 95 文件,请实例化**工作簿**带有模板文件路径或流的实例。可以从以下链接下载用于测试代码的示例文件:
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Opening Microsoft Excel 97 Files | |
// Creating an EXCEL_97_TO_2003 LoadOptions object | |
// Creating an Workbook object with excel 97 file path and the | |
// loadOptions object | |
new Workbook(srcDir + "Excel95_5.0.xls"); |
打开 Microsoft Excel 97 或更高版本 XLS 文件
要打开Microsoft Excel XLS 97或更高版本的XLS个文件,实例化**工作簿**带有模板文件路径或流的实例。或者使用**加载选项**方法并选择**EXCEL_97_TO_2003**中的价值**加载格式**枚举。
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningMicrosoftExcel972003Files.class) + "loading_saving/"; | |
// Opening Microsoft Excel 97 Files | |
// Createing and EXCEL_97_TO_2003 LoadOptions object | |
LoadOptions loadOptions1 = new LoadOptions(LoadFormat.EXCEL_97_TO_2003); | |
// Creating an Workbook object with excel 97 file path and the | |
// loadOptions object | |
Workbook workbook3 = new Workbook(dataDir + "Book_Excel97_2003.xls", loadOptions1); | |
// Print message | |
System.out.println("Excel 97 Workbook opened successfully."); |
打开 Microsoft Excel 2007 或更高版本 XLSX 文件
要打开 Microsoft Excel 2007 或更高版本的 XLSX 文件,请实例化**工作簿**带有模板文件路径或流的实例。或者使用**加载选项**类并选择**XLSX**中的价值**加载格式**枚举。
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningMicrosoftExcel2007XlsxFiles.class) + "loading_saving/"; | |
// Opening Microsoft Excel 2007 XLSX Files. Createing and XLSX LoadOptions object | |
LoadOptions loadOptions2 = new LoadOptions(LoadFormat.XLSX); | |
// Creating an Workbook object with 2007 xlsx file path and the loadOptions object | |
Workbook workbook4 = new Workbook(dataDir + "Book_Excel2007.xlsx", loadOptions2); | |
// Print message | |
System.out.println("Excel 2007 Workbook opened successfully."); |
打开不同格式的文件
Aspose.Cells 允许开发人员打开不同格式的电子表格文件,例如 SpreadsheetML、CSV、制表符分隔文件。要打开此类文件,开发人员可以使用与打开不同 Microsoft Excel 版本的文件相同的方法。
打开 SpreadsheetML 文件
SpreadsheetML 文件是电子表格的 XML 表示形式,包括有关电子表格的所有信息,例如格式、公式等。自 Microsoft Excel XP 起,向 Microsoft Excel 添加了一个 XML 导出选项,可将电子表格导出到 SpreadsheetML 文件。
要打开 SpreadsheetML 文件,请使用**加载选项**类并选择**电子表格_ML**中的价值**加载格式**枚举。
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningSpreadsheetMLFiles.class) + "loading_saving/"; | |
// Opening SpreadsheetML Files | |
// Creating and EXCEL_2003_XML LoadOptions object | |
LoadOptions loadOptions3 = new LoadOptions(LoadFormat.SPREADSHEET_ML); | |
// Creating an Workbook object with SpreadsheetML file path and the | |
// loadOptions object | |
Workbook workbook5 = new Workbook(dataDir + "Book3.xml", loadOptions3); | |
// Print message | |
System.out.println("SpreadSheetML format workbook has been opened successfully."); |
打开 CSV 文件
逗号分隔值 (CSV) 文件包含其值由逗号分隔或分隔的记录。在 CSV 文件中,数据以表格格式存储,其字段由逗号分隔并由双引号引起来。如果字段的值包含双引号字符,则使用一对双引号字符进行转义。您还可以使用 Microsoft Excel 将电子表格数据导出到 CSV 文件。
要打开 CSV 文件,请使用**加载选项**类并选择**CSV**值,预定义在**加载格式**枚举。
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningCSVFiles.class) + "loading_saving/"; | |
// Opening CSV Files | |
// Creating and CSV LoadOptions object | |
LoadOptions loadOptions4 = new LoadOptions(LoadFormat.CSV); | |
// Creating an Workbook object with CSV file path and the loadOptions | |
// object | |
Workbook workbook6 = new Workbook(dataDir + "Book_CSV.csv", loadOptions4); | |
// Print message | |
System.out.println("CSV format workbook has been opened successfully."); |
打开 CSV 文件并替换无效字符
在 Excel 中,打开带有特殊字符的 CSV 文件时,会自动替换这些字符。 Aspose.Cells API 完成了同样的操作,在下面给出的代码示例中进行了演示。
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
//Source directory | |
String dataDir = Utils.getSharedDataDir(OpeningCSVFilesAndReplacingInvalidCharacters.class) + "LoadingSavingConvertingAndManaging/"; | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.CSV); | |
//Load CSV file | |
Workbook workbook = new Workbook(dataDir + "[20180220142533][ASPOSE_CELLS_TEST].csv", loadOptions); | |
System.out.println(workbook.getWorksheets().get(0).getName()); // (20180220142533)(ASPOSE_CELLS_T | |
System.out.println(workbook.getWorksheets().get(0).getName().length()); // 31 | |
System.out.println("CSV file opened successfully!"); |
使用首选解析器打开 CSV 文件
这并不总是需要使用默认解析器设置来打开 CSV 文件。有时导入 CSV 文件不会创建预期的输出,例如日期格式不符合预期或空字段的处理方式不同。以此目的**TxtLoadOptions.PreferredParsers**可根据需要提供自己的首选解析器来解析不同的数据类型。以下示例代码演示了首选解析器的用法。
可以从以下链接下载示例源文件和输出文件以测试此功能。
outputsamplePreferredParser.xlsx
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
class TextParser implements ICustomParser | |
{ | |
@Override | |
public Object parseObject(String s) { | |
return s; | |
} | |
@Override | |
public String getFormat() { | |
return ""; | |
} | |
} | |
class DateParser implements ICustomParser { | |
@Override | |
public Object parseObject(String s) { | |
Date myDate = null; | |
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); | |
try { | |
myDate = formatter.parse(s); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
return myDate; | |
} | |
@Override | |
public String getFormat() { | |
return "dd/MM/yyyy"; | |
} | |
} | |
public class OpeningCSVFilesWithPreferredParser { | |
//Source directory | |
private static String sourceDir = Utils.Get_SourceDirectory(); | |
private static String outputDir = Utils.Get_OutputDirectory(); | |
public static void main(String[] args) throws Exception { | |
// Initialize Text File's Load options | |
TxtLoadOptions oTxtLoadOptions = new TxtLoadOptions(LoadFormat.CSV); | |
// Specify the separatot character | |
oTxtLoadOptions.setSeparator(','); | |
// Specify the encoding scheme | |
oTxtLoadOptions.setEncoding(Encoding.getUTF8()); | |
// Set the flag to true for converting datetime data | |
oTxtLoadOptions.setConvertDateTimeData(true); | |
// Set the preferred parsers | |
oTxtLoadOptions.setPreferredParsers(new ICustomParser[] { new TextParser(), new DateParser() }); | |
// Initialize the workbook object by passing CSV file and text load options | |
Workbook oExcelWorkBook = new Workbook(sourceDir + "samplePreferredParser.csv", oTxtLoadOptions); | |
// Get the first cell | |
Cell oCell = oExcelWorkBook.getWorksheets().get(0).getCells().get("A1"); | |
// Display type of value | |
System.out.println("A1: " + getCellType(oCell.getType()) + " - " + oCell.getDisplayStringValue()); | |
// Get the second cell | |
oCell = oExcelWorkBook.getWorksheets().get(0).getCells().get("B1"); | |
// Display type of value | |
System.out.println("B1: " + getCellType(oCell.getType()) + " - " + oCell.getDisplayStringValue()); | |
// Save the workbook to disc | |
oExcelWorkBook.save(outputDir + "outputsamplePreferredParser.xlsx"); | |
System.out.println("OpeningCSVFilesWithPreferredParser executed successfully.\r\n"); | |
} | |
private static String getCellType(int type){ | |
if(type == CellValueType.IS_STRING){ | |
return "String"; | |
} else if(type == CellValueType.IS_NUMERIC){ | |
return "Numeric"; | |
} else if(type == CellValueType.IS_BOOL){ | |
return "Bool"; | |
} else if(type == CellValueType.IS_DATE_TIME){ | |
return "Date"; | |
} else if(type == CellValueType.IS_NULL){ | |
return "Null"; | |
} else if(type == CellValueType.IS_ERROR){ | |
return "Error"; | |
} else{ | |
return "Unknown"; | |
} | |
} |
打开 TSV(制表符分隔)文件
制表符分隔的文件包含电子表格数据,但没有任何格式。数据按行和列排列,例如表格和电子表格。简而言之,制表符分隔文件是一种特殊的纯文本文件,文本中的每一列之间都有一个制表符。
要打开制表符分隔的文件,开发人员应该使用**加载选项**类并选择**TSV**值,预定义在**加载格式**枚举。
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningTabDelimitedFiles.class) + "loading_saving/"; | |
// Creating and TAB_DELIMITED LoadOptions object | |
LoadOptions loadOptions5 = new LoadOptions(LoadFormat.TSV); | |
// Creating an Workbook object with Tab Delimited text file path and the | |
// loadOptions object | |
Workbook workbook7 = new Workbook(dataDir + "Book1TabDelimited.txt", loadOptions5); | |
// Print message | |
System.out.println("Tab Delimited workbook has been opened successfully."); |
打开加密的 Excel 文件
我们知道可以使用 Microsoft Excel 创建加密的 Excel 文件。要打开此类加密文件,开发人员应调用特殊的重载 LoadOptions 方法并选择 FileFormatType 枚举中预定义的 DEFAULT 值。此方法还将采用加密文件的密码,如下例所示。
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(OpeningEncryptedExcelFiles.class) + "loading_saving/"; | |
// Opening Encrypted Excel Files | |
// Creating and EXCEL_97_TO_2003 LoadOptions object | |
LoadOptions loadOptions6 = new LoadOptions(LoadFormat.EXCEL_97_TO_2003); | |
// Setting the password for the encrypted Excel file | |
loadOptions6.setPassword("1234"); | |
// Creating an Workbook object with file path and the loadOptions object | |
Workbook workbook8 = new Workbook(dataDir + "encryptedBook.xls", loadOptions6); | |
// Print message | |
System.out.println("Encrypted workbook has been opened successfully."); |
Aspose.Cells 还支持打开受密码保护的 MS Excel 2013 文件。
打开 SXC 文件
StarSuite Calc 类似于 Microsoft Excel,支持公式、图表、函数和宏。使用此软件创建的电子表格以 SXC 扩展名保存。 SXC 文件也用于 OpenOffice.org Calc 电子表格文件。 Aspose.Cells 可以读取 SXC 文件,如以下代码示例所示。
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the source directory. | |
String sourceDir = Utils.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.SXC); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleSXC.sxc", loadOptions); | |
// Using the Sheet 1 in Workbook | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Accessing a cell using its name | |
Cell cell = worksheet.getCells().get("C3"); | |
System.out.println("Cell Name: " + cell.getName() + " Value: " + cell.getStringValue()); |
打开 FODS 文件
FODS 文件是以 OpenDocument XML 格式保存的电子表格,没有任何压缩。 Aspose.Cells 可以读取 FODS 文件,如以下代码示例所示。
例子
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the source directory. | |
String sourceDir = Utils.Get_SourceDirectory(); | |
// Instantiate LoadOptions specified by the LoadFormat. | |
LoadOptions loadOptions = new LoadOptions(LoadFormat.FODS); | |
// Create a Workbook object and opening the file from its path | |
Workbook workbook = new Workbook(sourceDir + "SampleFods.fods", loadOptions); | |
// Print message | |
System.out.println("FODS file opened successfully!"); |