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 是一款易於使用的開源 .NET SDK,旨在幫助開發者輕鬆建立和自訂 Microsoft Office 文件。這款直覺式 C# 函式庫允許開發者使用最少的程式碼行數來生成和修改 Word、Excel 和 PowerPoint 文件。

此輕量級解決方案易於安裝,並提供多種功能以滿足不同的文件需求。Openize.OpenXML-SDK 利用 Microsoft 支援的 OpenXML SDK 技術,作為一個便利的封裝工具,使開發者能夠更輕鬆地使用 OpenXML 的進階功能。

這款開源 .NET 函式庫專為開發者設計,可透過 OpenXML SDK 擴展功能。其使用者友善的設計使開發者能夠輕鬆執行如新增段落、文字格式化、插入及調整圖片大小、擷取圖片、修改文件屬性等智慧化操作。

歡迎訪問我們的 GitHub 儲存庫,為這款開源 SDK 貢獻程式碼、提供改進建議,並參與開發:https://github.com/openize-com/openize-open-xml-sdk-net

Previous Next

開始使用 Openize.OpenXML-SDK for .NET

推薦使用 NuGet 安裝 Openize.OpenXML-SDK for .NET。請使用以下命令進行順利安裝。

透過 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 方法為程式的進入點,可接受命令列參數(如果有的話)。
  • 使用 `Workbook workbook = new Workbook();` 初始化 Workbook 類別的實例。
  • 調用 Save 方法儲存 Excel 試算表,檔案預設儲存於磁碟根目錄(“/”)下,命名為 "Spreadsheet.xlsx"。

複製並貼上以下程式碼到您的主檔案中,然後執行程式。

使用 C# 建立空的 Excel 試算表/活頁簿

 
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 函式庫,在指定的行索引處插入行。

複製並貼上以下程式碼到您的主檔案中,然後執行程式。

使用 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 簡報中插入文字

以下程式碼範例展示如何使用程式碼在 PowerPoint 簡報中插入文字。

透過 .NET API 在 PowerPoint 簡報中插入文字

 

// 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 以獲取更多詳細的程式碼範例。

 中國傳統的