異なる形式のファイルを開く

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 バージョンのファイルを開く

ユーザーは**LoadOptions**クラスを使用して、Excel ファイルの形式を指定します。**LoadFormat**列挙。

の**LoadFormat**列挙には、事前に定義された多くのファイル形式が含まれており、その一部を以下に示します。

フォーマットの種類 説明
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 ファイルを表します
HTML HTML ファイルを表します
Mhtml MHTML ファイルを表します

Microsoft Excel 95/5.0 ファイルを開く

Microsoft Excel 95 ファイルを開くには、**ワークブック**テンプレート ファイルのパスまたはストリームを持つインスタンス。コードをテストするためのサンプル ファイルは、次のリンクからダウンロードできます。

Excel95_5.0.xls

// 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 ファイルを開くには、**ワークブックテンプレート ファイルのパスまたはストリームを持つインスタンス。または、LoadOptionsメソッドを選択し、EXCEL_97_TO_2003の値LoadFormat**列挙。

// 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 ファイルを開くには、**ワークブックテンプレート ファイルのパスまたはストリームを持つインスタンス。または、LoadOptionsクラスを選択し、XLSXの値LoadFormat**列挙。

// 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 表現です。

SpreadsheetML ファイルを開くには、**LoadOptionsクラスを選択し、SPREADSHEET_MLの値LoadFormat**列挙。

// 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 ファイルを開くには、**LoadOptionsクラスを選択し、CSVで定義済みの値LoadFormat**列挙。

// 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**要件に応じてさまざまなデータ型を解析するための独自の優先パーサーを提供することができます。次のサンプル コードは、優先パーサーの使用方法を示しています。

この機能をテストするためのサンプル ソース ファイルと出力ファイルは、次のリンクからダウンロードできます。

samplePreferredParser.csv

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(タブ区切り)ファイルを開く

タブ区切りファイルにはスプレッドシート データが含まれますが、書式はありません。データは、テーブルやスプレッドシートなどの行と列に配置されます。簡単に言うと、タブ区切りファイルは、テキストの各列の間にタブがある特別な種類のプレーン テキスト ファイルです。

タブ区切りファイルを開くには、開発者は**LoadOptions**クラスを選択し、**TSV**で定義済みの値**LoadFormat**列挙。

// 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 ファイルを開く

StarOffice 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!");

先行トピック