Aspose.Cells for C++'ta Pivot Tablolarına Stil Uygulama

Giriş

Aspose.Cells, pivot tabloları için iki paralel stil API’si sunar. Aralarındaki seçim, çalışma kitabını okuduğunuz formata göre değil, kaydettiğiniz formata göre belirlenir. Bir .xls dosyasından yüklenen çalışma kitabı .xlsx olarak yeniden kaydedilebilir; bu durumda eski stil API’si yerine modern stil API’si geçerli olur.

  • PivotTable.PivotTableStyleType, yerleşik adlandırılmış stillerden birini seçer (açık ve koyu temalar, Excel 2017’de eklenen stiller dahil). Bu hazır ayarlar salt okunurdur.
  • PivotTable.PivotTableStyleName, Worksheets.TableStyles.AddPivotTableStyle(...) aracılığıyla kendinizin tanımladığı özel bir stili seçer. Hazır ayarların izin verdiğinin ötesinde renk, kenarlık veya yazı tipi değişiklikleri istediğinizde özel stiller gereklidir. Ayrıca, PivotTable.FormatAll(Style), tek bir Style nesnesini pivotun her hücresine uygulayan ve yukarıdaki stil adı API’lerinden herhangi biriyle yapılan ayarları geçersiz kılan bir kısayoldur. Bu, altta yatan temadan bağımsız olarak tek tip bir görünüm gerektiğinde kullanışlıdır.

Eski XLS Hazır Otomatik Biçimini Uygulama

PivotTable.AutoFormatType, Aspose.Cells.Pivot.PivotTableAutoFormatType numaralandırmasından bir değer kabul eder. Kullanılabilir değerler Report1‘den Report10‘a, Classic ve Table1‘den Table10‘a kadardır. Aşağıdaki örnek, yeni bir çalışma kitabı yükler, Fruit/Year/Amount örnek verilerini ekler, bir pivot tablo oluşturur, PivotTableAutoFormatType.Report5 uygular ve sonucu .xls olarak kaydeder.

#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main() {
    Aspose::Cells::Startup();
    // Yeni bir çalışma kitabı oluştur
    Workbook workbook;
    // İlk çalışma sayfasını al
    Worksheet sheet = workbook.GetWorksheets().Get(0);
    // Başlık satırı (Meyve, Yıl, Miktar) ve 2020 ile 2021 yılları için üzüm, yaban mersini, kivi, kirazı kapsayan 9 veri satırı ile kaynak verileri doldur
    sheet.GetCells().Get(0, 0).PutValue(u"Fruit");
    sheet.GetCells().Get(0, 1).PutValue(u"Year");
    sheet.GetCells().Get(0, 2).PutValue(u"Amount");
    sheet.GetCells().Get(1, 0).PutValue(u"grape");
    sheet.GetCells().Get(1, 1).PutValue(2020);
    sheet.GetCells().Get(1, 2).PutValue(50);
    sheet.GetCells().Get(2, 0).PutValue(u"blueberry");
    sheet.GetCells().Get(2, 1).PutValue(2020);
    sheet.GetCells().Get(2, 2).PutValue(30);
    sheet.GetCells().Get(3, 0).PutValue(u"kiwi");
    sheet.GetCells().Get(3, 1).PutValue(2020);
    sheet.GetCells().Get(3, 2).PutValue(25);
    sheet.GetCells().Get(4, 0).PutValue(u"cherry");
    sheet.GetCells().Get(4, 1).PutValue(2020);
    sheet.GetCells().Get(4, 2).PutValue(40);
    sheet.GetCells().Get(5, 0).PutValue(u"grape");
    sheet.GetCells().Get(5, 1).PutValue(2021);
    sheet.GetCells().Get(5, 2).PutValue(60);
    sheet.GetCells().Get(6, 0).PutValue(u"blueberry");
    sheet.GetCells().Get(6, 1).PutValue(2021);
    sheet.GetCells().Get(6, 2).PutValue(35);
    sheet.GetCells().Get(7, 0).PutValue(u"kiwi");
    sheet.GetCells().Get(7, 1).PutValue(2021);
    sheet.GetCells().Get(7, 2).PutValue(28);
    sheet.GetCells().Get(8, 0).PutValue(u"cherry");
    sheet.GetCells().Get(8, 1).PutValue(2021);
    sheet.GetCells().Get(8, 2).PutValue(45);
    sheet.GetCells().Get(9, 0).PutValue(u"grape");
    sheet.GetCells().Get(9, 1).PutValue(2020);
    sheet.GetCells().Get(9, 2).PutValue(45);
    // Hedef hücre E3'e "Pivot1" adında, A1:C10 kaynak aralığını kullanan bir özet tablo ekle
    int pivotIndex = sheet.GetPivotTables().Add(u"A1:C10", u"E3", u"Pivot1");
    PivotTable pivotTable = sheet.GetPivotTables().Get(pivotIndex);
    // Alanları ata: Fruit -> Satırlar, Amount -> Veri
    pivotTable.AddFieldToArea(PivotFieldType::Row, u"Fruit");
    pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
    // Eski XLS ön ayar biçimi "Report5"i uygula
    pivotTable.SetAutoFormatType(PivotTableAutoFormatType::Report5);
    // Çalışma kitabını eski .xls biçiminde kaydet
    workbook.Save(u"output.xls");
    Aspose::Cells::Cleanup();
    return 0;
}

Modern Adlandırılmış Hazır Pivot Tablosu Stilini Uygulama

#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main() {
    Aspose::Cells::Startup();
    Workbook workbook;
    Worksheet worksheet = workbook.GetWorksheets().Get(0);
    Cells cells = worksheet.GetCells();
    cells.Get(u"A1").PutValue(u"Fruit");
    cells.Get(u"B1").PutValue(u"Year");
    cells.Get(u"C1").PutValue(u"Amount");
    cells.Get(u"A2").PutValue(u"Grape");
    cells.Get(u"B2").PutValue(2020);
    cells.Get(u"C2").PutValue(100);
    cells.Get(u"A3").PutValue(u"Blueberry");
    cells.Get(u"B3").PutValue(2020);
    cells.Get(u"C3").PutValue(150);
    cells.Get(u"A4").PutValue(u"Kiwi");
    cells.Get(u"B4").PutValue(2020);
    cells.Get(u"C4").PutValue(200);
    cells.Get(u"A5").PutValue(u"Cherry");
    cells.Get(u"B5").PutValue(2020);
    cells.Get(u"C5").PutValue(180);
    cells.Get(u"A6").PutValue(u"Grape");
    cells.Get(u"B6").PutValue(2021);
    cells.Get(u"C6").PutValue(120);
    cells.Get(u"A7").PutValue(u"Blueberry");
    cells.Get(u"B7").PutValue(2021);
    cells.Get(u"C7").PutValue(170);
    cells.Get(u"A8").PutValue(u"Kiwi");
    cells.Get(u"B8").PutValue(2021);
    cells.Get(u"C8").PutValue(210);
    cells.Get(u"A9").PutValue(u"Cherry");
    cells.Get(u"B9").PutValue(2021);
    cells.Get(u"C9").PutValue(190);
    cells.Get(u"A10").PutValue(u"Grape");
    cells.Get(u"B10").PutValue(2021);
    cells.Get(u"C10").PutValue(130);
    int pivotIndex = worksheet.GetPivotTables().Add(u"A1:C10", u"E3", u"Pivot1");
    PivotTable pivotTable = worksheet.GetPivotTables().Get(pivotIndex);
    pivotTable.AddFieldToArea(PivotFieldType::Row, u"Fruit");
    pivotTable.AddFieldToArea(PivotFieldType::Column, u"Year");
    pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
    pivotTable.SetPivotTableStyleType(PivotTableStyleType::PivotTableStyleDark1);
    workbook.Save(u"output.xlsx");
    Aspose::Cells::Cleanup();
    return 0;
}

Özel Bir Pivot Tablosu Stili Tanımlama ve Uygulama

Yerleşik hazır ayarlar değiştirilemez. Renkleri, kenarlıkları veya yazı tiplerini geçersiz kılmanız gerektiğinde, özel bir pivot stili tanımlamalısınız. İş akışı üç adımdan oluşur:

  1. Worksheets.TableStyles.AddPivotTableStyle(string name) aracılığıyla çalışma kitabının TableStyles koleksiyonuna özel bir stil ekleyin. Bu, yeni oluşturulan stilin dizinini döndürür.
  2. Stili, TableStyle.TableStyleElements.Add(TableStyleElementType) aracılığıyla öğeler (WholeTable veya GrandTotalRow gibi) ekleyerek yapılandırın, ardından TableStyleElement.SetElementStyle(Style) ile her öğeye bir Style atayın.
  3. PivotTable.PivotTableStyleName değerini stilin adına ayarlayarak özel stili pivota uygulayın. Burada PivotTableStyleType kullanmayın; çünkü bu özellik yerleşik hazır ayarları seçer.

Kullanılabilir TableStyleElementType değerleri WholeTable, FirstRow, LastRow, FirstColumn, LastColumn, GrandTotalRow, GrandTotalColumn, PageFieldLabels ve PageFieldValues değerlerini içerir. Aşağıdaki örnek, WholeTable üzerinde ince siyah kenarlık ve GrandTotalRow üzerinde kalın kırmızı yazı tipi içeren özel bir pivot stili tanımlar, ardından bunu PivotTableStyleName aracılığıyla uygular ve .xlsx olarak kaydeder.

#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main() {
    Aspose::Cells::Startup();
    Workbook workbook;
    Worksheet worksheet = workbook.GetWorksheets().Get(0);
    Cells cells = worksheet.GetCells();
    // Kaynak verileri doldur: başlık satırı + 9 veri satırı (A1:C10)
    cells.Get(u"A1").PutValue(u"Fruit");
    cells.Get(u"B1").PutValue(u"Year");
    cells.Get(u"C1").PutValue(u"Amount");
    cells.Get(u"A2").PutValue(u"Grape");
    cells.Get(u"B2").PutValue(2020);
    cells.Get(u"C2").PutValue(100);
    cells.Get(u"A3").PutValue(u"Blueberry");
    cells.Get(u"B3").PutValue(2020);
    cells.Get(u"C3").PutValue(200);
    cells.Get(u"A4").PutValue(u"Kiwi");
    cells.Get(u"B4").PutValue(2020);
    cells.Get(u"C4").PutValue(300);
    cells.Get(u"A5").PutValue(u"Cherry");
    cells.Get(u"B5").PutValue(2020);
    cells.Get(u"C5").PutValue(400);
    cells.Get(u"A6").PutValue(u"Grape");
    cells.Get(u"B6").PutValue(2021);
    cells.Get(u"C6").PutValue(500);
    cells.Get(u"A7").PutValue(u"Blueberry");
    cells.Get(u"B7").PutValue(2021);
    cells.Get(u"C7").PutValue(600);
    cells.Get(u"A8").PutValue(u"Kiwi");
    cells.Get(u"B8").PutValue(2021);
    cells.Get(u"C8").PutValue(700);
    cells.Get(u"A9").PutValue(u"Cherry");
    cells.Get(u"B9").PutValue(2021);
    cells.Get(u"C9").PutValue(800);
    cells.Get(u"A10").PutValue(u"Grape");
    cells.Get(u"B10").PutValue(2021);
    cells.Get(u"C10").PutValue(900);
    // A1:C10'dan kaynaklanan, E3'e sabitlenmiş ve "Pivot1" adında pivot tablo ekle
    int pivotIndex = worksheet.GetPivotTables().Add(u"A1:C10", u"E3", u"Pivot1");
    PivotTable pivotTable = worksheet.GetPivotTables().Get(pivotIndex);
    pivotTable.AddFieldToArea(PivotFieldType::Row, u"Fruit");
    pivotTable.AddFieldToArea(PivotFieldType::Column, u"Year");
    pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
    // Adım 1: yeni bir özel pivot tablo stili kaydet ve dizinini yakala
    int styleIndex = workbook.GetWorksheets().GetTableStyles().AddPivotTableStyle(u"CustomPivotStyle");
    TableStyle tableStyle = workbook.GetWorksheets().GetTableStyles().Get(styleIndex);
    // Adım 2: bir WholeTable öğesi ekle ve dört kenara ince siyah kenarlıklar uygula
    int wholeTableElementIndex = tableStyle.GetTableStyleElements().Add(TableStyleElementType::WholeTable);
    TableStyleElement wholeTableElement = tableStyle.GetTableStyleElements().Get(wholeTableElementIndex);
    Style wholeTableStyle = workbook.CreateStyle();
    wholeTableStyle.GetBorders().Get(BorderType::TopBorder).SetLineStyle(CellBorderType::Thin);
    wholeTableStyle.GetBorders().Get(BorderType::TopBorder).SetColor(Color::Black());
    wholeTableStyle.GetBorders().Get(BorderType::BottomBorder).SetLineStyle(CellBorderType::Thin);
    wholeTableStyle.GetBorders().Get(BorderType::BottomBorder).SetColor(Color::Black());
    wholeTableStyle.GetBorders().Get(BorderType::LeftBorder).SetLineStyle(CellBorderType::Thin);
    wholeTableStyle.GetBorders().Get(BorderType::LeftBorder).SetColor(Color::Black());
    wholeTableStyle.GetBorders().Get(BorderType::RightBorder).SetLineStyle(CellBorderType::Thin);
    wholeTableStyle.GetBorders().Get(BorderType::RightBorder).SetColor(Color::Black());
    wholeTableElement.SetElementStyle(wholeTableStyle);
    // Adım 3: bir GrandTotalRow öğesi ekle ve kalın kırmızı yazı tipi uygula
    int grandTotalElementIndex = tableStyle.GetTableStyleElements().Add(TableStyleElementType::GrandTotalRow);
    TableStyleElement grandTotalElement = tableStyle.GetTableStyleElements().Get(grandTotalElementIndex);
    Style grandTotalStyle = workbook.CreateStyle();
    grandTotalStyle.GetFont().SetIsBold(true);
    grandTotalStyle.GetFont().SetColor(Color::Red());
    grandTotalElement.SetElementStyle(grandTotalStyle);
    // Adım 4: özel stili ada göre uygula (PivotTableStyleType ile DEĞİL, bu yerleşik ön ayarlar içindir)
    pivotTable.SetPivotTableStyleName(u"CustomPivotStyle");
    workbook.Save(u"output.xlsx");
    Aspose::Cells::Cleanup();
    return 0;
}

FormatAll ile Her Pivot Hücresine Tek Bir Stil Uygulama

PivotTable.FormatAll(Style), tek bir Style nesnesini pivot tablosunun veri alanı, satır ve sütun başlıkları ile toplamlar dahil her hücresine uygulayan bir kısayoldur. PivotTableStyleType veya PivotTableStyleName aracılığıyla daha önce ayarlanan her şey geçersiz kılınır.

Aşağıdaki örnek, sarı düz dolgu, kalın koyu mavi yazı tipi ve tüm kenarlarda ince siyah kenarlıklar içeren bir Style oluşturur, ardından bunu FormatAll ile uygular ve .xlsx olarak kaydeder.

#include "Aspose.Cells.h"
#include <string>
using namespace Aspose::Cells;
using namespace Aspose::Cells::Pivot;
int main() {
    Aspose::Cells::Startup();
    Workbook wb;
    Worksheet worksheet = wb.GetWorksheets().Get(0);
    // Başlık satırı
    worksheet.GetCells().Get(u"A1").PutValue(u"Fruit");
    worksheet.GetCells().Get(u"B1").PutValue(u"Year");
    worksheet.GetCells().Get(u"C1").PutValue(u"Amount");
    // Veri satırları
    worksheet.GetCells().Get(u"A2").PutValue(u"Grape");
    worksheet.GetCells().Get(u"B2").PutValue(2020);
    worksheet.GetCells().Get(u"C2").PutValue(5000);
    worksheet.GetCells().Get(u"A3").PutValue(u"Blueberry");
    worksheet.GetCells().Get(u"B3").PutValue(2020);
    worksheet.GetCells().Get(u"C3").PutValue(3000);
    worksheet.GetCells().Get(u"A4").PutValue(u"Kiwi");
    worksheet.GetCells().Get(u"B4").PutValue(2020);
    worksheet.GetCells().Get(u"C4").PutValue(4000);
    worksheet.GetCells().Get(u"A5").PutValue(u"Cherry");
    worksheet.GetCells().Get(u"B5").PutValue(2020);
    worksheet.GetCells().Get(u"C5").PutValue(2000);
    worksheet.GetCells().Get(u"A6").PutValue(u"Grape");
    worksheet.GetCells().Get(u"B6").PutValue(2021);
    worksheet.GetCells().Get(u"C6").PutValue(6000);
    worksheet.GetCells().Get(u"A7").PutValue(u"Blueberry");
    worksheet.GetCells().Get(u"B7").PutValue(2021);
    worksheet.GetCells().Get(u"C7").PutValue(3500);
    worksheet.GetCells().Get(u"A8").PutValue(u"Kiwi");
    worksheet.GetCells().Get(u"B8").PutValue(2021);
    worksheet.GetCells().Get(u"C8").PutValue(4500);
    worksheet.GetCells().Get(u"A9").PutValue(u"Cherry");
    worksheet.GetCells().Get(u"B9").PutValue(2021);
    worksheet.GetCells().Get(u"C9").PutValue(2500);
    worksheet.GetCells().Get(u"A10").PutValue(u"Grape");
    worksheet.GetCells().Get(u"B10").PutValue(2021);
    worksheet.GetCells().Get(u"C10").PutValue(5500);
    // Pivot tablosu ekle: kaynak aralık A1:C10, hedef hücre E3, ad "Pivot1"
    int pivotIndex = worksheet.GetPivotTables().Add(u"A1:C10", u"E3", u"Pivot1");
    PivotTable pivotTable = worksheet.GetPivotTables().Get(pivotIndex);
    // Pivot alanlarını ata
    pivotTable.AddFieldToArea(PivotFieldType::Row, u"Fruit");
    pivotTable.AddFieldToArea(PivotFieldType::Column, u"Year");
    pivotTable.AddFieldToArea(PivotFieldType::Data, u"Amount");
    // Pivot tablosunun her hücresine uygulanacak bir Stil oluştur
    Style style = wb.CreateStyle();
    style.SetForegroundColor(Color::Yellow());
    style.SetPattern(BackgroundType::Solid);
    style.GetFont().SetIsBold(true);
    style.GetFont().SetColor(Color::DarkBlue());
    style.GetBorders().Get(BorderType::TopBorder).SetLineStyle(CellBorderType::Thin);
    style.GetBorders().Get(BorderType::TopBorder).SetColor(Color::Black());
    style.GetBorders().Get(BorderType::BottomBorder).SetLineStyle(CellBorderType::Thin);
    style.GetBorders().Get(BorderType::BottomBorder).SetColor(Color::Black());
    style.GetBorders().Get(BorderType::LeftBorder).SetLineStyle(CellBorderType::Thin);
    style.GetBorders().Get(BorderType::LeftBorder).SetColor(Color::Black());
    style.GetBorders().Get(BorderType::RightBorder).SetLineStyle(CellBorderType::Thin);
    style.GetBorders().Get(BorderType::RightBorder).SetColor(Color::Black());
    // FormatAll uygula
    pivotTable.FormatAll(style);
    // Çalışma kitabını kaydet
    wb.Save(u"output.xlsx");
    Aspose::Cells::Cleanup();
    return 0;
}

Hangi Stil API’sini Kullanmalıyım?

Stil API’si seçimi, kaydettiğiniz dosya formatına bağlıdır. Hızlı bir başvuru olarak aşağıdaki tabloyu kullanın.

Hedef dosya formatı Kullanılacak API Notlar
.xls (eski) PivotTable.AutoFormatType Aspose.Cells.Pivot.PivotTableAutoFormatType değerleri (ör. Report1Report10, Classic, Table1Table10). Modern formatlara kaydederken yok sayılır.
.xlsx / .xlsm / .xlsb (modern, yerleşik stil) PivotTable.PivotTableStyleType Aspose.Cells.PivotTableStyleType değerleri (açık/koyu temalar, Excel 2017 eklemeleri dahil).
.xlsx / .xlsm / .xlsb (modern, özel stil) PivotTable.PivotTableStyleName + Worksheets.TableStyles.AddPivotTableStyle(...) Yerleşik hazır ayarlar yeterli olmadığında kullanın. TableStyleElement.SetElementStyle(...) aracılığıyla yapılandırın.
Herhangi bir format (tek tip geçersiz kılma) PivotTable.FormatAll(Style) Pivotun tamamında diğer tüm stil ayarlarını geçersiz kılan kısayol.
Kararsız kaldığınızda, .xlsx olarak kaydedin ve yerleşik temalar için PivotTableStyleType, özel temalar için PivotTableStyleName kullanın.