I am building a program to read excel file into dataGridView.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace pro1._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string sConnecStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:\\Copy_of_Acute_HCV_2008.xls" + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conObj = new OleDbConnection(sConnecStr);
conObj.Open();
OleDbCommand sqlCommand = new OleDbCommand("SELECT * FROM [Sheet1$]",conObj);
OleDbDataAdapter adaObj = new OleDbDataAdapter();
adaObj.SelectCommand = sqlCommand;
DataSet setObj = new DataSet();
adaObj.Fill(setObj);
conObj.Close();
dataGridView1.DataSource = setObj.Tables[0];
dataGridView1.Refresh();
}
}
}
The program runs fine when i use a small excel file but when i use a big excel file it gives me this error
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: 'Sheet1$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.
thanks
edit: i always use .xls files not .xlsx