就是简单的做到从excel读取数据,但是程序调适出错,还有我添加过控件:Microsoft Excel 12.0 Object Library,按照道理应该能成的,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;namespace oleDb_app
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
string connection_str = "provider=Microsoft.ACE.OLEDB.12.0;Data Source='f:\\appText.xlsx';Extended Properties=Excel 12.0 Xml;HDR=YES";
using (OleDbConnection connection = new OleDbConnection(connection_str))
{
OleDbCommand cmd= new OleDbCommand("select * from [Sheet1$]", connection);
OleDbDataAdapter adapt = new OleDbDataAdapter();
DataSet dtset = new DataSet();
try
{
adapt.SelectCommand = cmd;
adapt.Fill(dtset, "[Sheet1$]");
dataGridView1.DataSource = dtset.Tables[0];
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString ());
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Excel = Microsoft.Office.Interop.Excel;namespace oleDb_app
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
string connection_str = "provider=Microsoft.ACE.OLEDB.12.0;Data Source='f:\\appText.xlsx';Extended Properties=Excel 12.0 Xml;HDR=YES";
using (OleDbConnection connection = new OleDbConnection(connection_str))
{
OleDbCommand cmd= new OleDbCommand("select * from [Sheet1$]", connection);
OleDbDataAdapter adapt = new OleDbDataAdapter();
DataSet dtset = new DataSet();
try
{
adapt.SelectCommand = cmd;
adapt.Fill(dtset, "[Sheet1$]");
dataGridView1.DataSource = dtset.Tables[0];
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString ());
}
}
}
}
}
string strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;";
strConnection += @"Data Source=C:\a.xlsx;";
strConnection += "Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
using(OleDbConnection objConnection = new OleDbConnection(strConnection))
{
objConnection.Open();
OleDbDataAdapter myCommandd = new OleDbDataAdapter("select * from [Sheet1$]", objConnection);
myCommandd.Fill(ds);
}
调试好了,这句话错了,谢谢