您好,欢迎访问一九零五行业门户网

C# 获取Excel工作薄中Sheet页(工作表)名集合

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
以上就是c#  获取excel工作薄中sheet页(工作表)名集合的内容。
其它类似信息

推荐信息