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

Free Support Forum - aspose.com

Copy Styles from one Range on worksheet to another range in a different worksheet

We are evaluating ASPose.Cells to replace our existing MS Excel based formatting app.

I am run in a road block. I am trying to Copy Values and Style from one workskeet to another worksheet. Looks like values are getting copied over but not the style. He is my sample code.

*** This code works well if the source range and target range are on the same worksheet.

/--Code start

Range source = getRange(sourceSheet, startSourceRange, endSourceRange);

Range target = getRange(targetSheet, startTargetRange, endTargetRange);

DataTable dt = sourceSheet.Cells.ExportDataTable(source.FirstRow, source.FirstColumn, source.RowCount, source.ColumnCount);

targetSheet.Cells.ImportDataTable(dt, false, target.FirstRow, target.FirstColumn, false, false);

//target.CopyValue(source); // Doesn't copy values

target.CopyStyle(source);

//Code end

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for considering Aspose.

I tested your scenario with the attached latest version of Aspose.Cells and it works fine. Please try the attached latest version, if you still face any problem, please share your template and generated file and we will check your issue soon.

Thank You & Best Regards,

Got the latest version and looks like it is working now.

One thing I noticed is that the size of the Source range and Target Range should be the same in order for the CopyValue and CopyStyle to work.

Thanks for the quick response.

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for your feedback.

trhymon:
One thing I noticed is that the size of the Source range and Target Range should be the same in order for the CopyValue and CopyStyle to work.

Well, ranges can be of different size and you may still utilize Range.CopyStyle, Range.CopyData and Range.Copy (to copy both Style & Data). Please see the following sample code in this regard.

Workbook workbook = new Workbook();

workbook.Open("D:\\Test_Temp\\area.xls");

Cells cells = workbook.Worksheets[0].Cells;

Cells cells2 = workbook.Worksheets[1].Cells;

Range range = cells.CreateRange("A1", "E5");

Range range2 = cells2.CreateRange("A11", "D14");

DataTable dt = workbook.Worksheets[0].Cells.ExportDataTable(0, 0,4, 3);

workbook.Worksheets[1].Cells.ImportDataTable(dt, false, 10, 1, false, false);

range2.CopyStyle(range);

range2.CopyData(range);

workbook.Save(@"D:\\Test_Temp\\combinestyles_test.xls");

If you face any problem, please create a sample application with your template file and post it here. We will check it soon.

Thank You & Best Regards,