สร้างตาราง Excel ใน C#

คู่มือนี้จะแชร์รายละเอียดสำหรับการสร้างตาราง Excel ใน C# บทความนี้มีรายละเอียดในการตั้งค่า IDE สำหรับการพัฒนา รายการงานการเขียนโปรแกรม และโค้ดตัวอย่างสำหรับการสร้าง ตารางใน Microsoft Excel ใน C# คุณจะได้เรียนรู้ตัวเลือกต่างๆ ในการปรับแต่งตารางและบันทึกเอาต์พุตในรูปแบบต่างๆ

ขั้นตอนในการสร้างตาราง Excel ใน C#

  1. ตั้งค่า IDE เพื่อใช้ Aspose.Cells for .NET เพื่อสร้างตาราง
  2. สร้างหรือโหลดไฟล์ Excel และเข้าถึง sheet ที่มีข้อมูล
  3. สร้างวัตถุรายการโดยระบุช่วงของเซลล์พร้อมข้อมูล
  4. ตั้งค่า TableStyleType ของวัตถุรายการเป็น TableStyleMedium10
  5. ตั้งค่าสถานะผลรวมเป็นจริงสำหรับคอลัมน์ตัวเลขทั้งหมด
  6. เพิ่มการคำนวณรวมสำหรับคอลัมน์ที่สอง
  7. บันทึกผลลัพธ์

ขั้นตอนข้างต้นอธิบายกระบวนการสร้าง ตาราง Excel แบบง่ายใน C# เริ่มต้นกระบวนการโดยการโหลดหรือสร้างไฟล์ Excel เข้าถึงชีต และเพิ่มออบเจ็กต์รายการที่มีช่วงของเซลล์ที่มีข้อมูล ใช้ออบเจ็กต์รายการใหม่ที่อ้างอิงถึงตารางเพื่อตั้งค่า TableStyleType การตั้งค่าสถานะเพื่อแสดงผลรวมและประเภทการคำนวณนับ

รหัสสำหรับการสร้างตาราง Microsoft Excel ใน C#

using System;
using System.Linq;
using Aspose.Cells;
using Aspose.Cells.Tables;
class Program
{
static void Main(string[] args) // Table creation in C#
{
new License().SetLicense("License.lic");
// Create a workbook.
Workbook wb = new Workbook();
// Optionally call this function if the workbook has no data
CreateSampleData(ref wb);
// Obtain the first sheet
Worksheet sheet = wb.Worksheets[0];
// Add a new list object with 20 rows and 5 columns
ListObject listObject = sheet.ListObjects[sheet.ListObjects.Add("A1", "E20", true)];
// Set table style
listObject.TableStyleType = TableStyleType.TableStyleMedium10;
// Show the flag to display the Total for all numbers
listObject.ShowTotals = true;
// Set the second column calculation type
listObject.ListColumns[1].TotalsCalculation = TotalsCalculation.Count;
// Saving the Excel file
wb.Save("output.xlsx");
Console.WriteLine("Table created successfully");
}
static void CreateSampleData(ref Workbook wb)
{
// Fill workbook with some dummy data
string[] titles = new string[] {"Employee", "Quarter", "Product", "Country","Sale"};
string[] employees = new string[] {"David", "James","Miya" };
string[] products = new string[] { "Chai", "Chang", "Geitost", "Maxilaku" };
string[] countries = new string[] { "Brazil", "China", "France", "Germany", "India", "Italy" };
foreach (var (item, idx) in titles.Select((value, index) => (value, index)))
wb.Worksheets[0].Cells[0, idx].Value = item;
Random random = new Random();
for(int i = 1; i < 20; i++)
{
wb.Worksheets[0].Cells[i, 0].Value = employees[random.Next() % employees.Count()];
wb.Worksheets[0].Cells[i, 1].Value = (random.Next() % 4) + 1;
wb.Worksheets[0].Cells[i, 2].Value = products[random.Next() % products.Count()];
wb.Worksheets[0].Cells[i, 3].Value = countries[random.Next() % countries.Count()];
wb.Worksheets[0].Cells[i, 4].Value = random.Next() % 2000;
}
}
}

โค้ดตัวอย่างนี้สาธิตกระบวนการสร้างตาราง MS Excel ใน C# คลาสแผ่นงานประกอบด้วยวัตถุ ListObjects สำหรับการเพิ่มตาราง TableStyleType มีสไตล์ตารางเริ่มต้นทั้งหมดที่รองรับโดย MS Excel ที่คุณอาจใช้ในขณะที่สร้างตาราง

บทความนี้ได้สอนเราถึงวิธีการทำงานกับตาราง Excel ขั้นสูงใน C# หากต้องการแทรกตัวแบ่งส่วนข้อมูลในไฟล์ Excel โปรดดูบทความเกี่ยวกับ วิธีแทรกตัวแบ่งส่วนข้อมูลใน Excel โดยใช้ C#

 ไทย