Excel Interop - 如何使用第一个未使用的列获取使用的范围数据?

时间:2013-08-29 09:43:14

标签: c# excel-interop

我使用以下代码从excel中检索数据。但它跳过了第一个空列,因为 Usedrange 属性。我怎么能包含它?

        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。

1 个答案:

答案 0 :(得分: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;
相关问题