|
1 | | -// Copyright 2025 The excelize Authors. All rights reserved. Use of this source |
2 | | -// code is governed by a BSD-style license that can be found in the LICENSE |
3 | | -// file. |
| 1 | +// Copyright 2025 - 2026 The excelize Authors. All rights reserved. Use of this |
| 2 | +// source code is governed by a BSD-style license that can be found in the |
| 3 | +// LICENSE file. |
4 | 4 | // |
5 | 5 | // Package excelize-cs is a C# port of Go Excelize library, providing a set of |
6 | 6 | // functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / |
@@ -470,6 +470,113 @@ public void TestOpenFile() |
470 | 470 | Assert.Equal("zip: not a valid zip file", err.Message); |
471 | 471 | } |
472 | 472 |
|
| 473 | + [Fact] |
| 474 | + public void TestPivotTable() |
| 475 | + { |
| 476 | + File f = Excelize.NewFile(); |
| 477 | + List<string> months = new List<string> |
| 478 | + { |
| 479 | + "Jan", |
| 480 | + "Feb", |
| 481 | + "Mar", |
| 482 | + "Apr", |
| 483 | + "May", |
| 484 | + "Jun", |
| 485 | + "Jul", |
| 486 | + "Aug", |
| 487 | + "Sep", |
| 488 | + "Oct", |
| 489 | + "Nov", |
| 490 | + "Dec", |
| 491 | + }; |
| 492 | + List<int> year = new List<int> { 2017, 2018, 2019 }; |
| 493 | + List<string> types = new List<string> { "Meat", "Dairy", "Beverages", "Produce" }; |
| 494 | + List<string> region = new List<string> { "East", "West", "North", "South" }; |
| 495 | + Random random = new Random(); |
| 496 | + Assert.Null( |
| 497 | + Record.Exception(() => |
| 498 | + { |
| 499 | + f.SetSheetRow( |
| 500 | + "Sheet1", |
| 501 | + "A1", |
| 502 | + new List<object> { "Month", "Year", "Type", "Sales", "Region" } |
| 503 | + ); |
| 504 | + for (int row = 2; row < 32; row++) |
| 505 | + { |
| 506 | + f.SetCellValue( |
| 507 | + "Sheet1", |
| 508 | + Excelize.CoordinatesToCellName(1, row), |
| 509 | + months[random.Next(12)] |
| 510 | + ); |
| 511 | + f.SetCellValue( |
| 512 | + "Sheet1", |
| 513 | + Excelize.CoordinatesToCellName(2, row), |
| 514 | + year[random.Next(3)] |
| 515 | + ); |
| 516 | + f.SetCellValue( |
| 517 | + "Sheet1", |
| 518 | + Excelize.CoordinatesToCellName(3, row), |
| 519 | + types[random.Next(4)] |
| 520 | + ); |
| 521 | + f.SetCellValue( |
| 522 | + "Sheet1", |
| 523 | + Excelize.CoordinatesToCellName(4, row), |
| 524 | + random.Next(5000) |
| 525 | + ); |
| 526 | + f.SetCellValue( |
| 527 | + "Sheet1", |
| 528 | + Excelize.CoordinatesToCellName(5, row), |
| 529 | + region[random.Next(4)] |
| 530 | + ); |
| 531 | + } |
| 532 | + f.AddPivotTable( |
| 533 | + new PivotTableOptions |
| 534 | + { |
| 535 | + DataRange = "Sheet1!A1:E31", |
| 536 | + PivotTableRange = "Sheet1!G2:M34", |
| 537 | + Rows = new PivotTableField[] |
| 538 | + { |
| 539 | + new() { Data = "Month", DefaultSubtotal = true }, |
| 540 | + new() { Data = "Year" }, |
| 541 | + }, |
| 542 | + Filter = new PivotTableField[] { new() { Data = "Region" } }, |
| 543 | + Columns = new PivotTableField[] |
| 544 | + { |
| 545 | + new() { Data = "Type", DefaultSubtotal = true }, |
| 546 | + }, |
| 547 | + Data = new PivotTableField[] |
| 548 | + { |
| 549 | + new() |
| 550 | + { |
| 551 | + Data = "Sales", |
| 552 | + Name = "Summarize", |
| 553 | + Subtotal = "sum", |
| 554 | + }, |
| 555 | + }, |
| 556 | + RowGrandTotals = true, |
| 557 | + ColGrandTotals = true, |
| 558 | + ShowDrill = true, |
| 559 | + ShowRowHeaders = true, |
| 560 | + ShowColHeaders = true, |
| 561 | + ShowLastColumn = true, |
| 562 | + } |
| 563 | + ); |
| 564 | + }) |
| 565 | + ); |
| 566 | + var err = Assert.Throws<RuntimeError>(() => f.AddPivotTable(new PivotTableOptions { })); |
| 567 | + Assert.Equal( |
| 568 | + "parameter 'PivotTableRange' parsing error: parameter is required", |
| 569 | + err.Message |
| 570 | + ); |
| 571 | + Assert.Null( |
| 572 | + Record.Exception(() => |
| 573 | + { |
| 574 | + f.SaveAs("TestAddPivotTable.xlsx"); |
| 575 | + }) |
| 576 | + ); |
| 577 | + Assert.Empty(f.Close()); |
| 578 | + } |
| 579 | + |
473 | 580 | [StructLayout(LayoutKind.Sequential)] |
474 | 581 | public unsafe struct C1 |
475 | 582 | { |
|
0 commit comments