Public Function ReadExcellCOM(FilePath As String, SheetName As String, Address As String, Password As String) As DataTable Dim xlApp As Microsoft.Office.Interop.Excel.Application = Nothing Dim xlBooks As Microsoft.Office.Interop.Excel.Workbooks = Nothing Dim xlBook As Microsoft.Office.Interop.Excel.Workbook = Nothing Dim xlSheets As Microsoft.Office.Interop.Excel.Sheets = Nothing Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet = Nothing Dim xlRange As Microsoft.Office.Interop.Excel.Range = Nothing Dim dt As New DataTable xlApp = New Microsoft.Office.Interop.Excel.Application 'ウィンドウの位置を(-500, -500)に、サイズを100x100に変更する MoveWindow(New IntPtr(xlApp.Hwnd), -500, -500, 100, 100, 0) xlApp.Visible = True xlApp.DisplayAlerts = False xlBooks = xlApp.Workbooks '既存のファイルを開く xlBook = xlBooks.Open(FilePath, Password:=Password) xlSheets = xlBook.Worksheets xlSheet = CType(xlSheets(SheetName), Microsoft.Office.Interop.Excel.Worksheet) xlRange = xlSheet.Range(Address) 'データの入力セル範囲 Dim arr2D = TryCast(xlRange.Value, Object(,)) '1オリジンになることに注意 Dim rowCount = arr2D.GetLength(0) '行数 Dim columnCount = arr2D.GetLength(1) '列数 '1次元配列に変換 Dim arr1D = arr2D.Cast(Of Object) '列作成 Dim heder = Enumerable.Range(1, columnCount).Select(Function(x) $"列{x.ToString}").ToArray For i = 0 To heder.Length - 1 dt.Columns.Add(heder(i).ToString, GetType(String)) Next '1オリジンを0オリジンに変換 Dim arr2D0 = arr1D.ToArray.masaTo2DArray(rowCount, columnCount) ' DataRowの作成 For r = 0 To rowCount - 1 Dim row As DataRow = dt.NewRow For c = 0 To columnCount - 1 row(heder(c).ToString) = arr2D0(r, c)?.ToString Next dt.Rows.Add(row) Next dt.AcceptChanges() Return dt End Function