private string getExcelSheetFirstNameFor(string path)
{
string firstName = "";
Excel.Application objExcelApp = new Application();
objExcelApp.Visible = false;
objExcelApp.DisplayAlerts = false;
Workbooks objWorkbooks = objExcelApp.Workbooks;
_Workbook objWorkbook = null;
_Worksheet objWorksheet = null;
object objMissing = System.Reflection.Missing.Value;
try
{
objWorkbooks.Open(path, objMissing, true, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, true, objMissing, objMissing, objMissing, objMissing, objMissing);
objWorksheet = (Worksheet)objWorkbooks.get_Item(1).Worksheets.get_Item(1);
firstName = objWorksheet.Name;
}
catch (Exception ex)
{
}
finally
{
objWorksheet = null;
if (objWorkbook != null)
{
objWorkbook.Close(objMissing, objMissing, objMissing);
objWorkbook = null;
}
if (objWorkbooks != null)
{
objWorkbooks.Close();
objWorkbooks = null;
}
if (objExcelApp != null)
{
objExcelApp.Quit();
objExcelApp = null;
}
}
return firstName;
}
转载请注明:清风亦平凡 » C#获得Excel第一个工作表的名称