バージョン

Excel テンプレート フォーマットでファイルを保存および読み込み

Infragistics Excel ライブラリは、テンプレート ファイルを定義できる 3 つのファイル形式があります。テンプレートの形式には、Excel テンプレート フォーマット(拡張子は XLTX)、Excel マクロに対応したテンプレート フォーマット(拡張子は XLTM)、および Excel 97-2003 テンプレート フォーマット(拡張子は XLT)があり、これらの形式で保存および読み込みが可能です。

WorkbookFormat 列挙体は、XLTX、XLTM、および XLT に対応する値 Excel2007Template、Excel2007MacroEnabledTemplate、および Excel97To2003Template を含みます。CurrentFormat プロパティは、ファイルの現在の形式を取得するために使用できます。拡張子が不明なファイルを読み込む場合、ファイル コンテンツは適切な形式を動的に決定するために解析されます。

以下のコードは、Excel ファイルを Save メソッドを使用して Excel2007Template 形式で保存します。

Visual Basic の場合:

' Create a workbook and set its format to Excel2007Template 
Dim newWorkBook As Infragistics.Documents.Excel.Workbook = New Workbook(WorkbookFormat.Excel2007Template)
' Add a worksheet to the workbook 
Dim worksheet1 As Infragistics.Documents.Excel.Worksheet = newWorkBook.Worksheets.Add("Sheet1")
' Format a cell in the worksheet 
worksheet1.Rows(1).Cells(1).CellFormat.FillPatternBackgroundColor = Color.Red 
' Save the workbook 
newWorkBook.Save("C:ExcelBookTemplate.xltx")

C# の場合:

// Create a workbook and set its format to Excel2007Template
Infragistics.Documents.Excel.Workbook newWorkBook = new Workbook(WorkbookFormat.Excel2007Template);
// Add a worksheet to the workbook
Infragistics.Documents.Excel.Worksheet worksheet1 = newWorkBook.Worksheets.Add("Sheet1");
// Format a cell in the worksheet
worksheet1.Rows[1].Cells[1].CellFormat.FillPatternBackgroundColor = Color.Red;
// Save the workbook
newWorkBook.Save("C:ExcelBookTemplate.xltx");

以下のコードは、SetCurrentFormat メソッドを使用して既存の Excel ファイルの形式を変更します。

Visual Basic の場合:

' Load an Existing Excel file 
Dim workbook As Infragistics.Documents.Excel.Workbook = Infragistics.Documents.Excel.Workbook.Load("C:Book1.xlsx")
' Change the format to Excel2007Template 
workbook.SetCurrentFormat(WorkbookFormat.Excel2007Template) 
' Save the file in the modified format 
workbook.Save("C:ModifiedBook1.xltx")

C# の場合:

// Load an Existing Excel file
Infragistics.Documents.Excel.Workbook workbook = Infragistics.Documents.Excel.Workbook.Load("C:Book1.xlsx");
// Change the format to Excel2007Template
workbook.SetCurrentFormat(WorkbookFormat.Excel2007Template);
// Save the file in the modified format
workbook.Save("C:ModifiedBook1.xltx");

オンラインで表示: GitHub