获取最后一个单元格的位置:
获取该单元格的位置名称,我是用于给定开始单元位置自动拿到最后一个单元格名称产生想要操作(复制、粘贴等)的范围。
首先定一个A到Z列的名称字符串数组,因为所有单元格的位置都是由字母(列)和数字(行)组成的。
1.调用 Microsoft.Office.Interop.Excel.dll
引用using Excel = Microsoft.Office.Interop.Excel;
2.创建对Excel操作的对象
string FilePath = @"D:\Study\VBA\VB.xlsm";//要操作Excel的路径
Excel.Application app = new Excel.Application();
Excel.Workbook wb = app.Workbooks.Open(FilePath);
app.Visible = true;
3.指定要操作的Sheet
Excel.Worksheet ws = (Excel.Worksheet)wb.Sheets[1];
4.获取该张表的总行数
int RowsCount = ws.UsedRange.Cells.Rows.Count;
5.获取该的总列数
int ColumnCount = ws.UsedRange.Cells.Columns.Count;
6.根据所获取的最后一個單元格地址,获取到该列号名称--->Address
string sc = ws.UsedRange.Columns.Address;
wb.Close();
app.Quit();