Get specific column from Data Table C# - c#

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

Related

cannot import left aligned values from excel sheet to datatable

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

OleDbAdapters doesn't read dates in different formats from single excel column

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.

Load Excel sheet into DataTable but only the first 256 chars per cell are imported,

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

Importing Excel data into C# without first row becoming column names?

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.

Selecting specified columns form DataTable in C#

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

Categories