Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
sir
I write a programe export gridview to excel.I have an button controle when iam click the button table will be displayed in the Excel

The coding is below
<br />
 protected void btnExcel2_Click(object sender, EventArgs e)<br />
        {<br />
            string s = "select  student.id,name,m1,m2,m3,semister,tot from student,studentmarks where student.id=studentmarks.id";<br />
            SqlDataAdapter da = new SqlDataAdapter(s, con);<br />
            DataTable dt = new DataTable();<br />
            da.Fill(dt);<br />
            using (var wrt = new StreamWriter(@"D:\kranthi tasks in windos\exceltonotepad\exceltonote\exceltonote\date.xls"))<br />
            {<br />
                for (int i = 0; i < dt.Columns.Count; i++)<br />
                {<br />
                    wrt.Write(dt.Columns[i].ToString().Trim() + "\t");<br />
                }<br />
                wrt.WriteLine();<br />
                for (int j = 0; j < dt.Rows.Count; j++)<br />
                {<br />
                    if (dt.Rows[j][0].ToString() != null)<br />
                    {<br />
                        wrt.Write(dt.Rows[j][0].ToString().Trim());<br />
                        for (int i = 1; i < dt.Columns.Count; i++)<br />
                        {<br />
                            wrt.Write("\t" + dt.Rows[j][i].ToString().ToString().Trim());<br />
                        }<br />
                        wrt.WriteLine();<br />
                    }<br />
                }<br />
            }<br />
            Process.Start(@"D:\kranthi tasks in windos\exceltonotepad\exceltonote\exceltonote\date.xls");<br />
        }<br />


and it display in excel like this

id name m1 m2 m3 semister tot
1 Govind 90 90 100 I 280
1 Govind 90 90 100 II 280
1 Govind 90 90 100 III 280
1 Govind 90 90 100 IV 280
1 Govind 90 90 100 VI 280
1 Govind 90 90 100 VII 280
1 Govind 90 90 100 VIII 280
1 Govind 90 90 100 IX 280
2 Krishna 90 90 100 I 280
2 Krishna 90 90 100 II 280
2 Krishna 90 90 100 III 280
2 Krishna 90 90 100 IV 280
2 Krishna 90 90 100 VI 280
2 Krishna 90 90 100 VII 280
2 Krishna 90 90 100 VIII 280


but i want Display folling way in excel

id name m1 m2 m3 semister tot
1 Govind 90 90 100 I 280
90 90 100 II 280
90 90 100 III 280
90 90 100 IV 280
90 90 100 VI 280
90 90 100 VII 280
90 90 100 VIII 280
90 90 100 IX 280
2 Krishna 90 90 100 I 280
90 90 100 II 280
90 90 100 III 280
90 90 100 IV 280
90 90 100 VI 280
90 90 100 VII 280
90 90 100 VIII 280

Pls help me
Posted

1 solution

This is more of logical problem then a technical problem. But you can try these steps:
1. Keep track of id/name cells (1st and 2nd column of each row).
2. Write id/name data only if the value is changed.

For this you can have two variables(currentValue and previousValue).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900