[英]How to get the selected sheet name in Excel using c#.net
嗨朋友們,我是使用 Excel 的初學者。 我需要弄清楚如何從 Excel 的工作簿中找到選定的工作表名稱。
非常感謝。
德瓦拉吉
這是一個更新的答案:
Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Application.ActiveSheet);
string activeWorksheetName = activeWorksheet.Name;
第
Application xlsxApp;
string sheetname = (xlsxApp.ActiveSheet).Name;
這有點舊了。 在 2004 年,但我使用了它,它幫助了我。 我的理解是您想將某個 excel 表調用到您的 c# 應用程序中? 不管怎樣,看看這個網站,如果那是你所做的,它會有所幫助。
您可以使用此解決方案....
取自這里...... 使用excel oledb按工作表順序獲取工作表名稱
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.