0

I would like to copy range from an excel file to another one, and right now, I'm using the get_Range method to select the source data and then the Cells.Value2 to get the array.

And then I put this value to the get_Range of the destination file, but doing so it doesn't keep the text format.

Is there a way to keep the format and better to choose what I want to keep (like only text format and not background cell format, or border etc..) ?

PeterFour
  • 329
  • 4
  • 16
  • possible duplicate of [How to copy a formatted cell in Excel to a table cell in Word using .NET?](http://stackoverflow.com/questions/2672032/how-to-copy-a-formatted-cell-in-excel-to-a-table-cell-in-word-using-net) – Pseudonym Jul 02 '14 at 13:32
  • @PseudoNym01 Hum maybe but I'm trying to do this range wide – PeterFour Jul 02 '14 at 13:37

1 Answers1

0

Yes It will not work, as you take only the .Value2 from your initial Range, you need also the format. Because if not, the format of your destination range will be concidered. I think you can do like :

Range initial_Range = initial_worksheet.get_Range(...);
Range destination_Range = destination_worksheet.get_Range(...);

destination_Range = initial_Range;

to have all : numberFormat, backgroundColor ... of your initial range in the new one,

Or by using something like:

destination_Range.Value2 = initial_Range.Value2;
destination_Range.Font.ColorIndex = initial_Range.Font.ColorIndex;
destination_Range.Interior.ColorIndex = initial_Range.Interior.ColorIndex;
destination_Range.Interior.Pattern = initial_Range.Interior.Pattern;
destination_Range.NumberFormat = initial_Range.NumberFormat;

to have only what you need, you can choose for example to instantiate NumberFormat and not background ...

(you can loop Cells in your destination and initial Range if this doesn't work for total range at once)

  • I tried your solution but text is still without any style (like I'm trying to copy some striked text and it does not work.) – PeterFour Jul 07 '14 at 15:52
  • I also tried the Copy, PasteSpecial method which kind of work but the copied cell are paste as images. – PeterFour Jul 08 '14 at 07:29
  • I found a solution with this question : http://stackoverflow.com/questions/16990946/how-can-i-copy-excel-cells-with-rich-text-formatting-but-not-the-global-cell-for?rq=1 – PeterFour Jul 08 '14 at 07:53