2017-08-04 179 views
4
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代码,C#删除空白单元格

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

我已经尝试了一些不同的选择替换“选择”,但我得做,是使C#边选择空白单元格最接近的,但随后会删除所有内容,而不仅仅是选定的文本。

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

也使用;

using Excel = Microsoft.Office.Interop.Excel; 

回答

0

这应该是在C#中的等价物:

Excel.Range selection = xlexcel.ActiveWindow.Selection; 
Excel.Range blankCells = selection.SpecialCells(Excel.XlCellType.xlCellTypeBlanks); 
blankCells.EntireRow.Delete(); 
+0

你好,我在AddinGlobal得到一个关于它在当前上下文中不存在的错误 – Zoltan

+0

对不起,我的错。使用Excel应用程序对象,例如'xlexcel'来自上面或下面的代码 –

0
 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; 

管理有位亨氏答案的混合和一些其他的变化来弄明白作为这个问题的答案将不接受AddinGlobal。

如果将来有人需要上述功能,上述功能就可以使用。它会删除列“A”中的任何空白行