Reputation: 25
hello guys we got a project from school. its getting data from excel and putting it to datagridview. i got and excel file named "data.xls" and inside a sheet name "clay"; my code gets saying they can't find the sheet clay. any comments... here is my code using asp.net
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.OleDb;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String filePath = textPath.Text;
String sheetName = textSheet.Text;
string constr= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;'";
OleDbConnection con = new OleDbConnection(constr);
OleDbDataAdapter sda = new OleDbDataAdapter("Select * from ['" + sheetName + "$']",con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
Upvotes: 2
Views: 155
Reputation: 98868
I don't think you need to use single quotes with sheetName
.
Use this instead;
OleDbDataAdapter sda = new OleDbDataAdapter("Select * from [" + sheetName + "$]",con);
Upvotes: 2