1. Home
  2.   Open xml sdk
  3.   Openize.OpenXML-SDK for .NET

Openize.OpenXML-SDK for .NET

 
 

C# .NET アプリで Office ドキュメントを操作

わずか数行のコードで、Microsoft Office ドキュメント(Word、Excel、PowerPoint)の作成・読み込み・編集が可能。

Openize.OpenXML-SDK for .NET は、Microsoft Office ドキュメントの作成・カスタマイズを簡単に行うためのオープンソース .NET SDK です。直感的な C# ライブラリで、Word、Excel、PowerPoint のドキュメントを最小限のコードで操作できます。

この軽量なソリューションは簡単にインストールでき、多様なドキュメント処理のニーズに対応可能です。Openize.OpenXML-SDK は、Microsoft が推奨する OpenXML SDK の機能を活用し、ラッパーとして便利に利用できます。

開発者向けに設計されたこのオープンソース .NET ライブラリは、OpenXML SDK を活用してさらに機能を拡張することが可能です。直感的なデザインにより、簡単に扱うことができます。新しい段落の追加、テキストのフォーマット、画像の挿入・サイズ変更、画像の抽出、ドキュメントのプロパティ変更など、さまざまな便利な機能を提供しています。

GitHub リポジトリで貢献、改善提案、機能拡張を行いましょう: https://github.com/openize-com/openize-open-xml-sdk-net

Previous Next

Openize.OpenXML-SDK for .NET の使い方

Openize.OpenXML-SDK for .NET をインストールする推奨方法は、NuGet を使用することです。スムーズにインストールするために、以下のコマンドを使用してください。

NuGet を使用して Openize.OpenXML-SDK for .NET をインストール

NuGet\Install-Package Openize.OpenXML-SDK 
また、GitHub から直接ダウンロードすることもできます。

Word ドキュメントをプログラムで作成する

以下のコードスニペットは、Word ドキュメントをプログラムで作成する方法を示しています。

.NET SDK を使用して Word ドキュメントを作成

 
// Create an instance of the Document class.
Document doc = new Document();

// Invoke the Save method to save the Word document onto the disk.
doc.Save("/Docs.docx");

Word ドキュメントにテキストを追加

以下のコードスニペットは、Word ドキュメントにテキストを追加する方法を示しています。

.NET SDK を使用して Word ドキュメントに段落を追加


// Create an instance of the Document class.
using (Document doc = new Document())
{
    //Initialize the constructor with the Document class object.
    Body body = new Body(doc);
    // Instantiate an instance of the Paragraph class.
    Paragraph para1 = new Paragraph();
    // Set the text of the paragraph.
    para1.AddRun(new Run { Text = "This is a Paragraph." });
    // Invoke AppendChild method of the body class to add a paragraph to the document.
    body.AppendChild(para1);
    // Call the Save method to save the Word document onto the disk.
    doc.Save("/Docs.docx"); 
}

空の Excel スプレッドシート / ワークブックをプログラムで作成

以下のコードスニペットは、C# と FileFormat.Cells ライブラリを使用して Microsoft Excel スプレッドシートを作成・保存する方法を示しています。

  • 最初に FileFormat.Cells ライブラリをインポートし、Excel スプレッドシート操作機能を利用できるようにします。
  • Example 名前空間内で Program クラスを定義します。
  • Main メソッドはプログラムのエントリーポイントとして機能し、コマンドライン引数 (string[] args) を受け取ります。
  • Workbook クラスのインスタンスを `Workbook workbook = new Workbook();` で作成します。
  • Save メソッドを使用して Excel スプレッドシートを保存します。ファイルはルートディレクトリ `/` に "Spreadsheet.xlsx" という名前で保存されます。

以下のコードスニペットをコピーして、メインファイルに貼り付けてプログラムを実行してください。

C# で空のワークブック / スプレッドシートを作成

 
using System;
using Openize.Cells;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize an instance of the Workbook class.
            var workbook = new Openize.Cells.Workbook();

            // Call the Save method to save the MS Excel Spreadsheet/Workbook onto the disk.
            workbook.Save("Z:\\Downloads\\Spreadsheet.xlsx");
            
            Console.WriteLine("Excel spreadsheet created successfully.");
        }
    }
}

Excel ワークシートの指定した行位置に行を挿入

この C# の例では、Openize.Cells ライブラリを使用して Excel ワークシートの特定の行位置に行を挿入する方法を紹介します。

以下のコードスニペットをコピーして、メインファイルに貼り付けてプログラムを実行してください。

C# で Excel シートに行を挿入

 
using System;
using Openize.Cells;

class Program
{
    static void Main(string[] args)
    {
        string filePath = "Z:\\Downloads\\test_spreadsheet.xlsx";

        // Load the workbook from the specified file path
        using (var wb = new Openize.Cells.Workbook(filePath))
        {
            // Access the first worksheet in the workbook
            var firstSheet = wb.Worksheets[0];

            // Define the starting row index and the number of rows to insert
            uint startRowIndex = 5;
            uint numberOfRows = 3;

            // Insert the rows into the worksheet
            firstSheet.InsertRows(startRowIndex, numberOfRows);

            // Get the total row count after insertion
            int rowsCount = firstSheet.GetRowCount();

            // Output the updated row count to the console
            Console.WriteLine("Rows Count=" + rowsCount);

            // Save the workbook to reflect the changes made
            wb.Save(filePath);

            Console.WriteLine("Rows inserted and workbook saved successfully.");
        }
    }
}

PowerPoint プレゼンテーションをプログラムで作成

以下のコードスニペットは、PowerPoint プレゼンテーションをプログラムで作成する方法を示しています。

.NET API を使用して PowerPoint プレゼンテーションを作成

 
// Create an object of the Presentation class.
 Presentation presentation = Presentation.Create("presentation.pptx");

//Perform necessary operations.
//...

// Call the Save method to save the PowerPoint file onto the disk.
presentation.Save();

プレゼンテーションにテキストを挿入

以下のコードスニペットは、PowerPoint プレゼンテーションにテキストをプログラムで挿入する方法を示しています。

.NET API を使用してプレゼンテーションにテキストを挿入

 

// Create a new PowerPoint presentation at the specified file path
Presentation presentation = Presentation.Create("D:\\AsposeSampleResults\\test2.pptx");

// Create a text shape for the title and set its properties
TextShape shape = new TextShape();
shape.Text = "Title: Here is my first title From FF";
shape.TextColor = "980078";
shape.FontFamily = "Baguet Script";

// Create the slide and add the text shape to it
Slide slide = new Slide();
slide.AddTextShapes(shape);

// Append the slide to the presentation
presentation.AppendSlide(slide);

// Save the modified presentation
presentation.Save();


その他のコード例とリソース

より詳細なコード例については、Openize Gists をご覧ください。

 日本