本文介绍了我想访问excel文件并在控制台应用程序中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;
using System.Data.OleDb;
using System.Data;
using Microsoft.Office.Interop.Excel;

using Excel = Microsoft.Office.Interop.Excel;


namespace ClassLibrary3
{
   
    class FileManager
    {
        private System.Data.DataTable GetDataTable(string sql, string connectionString)
        {
            System.Data.DataTable dt = null;

            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                {
                    using (OleDbDataReader rdr = cmd.ExecuteReader())
                    {
                        dt.Load(rdr);
                        return dt;
                    }
                }
            }
        }

      public void GetExcel()
        {
            string fullPathToExcel = "C:\\cdr.xls" ; //ie C:\Temp\YourExcel.xls
            string connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=yes'", fullPathToExcel);
            System.Data.DataTable dt = GetDataTable("SELECT * from [SheetName$]", connString);

            //foreach (DataRow dr in dt.Rows)
            //{
            //    //Do what you need to do with your data here
            //}
        }
    }
}













  class Program
    {
       
        
      public   static void Main(string [] args)
        {
            //FileManager fichier = new FileManager();
           GetExcel a =new GetExcel();
           Console.WriteLine(a);
           Console.ReadKey();

            //DataSet d = fichier.ReadExcelFile();
          

        }

    
    }
}





我尝试了什么:



i尝试了这个解决方案,但它无法正常工作。



What I have tried:

i tried this solution but it won't work properly.

推荐答案













  class Program
    {
       
        
      public   static void Main(string [] args)
        {
            //FileManager fichier = new FileManager();
           GetExcel a =new GetExcel();
           Console.WriteLine(a);
           Console.ReadKey();

            //DataSet d = fichier.ReadExcelFile();
          

        }

    
    }
}





我是什么尝试过:



i尝试了这个解决方案,但它无法正常工作。



What I have tried:

i tried this solution but it won't work properly.



这篇关于我想访问excel文件并在控制台应用程序中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:53