External table is not in the expected format c# - c#

I have seen more answers while googling it, but couldn't solved my problem.
This is the code I have written to insert excel sheet into sql server table.
string strConnection = ConfigurationManager.ConnectionStrings[0].ToString();
string path = #"D:/Projects/sample.xlsx";
string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";";
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand("Select [Name], [City], [Address], [Designation] from [Sheet1$]", excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
//Give your Destination table name
sqlBulk.DestinationTableName = "temp1";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
While executing the line "excelConnection.Open();". I am getting an error "External table is not in the expected format."
And "sample.xlsx" is actually an ods format file. I changed its format as xlsx.
What would be wrong in this code. Please suggest me.

As per the conversation in the comments, you have to convert the ODS file into XLS or XLSX format not just renaming it. You can use Excel or Online tools to convert ODS files.

Related

How to read .xltx (Excel template) in C#

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);

C# - How to insert data from SQL table to Excel template using Console Application

I have different SQL tables that I need to export to Excel file.
I created a blank Excel Template with cell formatting but no data, even column names.
Is there a way to insert data from SQL table with undefined numbers of columns to an existing Excel Template?
I found this code, but it's not working:
string fileName = #"C:\test.xls";
string connectionString = String.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source={0};Extended Properties='Excel 12.0;HDR=YES;IMEX=0'", fileName);
using (OleDbConnection cn = new OleDbConnection(connectionString))
{
cn.Open();
OleDbCommand cmd1 = new OleDbCommand("INSERT INTO [Sheet1$] " +
"VALUES(#value1, #value2, #value3, #value4)", cn);
cmd1.Parameters.AddWithValue("#value1", "Key1");
cmd1.Parameters.AddWithValue("#value2", "Sample1");
cmd1.Parameters.AddWithValue("#value3", 1);
cmd1.Parameters.AddWithValue("#value4", 9);
cmd1.ExecuteNonQuery();
}

cannot export data from excel file to databse table when 'Enable Editing' is OFF

I want to export data from an excel file to mysql database table. I use the following code to get the data from the excel file;
string path = label4.Text;
String connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + path + ";" +
"Extended Properties=Excel 12.0;";
OleDbConnection xlConn = new OleDbConnection(connectionString);
xlConn.Open();
OleDbCommand selectCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", xlConn);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = selectCommand;
DataTable dataSet = new DataTable();
dataAdapter.Fill(dataSet);
xlConn.Close();
This code works fine when 'Enable Editing' is ON on the excel file. But, it throws the following exception when 'Enable Editing' is OFF:
System.Data.OleDb.OleDbException: 'External table is not in the expected format'
How can I access the excel file even when 'Enable Editing' is OFF?The design of excel file is not on my hands. Thanks.

Can't Find Worksheet

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.

select query at excel sheet

i have a problem with select query. My aim is selecting some columns in excel sheet and copy them in my sql table. i have six colums to copy . When i tried to copy orders changing because of the identity column.
Here is my code;
string path = Server.MapPath(Session["excel_sheet"].ToString());
//Create connection string to Excel work book
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
//Create Connection to Excel work book
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd2 = new OleDbCommand("Select [column_1],[column_2],[column_3],[column_4],[column_5],[column_6] from [Sheet1$]", excelConnection);
excelConnection.Open();
OleDbDataReader dReader;
dReader = cmd2.ExecuteReader();
SqlBulkCopy sqlBulk = new SqlBulkCopy(con);
//Give your Destination table name
sqlBulk.DestinationTableName = "MYtable";
sqlBulk.WriteToServer(dReader);
excelConnection.Close();
my table has columns like this identity_column,column_1,column_2,column_3,column_4,column_5,column_6.
After the copy for example in excel column_4 strings writes to sql column_3 it must write to column_4 . i'm wating for an answer
You can iterate through the columns and map them, this will map the column names in excel to the same name in the connection (ie Excel column_1 = sql column_1)
SqlBulkCopy sqlBulk = new SqlBulkCopy(con);
//Define column mappings
for (int i = 0; i < dReader.FieldCount; i++)
{
sqlBulk.ColumnMappings.Add(dReader.GetName(i), dReader.GetName(i));
}
You should use ColumnMappings property of SqlBulkCopy Class.
for ex:
sqlBulk.ColumnMappings.Add("column_1", "column_1");
sqlBulk.DestinationTableName = "MYtable";

Categories

Resources