2011-11-10 85 views

回答

0

這是有點舊了。在2004年,但我用它,它幫助了我。我的理解是你想調用一個Excel表格到你的C#應用​​程序?無論如何,檢查這個網站,這將有助於如果這就是你做的。

C# - Retrieve Excel Workbook Sheet Names.

0

您可以使用此解決方案....

從這裏拍攝.... using excel oledb to get sheet names in sheet order

private Dictionary<int,string> GetExcelSheetNames(string fileName) 
    { 
    Excel.Application _excel = null; 
    Excel.Workbook _workBook = null; 
    Dictionary<int,string> excelSheets = new Dictionary<int,string>(); 
    try 
    { 
     object missing = Type.Missing; 
     object readOnly = true; 

    Excel.XlFileFormat.xlWorkbookNormal 
    _excel = new Excel.ApplicationClass(); 
    _excel.Visible = false; 
    _workBook = _excel.Workbooks.Open(fileName, 0, readOnly, 5, missing, 
    missing, true, Excel.XlPlatform.xlWindows, "\\t", false, false, 0, true, true, missing); 
    if (_workBook != null) 
    { 

        int index = 0; 

    foreach (Excel.Worksheet sheet in _workBook.Sheets) 
    { 
    // Can get sheet names in order they are in workbook 
    excelSheets.Add(++index, sheet.Name); 
    } 
    } 
} 
catch (Exception e) 
{ 
    return null; 
} 
finally 
{ 
    if (_excel != null) 
    { 

    if (_workBook != null) 
    { 
    _workBook.Close(false, Type.Missing, Type.Missing); 
    } 
    _excel.Application.Quit(); 
    } 
    _excel = null; 
    _workBook = null; 
} 
return excelSheets; 
} 
0

這裏是一個更最新的答案:

Excel.Worksheet activeWorksheet =((Excel.Worksheet)Application.ActiveSheet); string activeWorksheetName = activeWorksheet.Name;

hth

相關問題