Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy format from one row to another using c#

This question is quite similar to the one asked here. But the answer given suggests copying the format along with the data. I have a excel sheet (.xlsx) that I generate using SSIS. Now I have set the formatting in first row, which I want to copy to all the rows that are already filled in the worksheet. How can I do that using C#? I am using Excel interop.

like image 508
trailblazer Avatar asked Aug 30 '13 23:08

trailblazer


2 Answers

You can use PasteSpecial with xlPasteFormats.

Excel.Range R1 = (Excel.Range)oSheet.Cells[11, 11];
R1.Copy(Type.Missing);

Excel.Range R2 = (Excel.Range)oSheet.Cells[15, 15];
R2.PasteSpecial(Excel.XlPasteType.xlPasteFormats,
    Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
like image 132
Andy G Avatar answered Oct 19 '22 21:10

Andy G


So you want to copy format from first cell and apply it to all your sheet.

There is a way to process:

 Range sourceRange = sheet.get_Range("A1:A1");
 sourceRange.Copy();

 Range last = sheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing);
 Range destinationRange = sheet.get_Range("A1", last);

 destinationRange.PasteSpecial(XlPasteType.xlPasteFormats);
like image 3
mickro Avatar answered Oct 19 '22 21:10

mickro