C# 使用COM互操作复制Excel工作表会丢失单元格颜色

C# 使用COM互操作复制Excel工作表会丢失单元格颜色,c#,.net,excel,com,interop,C#,.net,Excel,Com,Interop,我尝试过使用COM互操作合并工作表的各种方法 以下是我的结论(为了简洁起见省略了不必要的代码): 我遇到了相同的问题,并发现以下Microsoft支持说明: 他们在这里提到了一些解决办法。对我来说,以下解决方案有效:只需将新工作簿的配色方案设置为旧工作簿的配色方案,如下所示: mergeToWorkbook.Colors = sourceWorkbook.Colors; // Reset the background colour in the cells to whi

我尝试过使用COM互操作合并工作表的各种方法

以下是我的结论(为了简洁起见省略了不必要的代码):


我遇到了相同的问题,并发现以下Microsoft支持说明: 他们在这里提到了一些解决办法。对我来说,以下解决方案有效:只需将新工作簿的配色方案设置为旧工作簿的配色方案,如下所示:

mergeToWorkbook.Colors = sourceWorkbook.Colors;
            // Reset the background colour in the cells to white. For some reason the worksheet copy
            // operation screws up the cell backgrounds - pinks, purples and other business-like 
            // colours.

            foreach (Excel.Worksheet targetWorksheet in mergeToWorkbook.Worksheets)
            {
                var usedRange = targetWorksheet.UsedRange;

                usedRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White);
            }
mergeToWorkbook.Colors = sourceWorkbook.Colors;