I want to Import Excel to Datagridview and after search found this: Import Excel to Datagridview
stirng file = "c:\myFile.xlsx";
DataGridView dgvIM;
private void Import()
{
String name = "Items";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
file +
";Extended Properties='Excel 8.0;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
System.Data.DataTable data = new System.Data.DataTable();
sda.Fill(data);
dgvIM.DataSource = data;
}
And received this error:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: 'Items$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.
What does it mean ?
Related
I am using OleDB connection for importing .xltx and it is showing exception - "External table not in expected format"
System.Data.DataTable dtExcel = new System.Data.DataTable();
string filePath = #"C:\Template.xltx";
string xlConnStr = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filePath + "';Extended Properties= 'Excel 12.0;HDR=Yes;IMEX=1;Format=xltx'";
System.Data.OleDb.OleDbConnection cxlConn = new OleDbConnection(xlConnStr);
string query = "Select * from [Sheet2$A3:C34]";
OleDbDataAdapter dtAdapter = new OleDbDataAdapter(query, cxlConn);
dtAdapter.Fill(dtExcel);
I am using Microsoft.Jet.OLEDB.12.0 and Extended Properties=Excel 12.0
Can anyone please help me?
If not Oledb, any other way to read .xltx into datatable?
dtExcel is datatable, the line at which exception thrown is dtAdapter.Fill(dtExcel);
I want to show data in datagridview in c# using from two source one from excel sheet and other from text file .I want to join these data source .For example if I get columns from Excel sheet excel1 excel2 AND Txt1 Txt2 from text file .Then I want to show result like excel1 excel2 Txt1 Txt2 in data grid view.
Current i can show data from excel file using below code.
private void button_cars_Click(object sender, EventArgs e)
{
String name = "Car List";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "c:\\Vehicle_List.xlsx" + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select CARID, BRAND, MODEL, FUEL, CC, YEAR From [" + name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
dataGridViewMain.DataSource = data;
}
I handle this situation like this
public DataTable mergeSheetAndTextFile()
{
String name = "Sheet1";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
"D:\\YOURPATH\\List.xlsx" +
";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select col1,col2 From [" + name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
data.Columns.Add("Txt1Col", typeof(System.String));
string[] txtFileLines= File.ReadAllLines("c://YOURPATH//test.txt");
int count=0;
foreach (DataRow row in data.Rows)
{
if(count<txtFileLines.Length){
row["Txt1Col"]=txtFileLines[count]
count++;`enter code here`
}
}
}
I try to get excel sheet names, with oledb.
My connection string is:
string ConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
where filepath is a filename.
My code for this:
OleDbCommand cmd = new OleDbCommand(#"SELECT * FROM [Employee$]", conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(Data);
Get an Error in this line
adapter.Fill(Data);
Error is
'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: 'Employee$' is not a valid name. Make sure
that it does not include invalid characters or punctuation and that it
is not too long.
How can this be Done?
Try this:
OleDbCommand cmd = new OleDbCommand(#"SELECT * FROM [dataGridView1_Data$]", conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(Data);
I am trying to select the "Report Details" worksheet from my excel file. However, I am having trouble selecting it,
The Microsoft Jet database engine could not find the object 'Report Details'. Make sure the object exists and that you spell its name and the path name correctly.
if (fileExtension == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (fileExtension == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
OleDbConnection con = new OleDbConnection(connString);
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
cmd.CommandText = "SELECT * FROM [Report Details]"; //ERROR HERE
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
con.Close();
GridView1.DataSource = dtExcelRecords;
GridView1.DataBind();
Viewing my "Tables" in the dataset viewer, the connection string is able to access the file. The Report Details column is displayed as 'Report Details$'. I have tried entering it that way, but I am still getting an error.
Your question title says EXCEL/C# Cant Find Worksheet
Your post says I am trying to select the "Report Details" WORKSHEET from my excel file.
The error you are getting is
The Microsoft Jet database engine could not find the object 'Report Details'. Make sure the object exists and that you spell its name and the path name correctly.
Solution
You are missing a $ sign
Try this (TRIED AND TESTED)
cmd.CommandText = "SELECT * FROM [Report Details$]";
I was able to get the tabel name throuh dtExcelSheetName
cmd.CommandText = "SELECT * FROM [" + con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[1][2].ToString() + "]";
It is working now.
how can I go around this error
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: The Microsoft Jet database engine could not find the object Sheet1. Make sure the object exists and that you spell its name and the path name correctly"
on this code
using System.Data.OleDb;
String sConnectionString ="Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source= Book1;" + "Extended Properties=Excel 8.0;";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1]",objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1);
objConn.Close();
Try to use [Sheet1$] instead of [Sheet1].