编程语言
首页 > 编程语言> > C#删除空白单元格

C#删除空白单元格

作者:互联网

    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();
    }

这是我现有的功能.最后,我想从Excel中复制这个VB代码;

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

我尝试了几个不同的选项来取代“选择”,但我最接近的是,让C#侧选择空白单元格,但随后它将删除所有内容而不仅仅是选定的文本.

有人有我可以进去的方向吗?

也用;

using Excel = Microsoft.Office.Interop.Excel;

解决方法:

        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;

通过Heinz答案和其他一些变化的混合来管理它,因为答案不会接受AddinGlobal.

如果将来有人需要它,上述工作就有用了.它将删除“A”列中的任何空行

标签:c,excel-interop
来源: https://codeday.me/bug/20190710/1427302.html