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
Related
I'm having a Data Table who gets values from some excel file. I use button as File dialog, find file somewhere in file system and then I parse needed values there.
Later in application I'm gonna need just one column from that Data Table. It is column named max t on picture attached.
Below is the code I'm using to get values from file:
string pathConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection connection = new OleDbConnection(pathConnection);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [DAYTIME CONFORT INDEX$]", connection);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
myDataGridView.DataSource = dt;
As you can see on picture I have few columns but only need some of them. For example I need to get values from column max t into some list of doubles.
I've tried few things but non of them didn't worked. Since I'm a beginner with this can someone help me with easiest way to do this.
Thank you. :)
List columns you need in query. E.g. getting only columns M and max t:
"Select [M],[max t] from [DAYTIME CONFORT INDEX$]"
This will give you an array values from the max t Column from your dataTable
DataView view = new DataView(dt);
DataTable distinctValues = view.ToTable(true, "max t");
DataRow[] myRows = distinctValues.Select();
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 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
I am importing excel sheet to DataTable using oledb connection as below.
private static DataTable UploadExcelSheet(string fileName)
{
DataTable uploadDataTable;
using (OleDbConnection objXConn = new OleDbConnection())
{
objXConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName +
";Extended Properties=\"Excel 12.0;IMEX=1\"";
objXConn.Open();
OleDbCommand objCommand =
new OleDbCommand("SELECT * FROM Template$ ", objXConn);
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter();
// retrieve the Select command for the Spreadsheet
objDataAdapter.SelectCommand = objCommand;
// Create a DataSet
DataSet objDataSet = new DataSet();
// Populate the DataSet with the spreadsheet worksheet data
objDataAdapter.Fill(objDataSet);
uploadDataTable = objDataSet.Tables[0];
}
return uploadDataTable;
}
Everything is working fine but problem comes when user delete content of few rows before uploading the excel. It reads those empty rows as well along with non empty rows, and saving data in database fails because of business rule violation (mandatory field missing).
What I tried is putting where condition in query :
"SELECT * FROM WHERE not [CandidateId*] = 0 or not [Firstname*] = '' or not [Lastname] = '' or not [type*] = '' or not [DOB*] =" + DBNull.Value
So it will select only those rows which has data.
But I am not able to compare non string field i.e. Date, Integer etc. Which are comming as DBNull when empty.
Can any one please suggest the way to do it, I dont want to use DataReader.
Expanding on vc's answer, this will remove all rows that which each of it's columns contain either nothing or white space:
dataTable = dataTable.Rows.Cast<DataRow>().Where(row => !row.ItemArray.All(field => field is System.DBNull || string.Compare((field as string).Trim(), string.Empty) == 0)).CopyToDataTable();
How about filtering the rows after the query has executed using Linq to object:
var filteredRows = uploadDataTable.Rows.Cast<DataRow>().Where(
row => row.ItemArray.Any(field => !(field is System.DBNull)));
Use
".. WHERE NOT ([Lastname] = '' OR [DOB*] IS NULL OR ... )
Expanding on the previous answers, this worked for me. Delete rows where all fields are null.
Dim deleteRows = From row In result.AsEnumerable
Where row.ItemArray.All(Function(field) Equals(field, DBNull.Value))
For Each deleteRow In deleteRows
deleteRow.Delete()
Next
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.