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

Openize.OpenXML-SDK for .NET

 
 

จัดการเอกสาร Office ในแอป C# .NET

สร้าง โหลด และแก้ไขเนื้อหาของเอกสาร Microsoft Office เช่น Word, Excel และ PowerPoint ได้อย่างง่ายดายเพียงไม่กี่บรรทัดของโค้ด

Openize.OpenXML-SDK for .NET เป็น .NET SDK แบบโอเพนซอร์สที่ใช้งานง่าย ออกแบบมาเพื่อการสร้างและแก้ไขเอกสาร Microsoft Office ได้อย่างมีประสิทธิภาพ ไลบรารี C# ที่ใช้งานง่ายนี้ช่วยให้สามารถสร้างและปรับแต่งเอกสาร Word, Excel และ PowerPoint ได้โดยใช้โค้ดเพียงไม่กี่บรรทัด

การติดตั้งโซลูชันที่มีน้ำหนักเบานี้เป็นไปอย่างราบรื่น พร้อมฟีเจอร์มากมายที่รองรับความต้องการด้านการประมวลผลเอกสาร Openize.OpenXML-SDK ใช้ประโยชน์จาก OpenXML SDK ซึ่งได้รับการสนับสนุนโดย Microsoft โดยทำหน้าที่เป็นตัวช่วยที่สะดวกในการใช้ฟีเจอร์ขั้นสูงของ OpenXML

ออกแบบมาเพื่อให้นักพัฒนาสามารถขยายฟังก์ชันการทำงานโดยอาศัย OpenXML SDK ไลบรารี .NET แบบโอเพนซอร์สนี้ใช้งานได้ง่ายและมาพร้อมฟีเจอร์อัจฉริยะมากมาย เช่น การเพิ่มย่อหน้าใหม่ การกำหนดรูปแบบข้อความ การแทรกและปรับขนาดรูปภาพ การดึงรูปภาพ การเปลี่ยนแปลงคุณสมบัติของเอกสาร และอื่น ๆ อีกมากมาย

เยี่ยมชมที่เก็บ GitHub ของเราเพื่อมีส่วนร่วม เสนอการปรับปรุง และช่วยพัฒนา SDK โอเพนซอร์สนี้: https://github.com/openize-com/openize-open-xml-sdk-net

Previous Next

เริ่มต้นใช้งาน Openize.OpenXML-SDK สำหรับ .NET

วิธีที่แนะนำในการติดตั้ง Openize.OpenXML-SDK สำหรับ .NET คือการใช้ NuGet โปรดใช้คำสั่งต่อไปนี้เพื่อการติดตั้งที่ราบรื่น

ติดตั้ง Openize.OpenXML-SDK สำหรับ .NET ผ่าน NuGet

NuGet\Install-Package Openize.OpenXML-SDK 
คุณยังสามารถดาวน์โหลดได้โดยตรงจาก GitHub

สร้างเอกสาร Word ด้วยโค้ด

ตัวอย่างโค้ดต่อไปนี้จะแสดงการสร้างเอกสาร Word เปล่าโดยใช้โค้ด

สร้างเอกสาร Word ผ่าน .NET SDK

 
// 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


// 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/Workbook เปล่าด้วยโค้ด

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างและบันทึกไฟล์ Microsoft Excel ใหม่โดยใช้ C# และไลบรารี FileFormat.Cells

  • เริ่มต้นด้วยการนำเข้าไลบรารี FileFormat.Cells เพื่อให้สามารถใช้ฟังก์ชันจัดการไฟล์ Excel ได้
  • สร้างคลาส Program ภายใต้ namespace Example
  • เมธอด Main เป็นจุดเริ่มต้นของโปรแกรมและสามารถรับอาร์กิวเมนต์คำสั่ง (string[] args) ได้
  • สร้างอินสแตนซ์ของคลาส Workbook โดยใช้ `Workbook workbook = new Workbook();`
  • ใช้เมธอด Save เพื่อบันทึกไฟล์ Excel โดยจะถูกบันทึกเป็น "Spreadsheet.xlsx" ในไดเรกทอรีราก `/`

คัดลอกและวางโค้ดด้านล่างลงในไฟล์หลักของคุณแล้วรันโปรแกรม

สร้าง Workbook/Spreadsheet เปล่าด้วย 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# นี้แสดงวิธีเพิ่มแถวในไฟล์ Excel ที่ตำแหน่งที่กำหนด โดยใช้ไลบรารี Openize.Cells

คัดลอกและวางโค้ดด้านล่างลงในไฟล์หลักของคุณแล้วรันโปรแกรม

เพิ่มแถวในไฟล์ Excel ด้วย C#

 
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 เปล่าโดยใช้โค้ด

สร้างไฟล์นำเสนอ PowerPoint ผ่าน .NET API

 
// 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 โดยใช้โค้ด

เพิ่มข้อความใน 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

 Thai