วิธีสร้าง Pivot Table ใน Excel โดยใช้ C#

คำแนะนำแบบฝึกหัดสั้น ๆ นี้เกี่ยวกับ วิธีสร้าง pivot table ใน Excel โดยใช้ C# หากต้องการ สร้างตารางเดือยด้วยการเขียนโปรแกรม C# รหัสและคำอธิบายโดยละเอียดมีให้ในตอนท้าย คุณจะได้รับไฟล์ XLSX (หรือ XLS) ที่มีตารางเดือยในนั้นโดยไม่ต้องใช้บุคคลที่สามอื่น เครื่องมือปาร์ตี้ นอกจากนี้ยังมีขั้นตอนที่เป็นแนวทางในการเพิ่มเขตข้อมูลต่างๆ ลงในพื้นที่ต่างๆ ในตาราง Pivot

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

  1. สร้างสภาพแวดล้อมเพื่อเพิ่ม Aspose.Cells for .NET จาก NuGet package manager สำหรับสร้าง pivot table
  2. สร้างหรือโหลด workbook ที่มีอยู่ซึ่งมีข้อมูลสำหรับตารางเดือย
  3. เข้าถึงเป้าหมาย worksheet ที่ต้องการเพิ่มตาราง Pivot
  4. สร้างตาราง Pivot และรับตัวอย่างสำหรับการประมวลผลเพิ่มเติม
  5. กำหนดค่า Pivot Table ใหม่และเพิ่มฟิลด์ต่างๆ ลงในคอลัมน์ แถว และพื้นที่ข้อมูล
  6. บันทึกสมุดงานผลลัพธ์ที่มีตารางเดือยอยู่ในนั้น

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

รหัสเพื่อสร้าง pivot Table ใน Excel โดยใช้ C

using System.IO;
using System.Text;
using Aspose.Cells;
namespace CreatePivotTableInExcelUsingCSharp
{
class Program
{
static void Main(string[] args) // Main function to set width of cell in CSharp
{
// Instantiate the license to remove trial version watermark in the output Excel file
Aspose.Cells.License licForCells= new Aspose.Cells.License();
licForCells.SetLicense("Aspose.Cells.lic");
// Set the hard-coded data. You may use an existing Excel file also if required
byte[] SrcDataByteArray = Encoding.ASCII.GetBytes(
$@"City,Product,Sales
Paris,Cream,2300
Paris,Lotion,1600
Tunis,Cream,900
Tunis,Lotion,1400
Tunis,Cream,3090
Tunis,Lotion,6000
Paris,Cream,4320" );
// Create a memory stream from the source data
MemoryStream dataStream = new MemoryStream( SrcDataByteArray );
// Create LoadOptions class object to load the comma-separated data given above
LoadOptions loadOptions = new LoadOptions(LoadFormat.Csv);
// Instantiate a workbook class object having above mentioned data
Workbook wbCSV = new Workbook(dataStream, loadOptions);
// Get access to the first worksheet in the collection
Worksheet targetSheet = wbCSV.Worksheets[0];
// Get collection of pivot tables in the target worksheet
Aspose.Cells.Pivot.PivotTableCollection pvTablesCollection = targetSheet.PivotTables;
// Get pivot table index after adding a new pivot table by provding source data range and destination cell
int iNewPivotTable = pvTablesCollection.Add("=A1:C8", "F3", "MyPivotTable");
// Get the instance of newly added pivot table for further processing
Aspose.Cells.Pivot.PivotTable newPivotTable = pvTablesCollection[iNewPivotTable];
// Hide the grand total for rows in the output Excel file
newPivotTable.RowGrand = false;
// Add the first field to the column area
newPivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Column, 0);
// Add the second field to the row area
newPivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 1);
// Add the third field to the data area
newPivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 2);
// Saving the output Excel file with pivot table
wbCSV.Save("OutputPivotTable.xlsx");
System.Console.WriteLine("Done");
}
}
}

โค้ดตัวอย่างนี้สาธิตวิธีสร้าง pivot table ใน C# โดยระบุช่วงข้อมูล และเซลล์ปลายทางที่จะวาง pivot table พร้อมกับชื่อตาราง pivot คลาส pivotTable มีฟังก์ชัน AddFieldToArea() ที่ใช้เพื่อลากเขตข้อมูลต่างๆ ไปยังพื้นที่ต่างๆ โดยใช้ pivotFieldType เช่น Column, Row หรือ Data พร้อมกับหมายเลขเขตข้อมูลในช่วงข้อมูลที่เลือก คุณยังสามารถใช้เพจ pivotFieldType อื่นได้หากจำเป็น

ในบทความนี้ เราได้เรียนรู้การ เพิ่มตาราง Pivot ของ Excel ใน C# หากคุณต้องการเรียนรู้วิธีส่งออกข้อมูลในรายการไปยัง Excel โปรดดูบทความใน วิธีส่งออกข้อมูลรายการไปยัง Excel ใน C#

 ไทย