C# 获取Excel工作薄中Sheet页(工作表)名集合
#region 获取Excel工作薄中Sheet页(工作表)名集合 /// <summary> /// 获取Excel工作薄中Sheet页(工作表)名集合 /// </summary> /// <param name="excelFile">Excel文件名及路径,EG:C:\Users\JK\Desktop\导入测试.xls</param> /// <returns>Sheet页名称集合</returns> private String[] GetExcelSheetNames(string fileName) { OleDbConnection objConn = null; System.Data.DataTable dt = null; try { string connString=string.Empty; string FileType =fileName.Substring(fileName.LastIndexOf(".")); if (FileType == ".xls") connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";Extended Properties=Excel 8.0;"; else//.xlsx connString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\""; // 创建连接对象 objConn = new OleDbConnection(connString); // 打开数据库连接 objConn.Open(); // 得到包含数据架构的数据表 dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (dt == null) { return null; } String[] excelSheets = new String[dt.Rows.Count]; int i = 0; // 添加工作表名称到字符串数组 foreach (DataRow row in dt.Rows) { string strSheetTableName = row["TABLE_NAME"].ToString(); //过滤无效SheetName if (strSheetTableName.Contains("$")&&strSheetTableName.Replace("'", "").EndsWith("$")) { excelSheets[i] = strSheetTableName.Substring(0, strSheetTableName.Length - 1); } i++; } return excelSheets; } catch (Exception ex) { MessageBox.Show(ex.ToString()); return null; } finally { // 清理 if (objConn != null) { objConn.Close(); objConn.Dispose(); } if (dt != null) { dt.Dispose(); } } } #endregion
最后更新:2017-04-03 07:57:16
上一篇:
ScheduledExecutorService和timer的异同
下一篇:
找不到BufferedImage这个Class的解决方法
js中组合使用构造函数模式和原型模式创建对象
magento -- 推荐插件 -- 智能版Js和Css合并 --Lesti::Merge(Smart Merging of Js and Css)
C# 判断字符串是否可以转化为数字
Android Fragment中监听事件
Cmd IIS 重启
速卖通(aliexpress)活动页面优化第二轮,qps大幅提高
【菜鸟翻译】Creating an Android Project
2014百度校招笔试题之动态链接库&静态链接库详解
云计算与GigaSpaces
独家专访 | 获得软银巨额投资的 Mapbox,要如何为自动驾驶提供地图服务?