C # Delete empty cells

    public static void ExportData()
    {
        Microsoft.Office.Interop.Excel.Application xlexcel;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;
        xlexcel = new Excel.Application();
        xlexcel.Visible = true;
        xlWorkBook = xlexcel.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        xlWorkBook.Application.ScreenUpdating = false;
        Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
        CR.Select();
        xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
        Excel.Range DR = (Excel.Range)xlWorkSheet.Columns["A:A"];
        DR.Select();
        DR.Delete();
        Excel.Range A1 = (Excel.Range)xlWorkSheet.Cells[1, 1];
        A1.Select();
    }

This is the current function that I have. In the end, I would like to reproduce this VB code from Excel, which I have;

    Columns("A:A").Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.EntireRow.Delete

I tried several different options, replacing "Selection", but the closest I had C # to select empty cells, but then it will delete ALL, not just the selected text.

Does anyone have a direction I can go?

Also using

using Excel = Microsoft.Office.Interop.Excel;
+4
source share
2 answers

This should be equivalent in C #:

Excel.Range selection = xlexcel.ActiveWindow.Selection;
Excel.Range blankCells = selection.SpecialCells( Excel.XlCellType.xlCellTypeBlanks );
blankCells.EntireRow.Delete();
0
source
        Microsoft.Office.Interop.Excel.Application xlexcel;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
        object misValue = System.Reflection.Missing.Value;
        xlexcel = new Excel.Application();
        xlexcel.Visible = true;
        xlWorkBook = xlexcel.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        xlWorkBook.Application.ScreenUpdating = false;
        Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
        CR.Select();
        xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
        Excel.Range DR = (Excel.Range)xlWorkSheet.Columns["A:A"];
        DR.Select();
        DR.Delete();
        Excel.Range ACol = (Excel.Range)xlWorkSheet.Columns["A:A"];
        Excel.Range Ronge = ACol.SpecialCells(Excel.XlCellType.xlCellTypeBlanks);
        Ronge.EntireRow.Delete();
        Excel.Range A1 = (Excel.Range)xlWorkSheet.Cells[1, 1];
        A1.Select();
        xlWorkBook.Application.ScreenUpdating = true;

Skillfully deal with a small number of Heinz answers and some other changes, as this answer would not accept AddinGlobal.

, - . "A"

0

Source: https://habr.com/ru/post/1683017/


All Articles