java得到excel所有的sheet名_获取execl中所有的sheet名字

private static List GetExcelSheetNames(string filePath)

{

List names = new List();

Microsoft.Office.Interop.Excel.Application excelApp = null;

object missing = System.Reflection.Missing.Value;

try

{

excelApp = new Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.Workbooks wbs = excelApp.Workbooks;

Microsoft.Office.Interop.Excel.Workbook wb = wbs.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing,

Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,

Type.Missing, Type.Missing, Type.Missing, Type.Missing);

int count = wb.Worksheets.Count;

for (int i = 1; i <= count; i++)

names.Add(((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[i]).Name);

wb.Close(missing, missing, missing);

wb = null;

excelApp.Quit();

}

catch

{

excelApp.Quit();

}

return names;

}

private static List GetExcelSheetNames(string filePath) { List names = new List(); Microsoft.Office.Interop.Excel.Application excelApp = null; object missing = System.Reflection.Missing.Value; try { excelApp = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbooks wbs = excelApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook wb = wbs.Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); int count = wb.Worksheets.Count; for (int i = 1; i <= count; i++) names.Add(((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[i]).Name); wb.Close(missing, missing, missing); wb = null; excelApp.Quit(); } catch { excelApp.Quit(); } return names; }
经验分享 程序员 微信小程序 职场和发展