仅将表格从gridview导出到Excel垂直单元格 [英] Export table from gridview to Excel perticular cells only

查看:90
本文介绍了仅将表格从gridview导出到Excel垂直单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生
我编写了一个到excel的程序导出gridview.当我单击按钮表将在Excel中显示时,我有一个按钮控件.
编码如下

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 />



并像这样在excel中显示

id名称m1 m2 m3 Semister tot
1戈文德90 90 100 I 280
1戈文德90 90 100 II 280
1戈文德90 90 100 III 280
1戈文德90 90 100 IV 280
1 Govind 90 90100 VI 280
1戈文德90 90 100 VII 280
1戈文德90 90 100 VIII 280
1戈文德90 90 100 IX 280
2克里希纳90 90 100 I 280
2克里希纳90 90 100 II 280
2克里希纳90 90 100 III 280
2克里希纳90 90 100 IV 280
2克里希纳90 90 100 VI 280
2克里希纳90 90 100 VII 280
2克里希纳90 90 100 VIII 280


但我想在excel中显示显示方式

id名称m1 m2 m3 Semister tot
1戈文德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克里希纳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

请帮助我



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

推荐答案

这更多的是逻辑上的问题,而不是技术上的问题.但是您可以尝试以下步骤:
1.跟踪ID/名称单元格(每行的第一和第二列).
2.仅当值更改时才写入ID/名称数据.

为此,您可以有两个变量(currentValue和previousValue).
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).


这篇关于仅将表格从gridview导出到Excel垂直单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆