Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Wed Jun 20, 2018 6:26 am

I have to make in pivot Table with Free Spire.XLS in Visual studio c#.
I can do it and this is my Code

Code: Select all
      Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();

       Spire.Xls.Worksheet sheet = workbook.Worksheets[0];
       sheet.InsertDataTable(dtFinal, true, 1, 1);
                 
        Spire.Xls.Worksheet sheet2 = workbook.CreateEmptySheet();
        sheet2.Name = "Pivot Table";

        Spire.Xls.CellRange dataRange = sheet.Range["A1:J9205"];
        Spire.Xls.PivotCache cache = workbook.PivotCaches.Add(dataRange);
        Spire.Xls.PivotTable pt = sheet2.PivotTables.Add("Pivot Table", sheet.Range["A1"], cache);

        pt.ShowRowGrand = false;
        pt.ShowColumnGrand = false;               

        var r0 = pt.PivotFields["ptime"];
        r0.Axis = Spire.Xls.AxisTypes.Row;
        pt.Options.RowHeaderCaption = "ptime";

        var r1 = pt.PivotFields["RankingSub"];
        r1.Axis = Spire.Xls.AxisTypes.Column;
        pt.Options.ColumnHeaderCaption = "RankingSub";
        r1.Subtotals = 0;

        var r2 = pt.PivotFields["Substation"];
        r2.Axis = Spire.Xls.AxisTypes.Column;
        pt.Options.ColumnHeaderCaption = "Substation";
        r2.Subtotals = 0;
                   

        var r3 = pt.PivotFields["RankingTit"];
        r3.Axis = Spire.Xls.AxisTypes.Column;
        pt.Options.ColumnHeaderCaption = "RankingTit";
        r3.Subtotals = 0;

        var r4 = pt.PivotFields["ColumnTitle"];
        r4.Axis = Spire.Xls.AxisTypes.Column;
        pt.Options.ColumnHeaderCaption = "ColumnTitle";
        r4.Subtotals = 0;                   

        var r7 = pt.PivotFields["RankingS6"];
        r7.Axis = Spire.Xls.AxisTypes.Column;
        pt.Options.ColumnHeaderCaption = "RankingS6";
        r7.Subtotals = 0;

         var r8 = pt.PivotFields["S6_NAME"];
         r8.Axis = Spire.Xls.AxisTypes.Column;
         pt.Options.ColumnHeaderCaption = "S6_NAME";
         r8.Subtotals = 0;

         pt.DataFields.Add(pt.PivotFields["ValueAmount"], "ValueAmount", Spire.Xls.SubtotalTypes.None);                   
         pt.AllSubTotalTop = false;


                    sheet2.Range["A34:LR34"].Style.Font.Color = Color.Blue;

          string filename = @"D:\Temp\XML\PivotTable.xlsx";
         workbook.SaveToFile(filename, Spire.Xls.ExcelVersion.Version2010);

My First Question is:
When I want to change th font color of a rows the code doesn't effect. is there something I am mising

Code: Select all
                    sheet2.Range["A34:LR34"].Style.Font.Color = Color.Blue;


Thank s

nnmmss
 
Posts: 4
Joined: Wed Jun 20, 2018 5:50 am

Wed Jun 20, 2018 7:43 am

Dear nnmmss,

Thanks for your inquiry.
From your code, I guess you want to change the font color of the text in pivot table. There are sample code which could set font color for some rows of pivot table, yet I found it cannot specify a row via Range. Please check if it could meet your requirement.
Code: Select all
            string styleName = "newone";
            pt.CustomTableStyleName = styleName;
            PivotTableStyle pivotTableStyle = new PivotTableStyle(styleName);
            PivotStyle pivotStyle = workbook.CreatePivotStyle();
            pivotStyle.Font.Color = Color.Blue;
            pivotTableStyle.Styles.Add(PivotTableElement.headerRow, pivotStyle);
            workbook.AddPivotTableStyle(pivotTableStyle);

If not, please share us your input document and expected document for further investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Jun 20, 2018 8:14 am

Thank you for answer

This code you have send is just for changing the whole Pivot Table Style. I want to change just the color for one row. is it possible?

Code: Select all
sheet2.Range["A34:LR34"].Style.Font.Color = Color.Blue;


Just One row. or let's say specific rows.

Thank you

nnmmss
 
Posts: 4
Joined: Wed Jun 20, 2018 5:50 am

Wed Jun 20, 2018 8:47 am

Dear nnmmss,

Thanks for your prompt reply.
Sorry the alternative solution I provided was not suitable for you. As for your requirement of setting font color for a certian row, our dev team would look into it and I'll keep you informed about the progress.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Wed Jul 05, 2023 2:06 am

Hi,

Thank you for your patience.

I'm glad to inform you that we have implemented your requirement of setting font color for a certian row. Please use new added method pt.ApplyStyleToRange(IXLSRange cellRange, CellStyle style).
Welcome to download and test Spire.XLS Pack(Hotfix) Version:13.7.0.
Our website link: https://www.e-iceblue.com/Download/download-excel-for-net-now.html
NuGet link:https://www.nuget.org/packages/Spire.XLS/

If you have any questions, please let us know.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.XLS