如何与QUOT;清理" Microsoft.Office.Interop.Excel.WorkbookOffice、Microsoft、QUOT、Workbook

2023-09-06 23:00:42 作者:吃醋是最诚实的告白

所以我有一个Microsoft.Office.Interop.Excel.Workbook对象。它基本上使用模板的Excel文件来构造本身。该Excel文件包含模板列色为结款等,然后将code基本上只是打印了这些模板列,它实际上并没有自定义文件本身的外观,只有把数据放入它。

So I have a Microsoft.Office.Interop.Excel.Workbook object. It basically uses a template Excel file to construct itself. The Excel file contains a template column color for the results section, etc. and then the code basically just prints over those template columns, it doesn't actually customize the look of the file itself, only puts the data into it.

不过,这是一个问题,因为它的完成后,我们的模板占了最可能的行是可以的,但很多时代(大部分时间),我们用连其中的一半。

However, this is an issue because after it's done, our template accounts for the most POSSIBLE rows it can, but a lot of the times (most of the time), we use not even half of them.

什么是去除没有单元格数据在他们已创建的文件后,直接与Microsoft.Office.Interop.Excel.Workbook对象工作的所有行的最简单的方法。我们已经有了创建后运行的清理的方法,但我想这种逻辑添加到它。下面是我们目前的清理:

What's the easiest way to remove all rows that DO NOT have cell data in them after the file has been created, working directly with the Microsoft.Office.Interop.Excel.Workbook object. We already have a "cleanup" method that runs after creation, but I want to add that logic to it. Here's our current cleanup:

private void CleanupExcel()
        {
            if (!_visible && _workbook != null)
            {
                _workbook.Close(false, Missing.Value, Missing.Value);
            }
            _workbook = null;
            _sheet = null;
            if (_excel != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(_excel);
                // WW, 5/26/09: not sure if a problem here, but it probably is since the code was taken from here
                // but in the indicator, Excel exists in the process even after the app is closed.  The code here seems to fix it.
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            _excel = null;
        }

P.S。它的两片的方式的文件中的第一个。我也有机会获得Microsoft.Office.Interop.Excel.Worksheet对象,如果它更容易做到这一点的方式。

P.S. It's the first of two sheets in the document by the way. I also have access to the Microsoft.Office.Interop.Excel.Worksheet object if it's easier to do that way.

推荐答案

假设所有的空行是在表的底部,你应该能够选择他们作为一个范围,然后将它们全部删除,这样的事情我认为:

Assuming that all the empty rows are at the bottom of the sheets, you should be able to select them as a range and then delete them all, something like this I think:

Excel.Range range = _sheet.get_Range("A501", "A60000");
Excel.Range row = range.EntireRow; 
rowDelete(Type.Missing);

如果他们不是在底部,也许你可以做一个排序,使他们都最终在底部,然后用类似的东西到我的code。

If they're not at the bottom, maybe you could do a sort so that they all end up at the bottom and then use something similar to my code.