Excel Interop copying cell formating

Sep 7 2009 12:20 PM
I am trying to create a c sharp program that takes an excel file with multiple sets of data and creates multiple excel files each with only one set of data in them. First I make a template based on the source file that only fits one set of data. I use this template in the Excel.Workbooks.Add command to create a new instance of the template. So far I have been using the Interop with excel to automate this and have been succssful in getting the code to copy the contents into a new excel file, and save them. It was working so well that I thought I was finished until I compared them to the source the next day and noticed that the formating was off. It is critical that I preserve all formating from the source cell. What is not getting copied is the cell properties such as background color and the rounding/significant figures of the cell (I.E: the source cell hold 8.89999999 but displays 8.9, the destination cell gets 8.89999999 copied into it successfully, but displays 8.89999999). The line of code that copies over: eDest.Cells[j, i] = eSource.Cells[j, i + iH]; i and j are variables in nested for loops to cycle through the data set. iH is an offset that is how many collumns over from the destination collum the source data is. Since the destination template is based on the source, writing to the template is equivalent to writing to the first set of data on the source file. eDest and eSource are the sheet objects for the destination and source sheets that are passed to the function Also, some of the data in the source file is the same across multiple sets, if these sets of data are next to each other then the source file has these cells "centered and merged. The above snippit copies this data properly if and only if it is copying the data from the top leftmost cell of the centered cells (I.E. if B4 though E4 are centered and merged, it only copies the data if it is reading from B4). Is there anyway to access the contents of these cells and copying the text over? If not, is there a test I can run to see if a cell is merged with other cells (something among the lines of if(Worksheet.Cells[i,j].Merged == true)). Finally, if there is not a way to do the above, how can i set a cell to active? I see a bunch of methods under activecell. that I should be able to use, but I can not find the syntax for activecell.set. Is it something using sheet.activerange?