I am trying to get data from Excel File to DataTable.
Here's my code-snippet :
FilePath = WebConfig.SavePath + "Book2.xls";
// Create the connection object
OleDbConnection oledbConn = new OleDbConnection(WebConfig.ExcelConnection(FilePath));
// Open connection
oledbConn.Open();
// Create OleDbCommand object and select data from worksheet Sheet1 //WebConfig.SheetNameFirstExcel
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + "Sheet1" + "$]", oledbConn);
// Create new OleDbDataAdapter
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
// Create a DataSet which will hold the data extracted from the worksheet.
DataTable dt = new DataTable();
// Fill the DataSet from the data extracted from the worksheet.
oleda.Fill(dt);
Problem with this is that, data of some cells is exported to data-table while some other is NOT.
format of excel is something like :
1st Row Heading
2nd Some text
3rd Row blank
4th onwards a table
of 10 columns & 298 rows.
What is missing in above code, or any suggestion for extracting such excel(.xlsx) to datatable in asp.net 3.5
Given all the problems you have I suspect the standard oledb driver just can't read your excel file correctly due to the rows of text prior to the table data.
How about move away and just code it manually using this library http://epplus.codeplex.com/ for reading the xlsx file and create your datatable or db records
Related
I want to import an excel sheet to DataTable. i use the following code:
string path = #"" + /****path of excel file****/;
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;
itemMasterUploadDataTable = new System.Data.DataTable();
dataAdapter.Fill(itemMasterUploadDataTable );
xlConn.Close();
Everything works fine except for the left aligned values in the excel sheet.
The above image shows a part of one of the columns of the excel sheet. The right aligned values(1511, 1511, 2202, 2202) are getting entered into datatable properly but the left aligned values(0450, 0405, 0406, 0406, 0406, 0406, 0401) are getting entered as null. what am i doing wrong? I want to import all the values into the datatable.
I think it is not dependent on the alignment . All the values in the column must be of the same data type . looks like the left aligned are coming from a formula. Check the properties of the cells.
It will work if you do this in excel file:
copy the whole column
select 1st value
hold keys CTRL(CMD) and ALT(OPTION)
push V
I am reading an excel file in my application. I use an OleDbDataAdapter to read and DataTable to store records from .xlsx file. I have problem reading different date formats from single column. My column looks like that:
2014-02-07
2014-02-05
27.01.2014
19.11.2013
2014-02-07
2014-02-07
My program loads only dates in "yyyy-MM-dd" format and rest of them are DBNull.Value. Is there any way to read whole column as strings (so i can manipulate them manualy) or to force OleDbDataAdapter to read all dates?
Thats my code:
var fileName = string.Format(#"C:\28.xlsx");
var connectionstring = string.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0 xml;HDR=NO;IMEX=1'");
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$B12:L]", connectionstring);
var ds = new DataSet();
adapter.Fill(ds, "anyNameHere");
System.Data.DataTable data = ds.Tables["anyNameHere"];
foreach (DataRow row in data.Rows)
{
try
{
Console.WriteLine(row.Field<DateTime>(3).ToShortDateString());
}
catch
{
Console.WriteLine(row.Field<string>(3));
}
}
OleDb figures out the column data type based on the first row's content (if headers available, the one after the header) and applies it to the rest of the rows.
So I would suggest to change the column's data format to text rather than Date/time and then deal with them on C# side.
I have an excel sheet that I want to load into a datatable withe OleDb.
The sheet contains a multiline text column with up to 1000 chars.
However, using this code below, I only have 256 chars in my DataTable per cell after the import.
Is this a limitation from the provider or is it possible to tell it to read the whole column?
var connectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\file.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"";";
var sheetName = "Sheet1";
using (var con = new OleDbConnection(connectionString))
{
con.Open();
var table = new DataTable(sheetName);
var query = "SELECT * FROM [" + sheetName + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(query, con);
adapter.Fill(table);
return table;
}
I found a solution.
The problem is that OleDb is guessing, which dbtype to choose.
And, if the first few rows only contain data shorter than 256 chars, that is applied to all rows.
Howevery, as a workaround I just moved one row with large data to the beginning of the sheet and now the whole data gets imported.
Here is a link that describes the problem. There is also a workaround with a registry key, but I haven't tried that.
http://www.xtremevbtalk.com/showthread.php?t=206454
This registry fix worked for me.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Jet\4.0\Engines\Excel]
"TypeGuessRows"=dword:00000000
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel]
"TypeGuessRows"=dword:00000000
I am trying to import data from excel into a datatable using c#. Here is the code I use to do so...
string ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + fileName + "; Jet OLEDB:Engine Type=5;" +
"Extended Properties=\"Excel 8.0;HRD=No;IMEX=1;\"";
OleDbDataAdapter SheetAdapter = new OleDbDataAdapter("select * from ["Sheet1"]", conn);
System.Data.DataTable excelData = new System.Data.DataTable();
SheetAdapter.Fill(excelData);
excelData.TableName = "excelData";
foreach (DataRow row in excelData.Rows)
{
ProcessDataRow(row);
}
When I look at the datatable while debugging the first row of data has become the tables column names. I don't understand why this is happening when I put HDR=No into the connection string. Is there a way to force the DataTable to not take the first row as column names?
The code sample you provided has HRD=No instead of HDR=No
I used the NPOI library for just the task you inquired about and more. The only limitation is that it cannot yet handle the Excel 2007 format, so you are limited to the 97-2003 format.
I have a DataTable object which is read from Excel speradsheet. Since the spread sheet has blank columns. I want to get rid of the blank columns in my DataTable retaining only the columns which have header data. Any better approaches rather than reading though each column?
I use C# 3.5.
You can better use "OleDbConnection" rather than looping through each columns to fetch data.
It will avoid the blank columns
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0");
OleDbDataAdapter da = new OleDbDataAdapter("select * from YourTable", con);
DataTable dt = new DataTable();
da.Fill(dt);