query for reading data from excel sheet in c# - excel

Thanks Astander for replying to my query
I am here with more detailed query.
string cs = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + #"D:\\sample.xls;" + "Excel 12.0;HDR=YES;";
OleDbConnection Excelcon = new OleDbConnection(cs);
OleDbDataAdapter ad = new OleDbDataAdapter();
ad.SelectCommand = new OleDbCommand("SELECT *FROM [Sheet1$]", Excelcon);
DataTable dt = new DataTable();
ad.Fill(dt);
return dt;
I am getting error at the select statement that :
The Microsoft Office Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly.
Hope someone can help me find a solution.

What worked for me is,
when file was created, it was stored in some specific location. In my case,C:/Documents.
I had manually changed the location to D:
this was what I had written
string connStringExcel = #"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\example.xls;Extended Properties=""Excel 12.0;HDR=YES;""";`
So,the actual path should be
string connStringExcel = #"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\A\Documents\example.xls;Extended Properties=""Excel 12.0;HDR=YES;""";`
So on giving the path of correct location,my query was solved.
Hope it helps someone else too.

// Create connection string variable. Modify the "Data Source"
// parameter as appropriate for your environment.
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("../ExcelData.xls") + ";" +
"Extended Properties=Excel 8.0;";
// Create connection object by using the preceding connection string.
OleDbConnection objConn = new OleDbConnection(sConnectionString);
// Open connection with the database.
objConn.Open();
// The code to follow uses a SQL SELECT command to display the data from the worksheet.
// Create new OleDbCommand to return data from worksheet.
OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM myRange1", objConn);
// Create new OleDbDataAdapter that is used to build a DataSet
// based on the preceding SQL SELECT statement.
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
// Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect;
// Create new DataSet to hold information from the worksheet.
DataSet objDataset1 = new DataSet();
// Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1, "XLData");
// Bind data to DataGrid control.
DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
DataGrid1.DataBind();
// Clean up objects.
objConn.Close();
ref to thisLink

Related

How to skip rows while importing ExcelSheet with variable sheetname?

I have to import ExcelSheets with variable Sheetnames via SSIS. Sheetname is determined by scripttask and passed via User::Variable to ExcelSource. The problem is that data/headline always starts at row 11.
How is it possible to pass something like "$A10:AB50" (required data selection) to the sheetname delivered in User::Variable?
I´ve already tried to pass the required data-selection-string (e.g. "$A10:AB50") to OpenRowsetVariable, ExcelConnectionManager, ConnectionString... without success. I also tried skipping/splitting first 10 rows via conditional split, but couldn`t find any approach to this.
A test with a second ConnectionManager getting data from a defined file and that has data access via sql-query (e.q. "select f1,f2... from [sheetname$a1:ab50]") works great indeed.
This is how scripttask determines sheetname:
ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FILEPATH + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
OleDbConnection cnn = new OleDbConnection(ConStr);
cnn.Open();
DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetname = "";
foreach (DataRow drSheet in dtSheet.Rows)
{
// sheetname only
if (drSheet["TABLE_NAME"].ToString().Contains("$") && !drSheet["TABLE_NAME"].ToString().Contains("Print_Area"))
{
sheetname = drSheet["TABLE_NAME"].ToString();
// return sheetname
//MessageBox.Show(sheetname);
Dts.Variables["User::rSheetName"].Value = sheetname;
Solved!
Simply added the decided data selection to the sheet-return-variable in scripttask:
if (drSheet["TABLE_NAME"].ToString().Contains("$") && !drSheet["TABLE_NAME"].ToString().Contains("Print_"))
{
SheetName = drSheet["TABLE_NAME"].ToString();
// concat datazone to sheetname
DataZone = "A10:AB50";
SnameValue = SheetName + DataZone;
SnameValue = SnameValue.Replace(#"'", "");
// return combined sheetname
MessageBox.Show(SnameValue);
Dts.Variables["User::rSheetName"].Value = SnameValue;
cnn.Close();
}
It was way to easy... thanks for thinking about it anyway ;-)

sqlbulkcopy from Excel via ACE.OLEDB truncates text to 255 chars

Pretty straight-forward import using SqlBulkCopy:
string excelConnectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;\"";
using (OleDbConnection excelConnection = new OleDbConnection(excelConnectionString))
{
excelConnection.Open();
OleDbCommand cmd = new OleDbCommand("Select " + fileID.ToString() + " as [FileID], * from [Sheet1$] where [Text] IS NOT NULL", excelConnection);
OleDbDataReader dReader = cmd.ExecuteReader();
using (SqlBulkCopy sqlBulk = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString))
{
sqlBulk.DestinationTableName = "table_name";
sqlBulk.ColumnMappings.Add(0, "FileID");
sqlBulk.ColumnMappings.Add(4, "Author");
sqlBulk.ColumnMappings.Add(3, "Title");
sqlBulk.ColumnMappings.Add(1, "Body");
sqlBulk.ColumnMappings.Add(2, "PublishedDate");
sqlBulk.BulkCopyTimeout = 600;
sqlBulk.WriteToServer(dReader);
}
}
Data goes in, no problem. Except the first column, which is mapped to Body (nvarchar(max)) gets truncated to 255 characters. I looked around, found some references to workaround that involves changing a registry setting. Set value to 0 to force full scan, not just first 8 rows, which is Excel's default, but that didn't help even after reboot. Looking for other ideas. Thank you.
I used ODBC instead of the OLEDB and it doesn't truncate the values
to 255 symbols anymore:
OdbcConnection con = new OdbcConnection(#"Driver={Microsoft Excel Driver
(*.xls)};DBQ=c:\temp\testbook.xls");
OdbcCommand cmd = new OdbcCommand(#"SELECT * FROM [Workbook1$]");
cmd.Connection = con;
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
you can use 2007 format driver to access XLSX files:
.... Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)} …

OleDbConnection() opens an Excel file in any open Excel window. But does not if there isnt a window open

I am writing an application which uses an OleDbAdapter to access information in an Excel file. When I try to create a connection to the Excel file if the user has another (unrelated) Excel file open on their desktop then the file being connected to by the adapter opens in this window in Read-Only format. If the user does not have an instance of Excel open then the files stay hidden.
Here is my code:
foreach (item app in apps)
{
DataTable dt = new DataTable();
string CnStr = ("Provider=Microsoft.Jet.OLEDB.4.0;" + ("Data Source="
+ ((app.FilePath) + (";" + "Extended Properties=\"Excel 8.0;\""))));
string OleDbString = ("Select * from [" + app.SheetName + "$]");
OleDbDataAdapter Adapter = new OleDbDataAdapter();
var conn = new OleDbConnection(CnStr);
conn.Open(); <----------------------------This is where the files are being opened.
var cmd = new OleDbCommand(OleDbString, conn);
Adapter.SelectCommand = cmd;
Adapter.Fill(app.DataTable);
conn.Close();
Adapter.Dispose();
}
Does anybody know why the OleDbConnection() would open a file if an instance of Excel was open but would not if one was not?
You should post the code to initialize your apps variable. Most probably the answer to your question lies in there.
Does it use a GetObject or CreateObject method?

Reading Excel file w/ADO.net - no data (or tables)

This is my first attempt to read an Excel 2007 file via ADO.net, and I must be missing something b/c when I try to run the query, I get an exception. When I started looking, it's b/c the table (worksheet) isn't there. Can someone please tell me what I'm doing wrong?
Here is my code:
string cs = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=My File.xlsx;Extended Properties=""Excel 12.0;IMEX=1;""";
using (OleDbConnection con = new OleDbConnection(cs))
{
con.Open();
string query = "SELECT * FROM [Sheet1$]";
OleDbCommand cmd = new OleDbCommand(query, con);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
DataTable worksheets = con.GetSchema("Tables");
adapter.Fill(dt);
.
.
.
}
Take a look at the accepted answer here
The First Column of the excel file to put in string variable C#?
It works for Excel 2003 but I think it could easily be adapted to work with 2007.

Why does one ADO.NET Excel query work and another does not?

I'm working on a SharePoint workflow, and the first step requires me to open an Excel workbook and read two things: a range of categories (from a range named, conveniently enough, Categories) and a category index (in the named range CategoryIndex). Categories is a list of roughly 100 cells, and CategoryIndex is a single cell.
I'm using ADO.NET to query the workbook
string connectionString =
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + temporaryFileName + ";" +
"Extended Properties=\"Excel 12.0 Xml;HDR=YES\"";
OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();
OleDbCommand categoryIndexCommand = new OleDbCommand();
categoryIndexCommand.Connection = connection;
categoryIndexCommand.CommandText = "Select * From CategoryIndex";
OleDbDataReader indexReader = categoryIndexCommand.ExecuteReader();
if (!indexReader.Read())
throw new Exception("No category selected.");
object indexValue = indexReader[0];
int categoryIndex;
if (!int.TryParse(indexValue.ToString(), out categoryIndex))
throw new Exception("Invalid category manager selected");
OleDbCommand selectCommand = new OleDbCommand();
selectCommand.Connection = connection;
selectCommand.CommandText = "SELECT * FROM Categories";
OleDbDataReader reader = selectCommand.ExecuteReader();
if (!reader.HasRows || categoryIndex >= reader.RecordsAffected)
throw new Exception("Invalid category/category manager selected.");
connection.Close();
Don't judge the code itself too harshly; it's been through a lot. Anyway, the first command never executes correctly. It doesn't throw an exception. It just returns an empty data set. (HasRows is true, and Read() returns false, but there is no data there) The second command works perfectly. These are both named ranges.
They are populated differently, however. There's a web service call that fills up Categories. Those values are displayed in a dropdown box. The selected index goes into CategoryIndex. After hours of banging my head, I decided to write a couple of lines of code so that the dropdown's value goes into a different cell, then I copy the value using a couple of lines of C# into CategoryIndex, so that the data is set identically. That turned out to be a blind alley, too.
Am I missing something? Why would one query work perfectly and the other fail to return any data?
I have found the issue. Excel was apparently unable to parse the value in the cell, so it was returning nothing. What I had to do was adjust the connection string to the following:
string connectionString =
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + temporaryFileName + ";" +
"Extended Properties=\"Excel 12.0 Xml;HDR=NO;IMEX=1\"";
It would have been helpful if it would have thrown an exception or given any indication of why it was failing, but that's beside the point now. The option IMEX=1 tells Excel to treat all values as strings only. I'm quite capable of parsing my own integers, thankyouverymuch, Excel, so I didn't need its assistance.

Resources