csharp read excel file get sheetName list

/// <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;
             }

         }
时间: 2024-07-31 06:58:01

csharp read excel file get sheetName list的相关文章

Csharp: read excel file using Open XML SDK 2.5

/// <summary> /// /// </summary> public class SheetNameInfo { private int _sheetId; private string _sheetName; private string _rid; /// <summary> /// /// </summary> public int SheetID { get{return _sheetId;} set{_sheetId= value;} }

csharp: read excel using Aspose.Cells

/// <summary> /// /// </summary> /// <param name="strFileName"></param> /// <returns></returns> public static System.Data.DataTable ReadExcel(String strFileName) { Workbook book = new Workbook(strFileName); //

csharp 操作excel问题!

问题描述 csharp 操作excel问题! int i=excel.range.find().count 当find找不到的时候会报错,问问find找不到返回什么,应该怎么修改才能使find找不到返回0 解决方案 Excel的Range对象(C#) 解决方案二: 不明确对象,简单的方法就用var, var rang=excel.range.find(); int i =0; if(rang!=null) i=rang.count;

Csharp: Create Excel Workbook or word from Template File using aspose.Word 14.5 and aspose.Cell 8.1

winform: /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGenWord_Click(object sender, EventArgs e) { Dictionary<string, string> d

C# Operate Excel File

excel 引言 Excel是微软公司办公自动化套件中的一个软件,他主要是用来处理电子表格.Excel以其功能强大,界面友好等受到了许多用户的欢迎.在设计应用系 统时,对于不同的用户,他们对于打印的需求是不一样的,如果要使得程序中的打印功能适用于每一个用户,可以想象程序设计是十分复杂的.由于Excel表格 的功能强大,又由于几乎每一台机器都安装了它,如果把程序处理的结果放到Excel表格中,这样每一个用户就可以根据自己的需要在Excel中定制自己的 打印.这样不仅使得程序设计简单,而且又满足了诸

Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable

/// <summary> /// 添加 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { BinaryWriter binWriter = new BinaryWriter(F

csharp create ICS file extension

  string schLocation = "Conference Room 涂聚文约会你"; string schSubject = "Business visit discussion约会"; string schDescription = "Schedule description明天到深圳市福田區車公廟冶金大大廈約會"; System.DateTime schBeginDate = Convert.ToDateTime("2/

csharp: Export or Import excel using NPOI

 excel 2003: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Globalizatio

Python_openpyxl处理Excel表格

前言 Python处理Excel表格有多种方法,其中对于.xlsx后缀的Excel版本而言openpyxl绝对是一个非常棒的选择.在openpyxl中,一个Excel文件就是一个Workbook,一张Excel文件中的表就是一个Worksheet.当我们需要对一个Excel文件进行处理的时候,需要先获取到一个Workbook对象,再获取到一个Worksheet对象,对Worksheet对象中rows.columns进行数据处理,最后通过Workbook.save()方法将Workbook对象的内