DataSet ds = new DataSet();
ds.DataSetName = "Configurations";
using (
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + xlsxPath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\""))
{
try
{
using (OleDbCommand command = new OleDbCommand("select * from [Sheet1$] ", conn))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
try
{
adapter.Fill(ds);
}
catch (Exception ex1)
{
MessageBox.Show(ex1.ToString());
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//this can be extended in to dofferent formats since we got the data in an object.
ds.Tables[0].TableName = "Meters";
ds.WriteXml(xlsxPath.Substring(0, xlsxPath.IndexOf(".")) + ".xml")
From the attached spreadsheet, I want to filter a rows from dataset where value of column1 is 2 (Header name is MeterID)?
Any clue how shall I do it?
Will it be possible once the data get populated in the dataset, assign a column names as user friendly name?
Thanks
VulpesPosted Feb 12, 2015, 10:49 AM
string filterExpression = String.Format("{0} = {1}", columnName, columnValue);
DataRow[] filteredRows = ds.Tables[0].Select(filterExpression);
Sairam SohamPosted Feb 12, 2015, 11:38 AM
VulpesPosted Feb 12, 2015, 11:34 AM
So {0} means substitute the first argument (i.e. columnName) and {1} means substitute the second argument (i.e. columnValue).
You can have as many arguments as you like referenced by numbers in this way and can repeat some arguments or insert them in a different order if that is convenient for whatever it is you're doing at the time.
Sairam SohamPosted Feb 12, 2015, 11:15 AM
I am curious what does 0 and 1 means in the following line.
string filterExpression = String.Format("{0} = {1}", columnName, columnValue);
Sairam SohamPosted Feb 12, 2015, 10:42 AM
When I tried the following it worked.
ds.Tables[0].Rows[5]["Test"]
I want to do the following dynamically
DataRow[] filteredRows = ds.Tables[0].Select("MeterID = 2");
Instead of MeterID =2 (Hardcoding), I want to write
select the value of the column MeterID =2
Sairam SohamPosted Feb 12, 2015, 10:31 AM
? ds.Tables[0].Columns[0].ToString()
"MeterID"
? ds.Tables[0].Columns[0].ColumnName
"MeterID"
? ds.Tables[0].Columns[0].ColumnName = "Test"
"Test"
? ds.Tables[0].Columns[0].ToString()
"Test"
If I want to access row 5 of column "Test", how shall I do this?
VulpesPosted Feb 12, 2015, 7:31 AM
DataRow[] filteredRows = ds.Tables[0].Select("MeterID = 2");
To change the column name:
ds.Tables[0].Columns[0].ColumnName = "Whatever";