我想访问excel文件并在控制台应用程序中显示数据 [英] I wanna access to excel file and show the data in console application

查看:151
本文介绍了我想访问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.

推荐答案

,connString);

// foreach(dt.Rows中的DataRow dr)
// {
// // Do你在这里需要做什么
//}
}
}
}
", 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.


什么是它不会正常工作是什么意思? 它不起作用是最无用的问题描述。它没有传达错误信息,行为,代码期望的方式,......我们无法使用任何东西。



只看你的代码,它甚至不会编译。您正在尝试创建方法的实例。你不能这样做。您只能创建一个类的实例。你的类叫做FileManager,而不是GetExcel。



另外,你期望GetExcel返回一个DataTable,但你得到它的返回类型列为 void ,根本不会返回任何内容。
And what does "it won't work properly" mean? "It doesn't work" is the most useless problem description there is. It conveys nothing in the way of error messages, behaviors, expectations of the code, ... nothing at all we can work with.

Just looking at your code, it won't even compile. You're trying to create an instance of a method. You can't do that. You can only crate instances of a class. Your class is called FileManager, not GetExcel.

Also, you're expecting GetExcel to return a DataTable, but you're got it's return type listed as void, which won't return anything at all.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆