Getting the first sheet from an Excel document regardless of sheet name with OleDb

前端 未结 9 696
青春惊慌失措
青春惊慌失措 2020-12-29 02:23

I have users that name their sheets all sorts of crazy things, but I want to be able to get the first sheet of the Excel document regardless of what it is named.

I

9条回答
  •  没有蜡笔的小新
    2020-12-29 02:35

    That's my solution

     private static string GetExcelWorkSheet(string pathToExcelFile)
            {
                Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
    
                Microsoft.Office.Interop.Excel.Workbook theWorkbook = null;
    
                theWorkbook = ExcelObj.Workbooks.Open(pathToExcelFile);
    
                Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
    
                // Get the reference of first worksheet. Index start at 1 
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
    
                // Get the name of worksheet.
                string strWorksheetName = worksheet.Name; 
    
                return strWorksheetName;
            }
    

提交回复
热议问题