We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copying formatting

I love aspose. Usually. But sometimes even the simplest tasks are mindbogglingly impossible that it feels unreal.


I want to copy the formatting from range B8:B10 to a dynamic set. Lets say destination range is C8:C10.
It cant get any simpler than that. but the following code does not work.

// Copy the format of one cell - create source range
Range srcRange = wsheet.Cells.CreateRange(“B8”, “B10”);

Range destRange = wsheet.Cells.CreateRange(“C8”, “C10”)
destRange.CopyStyle(srcRange);


I cant believe that this does not work. The end formatting is all screwed up.

Hi,

I have tested your mentioned issue and found it is working fine. I have used the latest:
Aspose.Cells for .NET (Latest Version)

Please see the code below and the source and output file. Please see the screenshot.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\source.xlsx”;


Workbook workbook = new Workbook(filePath);


Worksheet worksheet = workbook.Worksheets[0];


Range rng1 = worksheet.Cells.CreateRange(“A1”, “B8”);


Range rng2 = worksheet.Cells.CreateRange(“D1”, “E8”);


rng2.CopyStyle(rng1);


workbook.Save(filePath + “.out.xlsx”);


Screenshot:

Not quite sure what is happening. The exact same code is not working for me.

I am attaching an image of the original file. The only difference is that the columns are frozen in my case where the red arrow shows. Can you copy the formatting from range B8:B10 to C8:C10?

Hi,

Please provide us your source sample project replicating this issue along with your sample input file.

Hi.

I am attaching a sample application that I wrote. Its extremely straightforward. There is a template attached too. Please look at the README folder. It contains the template as well as an explanation of what is expected.


I pull data from db and paste it into the excel. In the real world, there can be any number of rows and columns, so the range will be dynamic !

Thanks,
Sunil

Hi,

Well, your source range and destination range have different cells area to be copied/formatted, you may try to change your lines of code a bit, i.e.:

string importFile = “Template.xlsx”;

Workbook wbook = new Workbook(importFile);

Worksheet wsheet = wbook.Worksheets[0];

// Copy the format of one cell - create source range

Range srcRange = wsheet.Cells.CreateRange(“A8”, “B10”);

Range destRange = wsheet.Cells.CreateRange(“C8”, “D10”);
//wsheet.Cells.CreateRange(srcRange.FirstRow, srcRange.FirstColumn+1, srcRange.RowCount, 1);

destRange.CopyStyle(srcRange);

DataTable dt = GetDataFromDB();

wsheet.Cells.ImportDataTable(dt, true, 9,0,false);

I have tested and it works fine for your needs.

Thank you.

in excel, if you copy the formatting of a range, and apply it to a bigger range, it just copies itself multiple times. i was expecting the same behaviour.

Hi,


Yes, but using Aspose.Cells APIs, your source and destination ranges should be of same size. We may look into it if we can enhance it in future. But, please do what I suggested in my previous reply at the moment.

Thank you.