日期:2014-05-17 浏览次数:21043 次
public Form1()
{
InitializeComponent();
button1.Name = "btnGetXls";
button1.Click += new EventHandler(btnGetXls_Click);
}
private void btnGetXls_Click(object send, EventArgs e)
{
string PathName = Application.StartupPath + "\\JetEazyTilt.xls";
ExcelToDataGridView(PathName, this.dgvTilt);
}
public void ExcelToDataGridView(string filePath, DataGridView dgv)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties ='Excel 14.0;HDR=NO;IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
DataTable tb = new DataTable();
foreach (DataGridViewColumn dgvc in dgv.Columns)
{