【问题标题】:How to edit excel cells without changing format of cells in C#如何在不更改 C# 中的单元格格式的情况下编辑 Excel 单元格
【发布时间】:2014-11-20 05:08:25
【问题描述】:

我创建了一个 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();
        }
    }

【问题讨论】:

    标签: c# excel colors cells


    【解决方案1】:

    我相信你在这里改变它:

    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 :)

    【讨论】:

    • 感谢您的回答,但我已经尝试过了(有或没有.Value)。在您发送的文章中,它会创建一个新的 excel 文件并更改格式。它是不同的。我想编辑现有的和以前格式化的 excel 文件。
    • hmmm...我觉得没关系,您使用的是哪个版本? (办公室和实习生)
    • 噢,对不起,我忘了提。 Visual Studio Express 2013 V12、.NET 4.5、Microsoft Excel 15.0 对象库、Microsoft Office 2013。
    • Hmmm... 尝试在此处输入字符串,例如 = "test" 现在我记得我正在更改单元格的文本(并且不要更改单元格的背景 - 你想要那个)但我会得到类似的代码在 1-2 小时内(当我回到家时),所以请稍等 :) 我正在使用 office2010 不确定 interp...
    • 我试过了,还是不行。好的,我会等你的代码,谢谢:)
    【解决方案2】:

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

                  xlWorkBook.Save();
    

    而不是

     xlWorkBook.SaveAs(....);
    

    感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-23
      • 2013-06-02
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多