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.

Fri Dec 13, 2024 1:04 pm

Hi-

I want to copy the values from the last 2 columns of a pivot table (The Grand totals) to a new tab. Values ONLY.

Thanks
-Tom

trozzi1957
 
Posts: 14
Joined: Sun Oct 14, 2018 10:58 pm

Mon Dec 16, 2024 3:44 am

Hello Tom,

Thank you for your inquiry.

Are you asking to copy the data from the last two columns of a PivotTable to a new sheet? If so, please refer to the example code below:
Code: Select all
Workbook workbook = new Workbook();
workbook.LoadFromFile(path+ "PivotTableExample.xlsx");

Worksheet sheet = workbook.Worksheets["PivotTable"];
Worksheet newSheet = workbook.Worksheets.Add("New Sheet");

XlsPivotTable pt = (XlsPivotTable)sheet.PivotTables[0];

CellRange cr  = pt.Location;
int firstRow = cr.Row;
int lastRow = cr.LastRow;
int lastColumn = cr.LastColumn;

CellRange cellRange = sheet.Range[firstRow,lastColumn-1,lastRow,lastColumn];

sheet.Copy(cellRange, newSheet, 1, 1, true, true);

workbook.SaveToFile(path+"result.xlsx",ExcelVersion.Version2016);

workbook.Dispose();


If I have misunderstood your request, please provide a more detailed description. Thank you for your assistance.

Sincerely,
Amy
E-iceblue support team
User avatar

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

Mon Dec 16, 2024 11:36 pm

Hi-

Did you try this against a pivot table on the worksheet?

-Tom

trozzi1957
 
Posts: 14
Joined: Sun Oct 14, 2018 10:58 pm

Tue Dec 17, 2024 2:28 am

Hi Tom,

Thank you for your response.

I created the above demo using the code in a worksheet that contains a pivot table. May I ask if this meets your requirements? If not, could you please provide detailed information about the effect you desire? Thank you!

Sincerely,
Amy
E-iceblue support team
User avatar

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

Return to Spire.XLS