使用c#从excel(.xlsx)读取数据 [英] Read data from excel (.xlsx) using c#

查看:96
本文介绍了使用c#从excel(.xlsx)读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用c#

解决方案

Google是您的朋友。 [ ^ ]。


您好,

试试这段代码。



  private  DataTable ExcelToDataTable()
{
string conStr = Provider = Microsoft.ACE.OLEDB.12.0;数据源= C:\\Temp \\Sample.xlsx;扩展属性='Excel 12.0 Xml; HDR = YES';

OleDbConnection connExcel = new OleDbConnection(conStr);
connExcel.Open();
尝试
{

OleDbCommand cmdExcel = new 的OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmdExcel.Connection = connExcel;

/ * 获取First Sheet的名称* /

DataTable dtexcelSchema;
dtexcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null );
string sheetName = dtexcelSchema.Rows [ 0 ] [ TABLE_NAME]。ToString();
connExcel.Close();

/ * 从第一张纸上读取数据* /
connExcel 。打开();
cmdExcel.CommandText = SELECT * FROM [ + sheetName + ];
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
connExcel.Close();

return dt;
}
catch (例外情况)
{
connExcel.Close();
throw ex;
}
}


公共部分类MainWindow:Window

{

public MainWindow()

{

InitializeComponent();

ReadFromExcel();

}





private void ReadFromExcel()

{

string con =

@Provider = Microsoft.ACE.OLEDB.12.0;数据源= D:\\2015.xlsx ;;扩展属性= Excel 12.0;;

使用(OleDbConnection连接) =新的OleDbConnection(骗局)

{

connection.Open();

OleDbCommand command = new OleDbCommand(select * from [Sheet1]

Any working code which will read data from excel using c#

解决方案

Google is your friend.[^].


Hi,
Try this code.

private DataTable ExcelToDataTable()
    {
        string conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Temp\\Sample.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES'";
        
        OleDbConnection connExcel = new OleDbConnection(conStr);
        connExcel.Open();
        try
        {

            OleDbCommand cmdExcel = new OleDbCommand();
            OleDbDataAdapter oda = new OleDbDataAdapter();
            DataTable dt = new DataTable();
            cmdExcel.Connection = connExcel;

            /*Get the name of First Sheet*/

            DataTable dtexcelSchema;
            dtexcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string sheetName = dtexcelSchema.Rows[0]["TABLE_NAME"].ToString();
            connExcel.Close();

            /*Read Data from First Sheet*/
            connExcel.Open();
            cmdExcel.CommandText = "SELECT * FROM [" + sheetName + "]";
            oda.SelectCommand = cmdExcel;
            oda.Fill(dt);
            connExcel.Close();

            return dt;
        }
        catch (Exception ex)
        {
            connExcel.Close();
            throw ex;
        }
    }


public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ReadFromExcel();
}


private void ReadFromExcel()
{
string con =
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\2015.xlsx;; Extended Properties=Excel 12.0;";
using (OleDbConnection connection = new OleDbConnection(con))
{
connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1


这篇关于使用c#从excel(.xlsx)读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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