如何在不改变C#中单元格格式的情况下编辑Excel单元格

时间:2014-09-25 12:12:23

标签: c# excel colors cells

我创建了一个excel文件并更改了某些单元格的颜色和宽度。我打开这个现有的excel文件,使用C#进行编辑。如果我运行程序,它会将值写入单元格,但它也会重置单元格的格式。我怎么能避免这个?如何在单元格中添加值而不更改其格式,即颜色,宽度等

    void DumpRegistersToFile(bool openFileInNotepad, uint[] registers, params uint[] registerStartEnds)
    {
        string excelFileName = "RegisterDump.xlsx";
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;

        int[] RegOffsetValues = (int[])Enum.GetValues(typeof(enumRegOffset));
        int columnOffset = 3;

        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();

        xlWorkBook = xlApp.Workbooks.Open(excelFileName,
                                                0,      // Updatelinks
                                                false,  // Readonly
                                                5,      // Format
                                                "",
                                                "",
                                                true,   // IgnoreReadOnlyRecommended
                                                Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
                                                0,      // Delimiter
                                                true,   // Editable
                                                true,   // Notify
                                                0,      // Converter
                                                true,   // Add workbook to recently used files
                                                true,   // Local
                                                0);     // CorruptLoad

        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        uint regIdx = 0;
        int colIndex = 0;

        for (int i = 0; i < registerStartEnds.Count(); i += 2)
        {
                uint startAddress = registerStartEnds[i];
                uint endAddress = registerStartEnds[i + 1];

                uint readLength = endAddress - startAddress; 

                for (int j = 0; j < readLength; j++)
                {
                    xlWorkSheet.Cells[RegOffsetValues[regIdx], columnOffset-1] = registers[regIdx];

                    for (colIndex = 0; colIndex < 16; colIndex++)
                    {
                        xlWorkSheet.Cells[RegOffsetValues[regIdx], columnOffset + (16-colIndex)].Value = ( (registers[regIdx] >> colIndex) & 1 );                     
                    }

                    regIdx++;
                }            
          }


           xlApp.DisplayAlerts = true;

           xlWorkBook.SaveAs(excelFileName,
                            Excel.XlFileFormat.xlOpenXMLWorkbook, 
                            misValue, 
                            misValue,
                            false, 
                            false, 
                            Excel.XlSaveAsAccessMode.xlNoChange,
                            misValue, 
                            misValue, 
                            misValue, 
                            misValue, 
                            misValue);



            xlWorkBook.Close(true, misValue, misValue);

            xlApp.DisplayAlerts = true;
            xlApp.Quit();



            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);

            xlWorkSheet = null;
            xlWorkBook = null;
            xlApp = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();

            Process.Start("Excel.exe", excelFileName);

    }

    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }

2 个答案:

答案 0 :(得分:0)

我相信你在这里改变它:

xlWorkSheet.Cells[RegOffsetValues[regIdx], columnOffset + (16-colIndex)].Value = ( (registers[regIdx] >> colIndex) & 1 );

您不需要 .Value

xlWorkSheet.Cells[RegOffsetValues[regIdx], columnOffset + (16-colIndex)] = ( (registers[regIdx] >> colIndex) & 1 ); 

检查this link:)

答案 1 :(得分:0)

我发现自己的错误:)我不应该保存为...文件,我应该保存它。

              xlWorkBook.Save();

而不是

 xlWorkBook.SaveAs(....);

感谢您的帮助。