Hi All ,
I have a Datatable say , having column column1,column2,column3.
ex data
column1 column2 column3
A xyz pqr
A asd poi
B zxc mnb
B 123 456
Now i want to export it to excel in the following way
column2 column3
A
xyz pqr
asd poi
B
zxc mnb
123 456
Please tel me how to get the desired output as shown above .
Loading

Shen HengbinPosted Aug 8, 2012, 5:33 AM
Here is my code sample .
using System;
using System.Linq;
using System.Text;
using System.Data;
public class Test
{
public static void Main()
{
var table = new DataTable();
table.Columns.Add("Column1");
table.Columns.Add("Column2");
table.Columns.Add("Column3");
table.LoadDataRow(new object[] { "A", "xyz", "pqr" }, false);
table.LoadDataRow(new object[] { "A", "asd", "poi" }, false);
table.LoadDataRow(new object[] { "B", "zxc", "mnb" }, false);
table.LoadDataRow(new object[] { "B", "123", "456" }, false);
var grps = from p in table.AsEnumerable()
group p by p["Column1"].ToString() into g
select new
{
Key = g.Key,
Rows = (from q in g
select new
{
column2 = q["Column2"].ToString(),
column3 = q["Column3"].ToString()
}).ToList()
};
foreach (var grp in grps)
{
Console.WriteLine(grp.Key);
foreach (var r in grp.Rows)
{
Console.WriteLine(string.Format("{0}\t{1}", r.column2, r.column3));
}
}
Console.Read();
}
}