csharp read excel file get sheetName list

简介: /// <summary> /// /// 塗聚文 /// 20120803 /// Geovin Du ///找到EXCEL的工作表名称 要考慮打開的文件的進程問題 /// </summary> /// <p
/// <summary>
         ///      
         /// 塗聚文
         /// 20120803
         /// Geovin Du
         ///找到EXCEL的工作表名称 要考慮打開的文件的進程問題
         /// </summary>
         /// <param name="filename"></param>
         /// <param name="comboBox2"></param>
         /// <returns></returns>
         public static System.Data.DataTable getSheetName(string filename, ComboBox comboBox2)
         {
             System.Data.DataTable dtSheets = new System.Data.DataTable();
             try
             {
                 
                 dtSheets.Columns.Add("id", typeof(int));
                 dtSheets.Columns.Add("SheetName", typeof(string));            
                 object objOpt = Missing.Value;
                 Excel.Application excelApp = new Excel.Application();
                 excelApp.Workbooks.Open(filename, objOpt, objOpt, objOpt, objOpt, objOpt, true, objOpt, objOpt, true, objOpt, objOpt, objOpt, objOpt, objOpt);
                 for (int i = 0; i < excelApp.Workbooks[1].Worksheets.Count; i++)
                 {
                     Excel.Worksheet ws = (Excel.Worksheet)excelApp.Workbooks[1].Worksheets[i + 1];
                     string sSheetName = ws.Name;
                     dtSheets.Rows.Add(i, ws.Name);
                 }
                 comboBox2.DataSource = dtSheets;
                 comboBox2.DisplayMember = "SheetName";
                 comboBox2.ValueMember = "id";
                 comboBox2.AutoCompleteMode = AutoCompleteMode.Suggest;
                 comboBox2.AutoCompleteSource = AutoCompleteSource.ListItems;
                 KillExcelProceed();
                 Kill(excelApp);
             }
             catch (IOException ex)
             {
                 ex.Message.ToString();
             }
             return dtSheets;
         }
         /// <summary>
         ///      
         /// 塗聚文 締友計算機信息技術有限公司
         /// 20120803
         /// Geovin Du
         /// </summary>
         /// <param name="filename"></param>
         /// <param name="comboBox2"></param>
         /// <returns></returns>
         public static System.Data.DataTable getGeovinDuSheetName(string filename, ComboBox comboBox2)
         {
             System.Data.DataTable dtSheets = new System.Data.DataTable();
             
 
             try
             {
 
                 dtSheets.Columns.Add("id", typeof(int));
                 dtSheets.Columns.Add("SheetName", typeof(string));
                 string connectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", filename); //高版本用:Microsoft.ACE.OLEDB.12.0
                 DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
                 DbConnection connection = factory.CreateConnection();
                 connection.ConnectionString = connectionString;
                 connection.Open();
                 DataTable tbl = connection.GetSchema("Tables");
                 connection.Close();
                 int i = 0;
                 foreach (DataRow row in tbl.Rows)
                 {
                     string sheetName = (string)row["TABLE_NAME"];
                     if (sheetName.EndsWith("$"))
                     {
                         sheetName = sheetName.Substring(0, sheetName.Length - 1);
                     }
                     //繁體系統需要此操作,簡體的不需要也可以
                     sheetName = sheetName.Replace("$", "");
                     sheetName = sheetName.Replace("'", "");
                     dtSheets.Rows.Add(i,sheetName.Replace("$", ""));
                     i++;
                 }
                 comboBox2.DataSource = dtSheets;
                 comboBox2.DisplayMember = "SheetName";
                 comboBox2.ValueMember = "id";
                 comboBox2.AutoCompleteMode = AutoCompleteMode.Suggest;
                 comboBox2.AutoCompleteSource = AutoCompleteSource.ListItems;
 
                 return dtSheets;
             }
             catch (IOException ex)
             {
                 ex.Message.ToString();
                 return null;
             }
                 
         }

目录
相关文章
|
3月前
|
Python
Python小姿势 - 5 tips to get the most out of list comprehensions in Python
Python小姿势 - 5 tips to get the most out of list comprehensions in Python
|
XML 开发工具 数据格式
Csharp: read excel file using Open XML SDK 2.5
/// &lt;summary&gt; /// /// &lt;/summary&gt; public class SheetNameInfo { private int _sheetId; private string _sheetName; private string _rid;
1132 0
|
C#
csharp: read excel using Aspose.Cells
/// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="strFileName"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static Syste
1544 0
|
14天前
|
SQL 缓存 easyexcel
面试官问10W 行级别数据的 Excel 导入如何10秒处理
面试官问10W 行级别数据的 Excel 导入如何10秒处理
42 0
|
26天前
|
安全 Java 数据库连接
jdbc解析excel文件,批量插入数据至库中
jdbc解析excel文件,批量插入数据至库中
19 0
|
1月前
|
Java API Apache
使用AOP+反射实现Excel数据的读取
使用AOP+反射实现Excel数据的读取
|
1月前
|
SQL 数据可视化 数据处理
使用SQL和Python处理Excel文件数据
使用SQL和Python处理Excel文件数据
51 0