Binding excel file to datagridview - c#

I am trying to bind excel file to dataGridView
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=E://Org.xls;"
+ "Extended Properties=" + (char)34
+ "Excel 8.0;HDR=Yes;" + (char)34;
OleDbConnection conn = new OleDbConnection(strConn);
textBox1.Text = "test";
OleDbCommand command = new OleDbCommand("Select * from [Sheet1$]", conn);
conn.Open();
dataGridView1.DataSource = command.ExecuteReader();
conn.Close();
But grid view doesn't show anything. It doesn't give error either

Heres how to do it, just need to change the path for the excel file and the reference to the grid.
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c://Org.xls;Extended Properties=" + (char)34 + "Excel 8.0;HDR=Yes;" + (char)34);
DataSet myExcelData=new DataSet();
conn.Open();
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [Sheet1$]", conn);
myDataAdapter.Fill(myExcelData);
ultraGrid1.DataSource = myExcelData;
conn.Close();

Change that code
ultraGrid1.DataSource = myExcelData;
to this
dataGridView1.DataSource = myExcelData.Tables[0];

Related

Select specific row in SQL Server

I´m starting to develop in C# and SQL Server, I don´t know how to extract information from one excel specific colum.
I have this code working, but what i need it´s to compare a textbox with a specific column and get the data:
Example
Select *
From T_Empleado
Where "Specific column" = "textbox".
public void mostrarExcel()
{
String name = "Sheet1";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + "C:\\Users\\alegriad\\Desktop\\sample\\Book2.xlsx" + "; Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]'", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
dgv_Reporte.DataSource = data;
}//mostrarExcel
Thank you.
You can write your query like this
OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$] where columnName = '"+ YourTextboxValue+ "'" , con);
I try with sample excel like below
And my query like this
OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$] WHERE Name = 'T1'", con);
This works for me.

Gridview not sorting and paging for excel sheet in asp

I have a gridview and it is taking the data from an excel sheet. In visual studio, from the properties menu of the gridview I have just set paging and sorting enabled. But this is not working. Is it about the excel source or should I add some extra code ?
string whereClause = SearchTextBox.Text;
String name = "Sheet0";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
"C:\\Users\\BerkayS\\Desktop\\VSCLOGN.xls" +
";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select " + selectedCriteria + " From [" + name + "$] where USER_ID = '" + whereClause + "'", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
GridView2.DataSource = data;
GridView2.DataBind();
GridView2.Visible = true;
Note: Ignore some variable names in the code, they are assigned before

Im having difficulty setting my OleDb connection

Hi there I am trying to establish a connection to a data source and extract the information and display it in a grid view. The problem is that i always get null value for ada. Is it possible to have mistyped the query or is there something wrong with the adapter?
Furthermore I am using the myInt variable to insert different data sources because i hav to process more than one file, maybe this could be problematic as well.
try
{
//establish connectioin
OleDbConnection conn = new OleDbConnection(("provider=Microsoft.Jet.OLEDB.4.0; " + ("data source=" + myInt + ";" + "Extended Properties=Excel 8.0;")));
OleDbDataAdapter ada = new OleDbDataAdapter("SELECT * FROM MarkingSheet$]", conn);
DataSet ds = new DataSet();
ada.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
conn.Close();
}
ANSWER
Thats what worked for me
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + myPath + ";Excel 12.0;HDR=YES;"); ;
conn.Open();
OleDbDataAdapter ada = new OleDbDataAdapter("select * from [Marking Sheet$]", conn); ;
DataSet ds = new DataSet();
ada.Fill(ds);
Change the sql to
"SELECT * FROM [MarkingSheet$]"
since there's a missing opening bracket.
The Fault in your code is that you havent opened a connection when you attempt to fill the Adapter. Your SQL Statement is also wrong. You may also wan wish to bind the DataTable to the DataGridView too like this :-
try
{
OleDbConnection conn = new OleDbConnection(("provider=Microsoft.Jet.OLEDB.4.0; " + ("data source=" + myInt + ";" + "Extended Properties=Excel 8.0;")));
OleDbDataAdapter ada = new OleDbDataAdapter("SELECT * FROM [MarkingSheet$]", conn);
DataSet ds = new DataSet();
conn.Open();
ada.Fill(ds.Tables[0]);
conn.Close();
BindingSource bs = new BindingSource();
bs.Datasource = ds.Tables[0];
dataGridView1.DataSource = bs;
}
catch(OledbException x)
{
// Handle Exception
}
EDIT **
Try Changing your connection string to :-
string connString = "provider=Microsoft.Jet.OLEDB.4.0;Data source=" + myInt + ";Extended Properties=Excel 8.0;HDR=Yes;IMEX=1\";";

Syntax error when opening an Excel workbook using C#

When I try to open an Excel workbook I get a syntax error. Here is the code I'm using:
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;"
+ "Data Source=" + fileName + ";"
+"Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
OleDbConnection objConn = new OleDbConnection(connectionString);
OleDbCommand objCommand = new OleDbCommand(#"SELECT * FROM Sheet1$", objConn);
OleDbDataAdapter odjAdp = new OleDbDataAdapter();
odjAdp.SelectCommand = objCommand;
DataTable dt1 = new DataTable();
odjAdp.Fill(dt1);
GridView2.DataSource = dt1;
GridView2.DataBind();
Why is this happening?
Because of the dollar symbol that sheet name needs to be escaped, enclose it in square brackets;
#"SELECT * FROM [Sheet1$]"

Joining two Excel Files and Displaying in a GridView

I have two different Excel files (.xls). There is a column named KATIP in excel1.xls, and there is a column named SAVCI in excel2.xls. I want to get these columns and merge them into a table named Nobet with 2 columns: SAVCI and KATIP.
Then I want to show them in a Gridview Control in ASP.NET. I have working code, but when I run it, it's getting the first column named KATIP then getting the column named SAVCI. They are not merged, and they appear as different columns.
OleDbConnection DBConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +
Server.MapPath("~/App_Data/excel1.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes\"");
DBConnection.Open();
string SQLString = "SELECT * FROM [Page1$]";
OleDbCommand DBCommand = new OleDbCommand(SQLString, DBConnection);
OleDbDataAdapter da = new OleDbDataAdapter(DBCommand);
DataSet ds = new DataSet("Nobet");
da.Fill(ds,"Nobet");
DBConnection.Close();
DBConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +Server.MapPath("~/App_Data/excel2.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes\"");
DBConnection.Open();
DBCommand = new OleDbCommand(SQLString, DBConnection);
da = new OleDbDataAdapter(DBCommand);
da.Fill(ds,"Nobet");
GridView1.DataSource = ds.Tables["Nobet"];
GridView1.DataBind();
DBConnection.Close();
I solved my problem... here is what I did:
OleDbConnection DBConnection = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +
Server.MapPath("~/App_Data/savcilik_katip.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes\"");
DBConnection.Open();
OleDbCommand DBCommand = new OleDbCommand("SELECT * FROM [Sayfa1$]", DBConnection);
OleDbDataAdapter da = new OleDbDataAdapter(DBCommand);
DataSet ds = new DataSet("Nobet");
da.Fill(ds, "Nobet");
DBConnection.Close();
DBConnection = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" +
Server.MapPath("~/App_Data/savcilik_savci.xls") + ";" + "Extended Properties=\"Excel 8.0;HDR=Yes\"");
DBConnection.Open();
DBCommand = new OleDbCommand("SELECT * FROM [Sayfa1$]", DBConnection);
da = new OleDbDataAdapter(DBCommand);
da.Fill(ds, "Nobetci");
DBConnection.Close();
for (int i = 0; i < ds.Tables["Nobet"].Rows.Count; i++)
{
ds.Tables["Nobet"].Rows[i]["SAVCI"] = ds.Tables["Nobetci"].Rows[i]["SAVCI"];
}
GridView1.DataSource = ds.Tables["Nobet"];
GridView1.DataBind();

Categories