2013-08-29 32 views
1

我用下面的代码从excel中检索数据。但它跳过第一个空列,因为Usedrange属性。我怎样才能包括呢?Excel Interop - 如何获取第一个未使用列使用的范围数据?

 Excel.Application xlApp; 
     Excel.Workbook xlWorkbook; 
     xlApp = new Excel.Application(); 
     xlWorkbook = xlApp.Workbooks.Open(fileName); 
     Excel._Worksheet xlWorksheet = (Excel._Worksheet)xlApp.Workbooks[1].Worksheets[2]; 
     Excel.Range excelCell = xlWorksheet.UsedRange; 
     Object[,] values = (Object[,])excelCell.Cells.Value2; 

enter image description here

UsedRange返回B,C和D但我需要A也。

回答

0

我有一个解决方案,它可能有助于某人。

Excel.Range excelCell = xlWorksheet.UsedRange; 
Excel.Range oRng = xlWorksheet.get_Range("A1").SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell); 
     int lLastCol = oRng.Column; 
     int colCount = excelCell.Columns.Count; 
     int firstEmptyCols = lLastCol - colCount; 
相关问题