UsedRange in excel column

情到浓时终转凉″ 提交于 2019-12-10 19:35:52

问题


How do you get a column from the UsedRange; for example column A?

xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open("C:\\base.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

//range = xlWorkSheet.UsedRange;

range = xlWorkSheet.get_Range("A") // reading from A1 to max of excel 65536?

回答1:


@Lance's answer:

range = xlWorkSheet.UsedRange.Columns["A:A", Type.Missing] 

will give you the first column of UsedRange. If, for example, the first column that contains data is column C, it will actually return the used part of column C.

To get the used rows of the first column in the worksheet, use Intersect:

range = xlApplication.Intersect(xlWorksheet.UsedRange, xlWorksheet.Columns["A:A", Type.Missing])

This will return null if column A is not part of the UsedRange.




回答2:


This will get you Column A, which as Joe points out might be the first column in your UsedRange:

range = xlWorkSheet.UsedRange.Columns["A:A", Type.Missing]

This will get you the first Column in your range, so you can reference by number:

range = xlWorkSheet.UsedRange.Columns[1, Type.Missing]


来源:https://stackoverflow.com/questions/5490903/usedrange-in-excel-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!