2019/10/03(Thu) 09:23:26 編集(投稿者)
C#素人Bさんのやり方でこんな感じ
行数多ければVBAに勝てるかも
var ファイルパス = @"aaa.xlsx";
var 指定セル = "B15";
int? 最終行;
var classType = Type.GetTypeFromProgID("Excel.Application");
dynamic excel = Activator.CreateInstance(classType);
excel.Visible = false;
dynamic workbooks = excel.Workbooks;
dynamic workbook = workbooks.Open(ファイルパス);
dynamic worksheets = workbook.Worksheets;
dynamic worksheet = worksheets[1];
dynamic cells = worksheet.Cells;
dynamic startCell = cells.Range[指定セル];
var column = Convert.ToInt32(startCell.Column);
var startRow = Convert.ToInt32(startCell.Row);
dynamic lastCell = cells.SpecialCells(11);
var endRow = Convert.ToInt32(lastCell.Row);
dynamic endCell = cells[endRow, column];
var range = cells.Range[startCell, endCell];
var data = (object[,])range.value;
最終行 = null;
for (var i = data.GetLength(0); i >= 1; i--)
{
if (data[i, 1] != null)
{
最終行 = i + startRow - 1;
break;
}
}
workbook.Close(false);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
System.Runtime.InteropServices.Marshal.ReleaseComObject(endCell);
System.Runtime.InteropServices.Marshal.ReleaseComObject(lastCell);
System.Runtime.InteropServices.Marshal.ReleaseComObject(startCell);
System.Runtime.InteropServices.Marshal.ReleaseComObject(cells);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheets);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
アンドロイド9さんのやり方だと
空行あっても大丈夫?