How to read .xltx (Excel template) in C# - 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);

Related

EPPLUS import excel file without header ASP.net

Hi is it possible to read an excel file without headers using EPPLUS?
You can save the excel in csv format and skip the first line after reading all line.
var lines = File.ReadAllLines(FileName).Skip(1);
or you can use oledb connection to import data from excel to datatable .
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filename + ";" + "Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';";
string query = string.Format("SELECT * FROM [{0}$]", tablename);
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString))
{
DataSet jobDataSet = new DataSet();
dataAdapter.Fill(jobDataSet, "jobInfo");
DataTable jobDataTable = jobDataSet.Tables["jobInfo"];
}

How to read a password protected Excel worksheet with OLEDB

I have set a password on my excel sheet. To unlock the worksheet, I modified my OLEDB connection string, but it didn't work. I got an error that "the source contains no dataRows", which means that it couldn't read the data from the excel file.
Before it was fine. What might be my problem?
This is my code:
public DataTable getExcelData(string fileName, string sheetName, ComboBox[] User_ComboBox)
{
this.m_comboBox = User_ComboBox;
// connectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fileName + "';Extended Properties= 'Excel 12.0 XML;HDR=No;IMEX=1'";
connectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Password=xyz;Extended Properties='Excel 8.0;HDR=YES'";
string errorMessage = "";
DataTable dt = new DataTable();
try
{
string query = "SELECT * FROM [" + sheetName + "]";
OleDbConnection con = new OleDbConnection(connectionString);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, con);
dataAdapter.Fill(dt);
}
catch (Exception exp)
{
// errorCode = ErrorDefinition.ERROR_OLEDBERROR;
errorMessage = exp.Message;
}
return dt;
}
Instead of using an OleDbConnection you might consider using an Excel reading library like EPPlus to read the excel file. EPPlus supports reading password protected excel files.
You would need to implement some code to build the actual DataTable, but that should be simple enough. Just google it if you need to.

excel sheet to dataset c#

i'm currently using oledb for getting excel data to dataset/datatable in c#. so far it works but i encountered a problem in getting excel sheets. i have this code in getting the data from excel to dataset
bool ret = false;
DataSet dset = new DataSet();
string strConn;
try
{
if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx")
{
strConn = String.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=NO;IMEX=1;""", filePath);
}
else
strConn = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=NO;'";
//SELECT * FROM [{0}$A5:BE] dont accept all sheet names
string sql = String.Format("SELECT * FROM [{0}$A6:BH]", sheetname);
OleDbDataAdapter adap = new OleDbDataAdapter(sql, strConn);
adap.Fill(dset, "SignalList");
my problem is this select statement where i get the excel data. sometimes it accept this select statement:
String.Format("SELECT * FROM [{0}$A6:BH]", sheetname),
and sometimes it accepts this statement:
String.Format("SELECT * FROM [{0} $A6:BH]", sheetname)
Am i doing something wrong on getting data on excel? sorry for my english. please help

Import Excel to Datagridview -

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 ?

Read alphanumeric characters from csv file in C#

I am using the following code to read my csv file:
public DataTable ParseCSV(string path)
{
if (!File.Exists(path))
return null;
string full = Path.GetFullPath(path);
string file = Path.GetFileName(full);
string dir = Path.GetDirectoryName(full);
//create the "database" connection string
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;"
+ "Data Source=\"" + dir + "\\\";"
+ "Extended Properties=\"text;HDR=Yes;FMT=Delimited\"";
//create the database query
string query = "SELECT * FROM " + file;
//create a DataTable to hold the query results
DataTable dTable = new DataTable();
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
//fill the DataTable
dAdapter.Fill(dTable);
dAdapter.Dispose();
return dTable;
}
But the above doesn't reads the alphanumeric value from the csv file. it reads only i either numeric or alpha.
Whats the fix i need to make to read the alphanumeric values? Please suggest.
I suggest you use A Fast CSV Reader which does not have this issue and is much more faster.
Remove IMEX=1 from the connection string. I don't think you need it for CSV files.
Try this OleDBAdapter Excel QA I posted via stack overflow.
I have not tried this out, but it sounds interesting! LinqToExcel
they say it can be used on .CSV files as well...
hi all this code is gets alphanumeric values also
using System.Data.OleDb;
string ConnectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filepath + ";" + "Extended Properties="+(char)34+"Excel 8.0;IMEX=1;"+(char)34;
string CommandText = "select * from [Sheet1$]";
OleDbConnection myConnection = new OleDbConnection(ConnectionString);
myConnection.Open();
OleDbDataAdapter myAdapter = new OleDbDataAdapter(CommandText, myConnection);
ds = null;
ds = new DataSet();
myAdapter.Fill(ds);

Categories

Resources