繁体   English   中英

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

[英]How to edit excel cells without changing format of cells in C#

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

我相信您可以在此处进行更改:

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

您不需要.Value

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

检查此链接 :)

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

              xlWorkBook.Save();

代替

 xlWorkBook.SaveAs(....);

感谢您的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM