第一次C#开发,帮朋友做个转换Excel数据格式的小程序,开发存在如下问题,请高手帮忙解决。
简单描述:
原始表:
序号  经度 纬度 时间
1        88     76
2        89     76
3        90     754        77     90
5        78     91
6        79     90
转换成目标表
序号  经度1 纬度1 经度2 纬度2 经度3 纬度3
1          88      76      89      76       90    75
4          77      90      78      91       79    90数据量每天大概1W左右。我首先利用OLEDB读取原始记录表,绑定DataGridView,显示出来。然后,想做个按钮,点击开始转换,形成目标表的样子利用DataGridView显示出来,最终点击“导出Excel”按钮,生成新的报表。思路就是这样的,现在在转换这一步不知道该怎么弄,如果给目标表DataGridView绑定数据源显示。请高手指点。

解决方案 »

  1.   

    --在SQL SERVER 中就是个行转列
    declare @T table (
    序号 int,经度 int,纬度 int)
    insert into @T
    select 1,88,76 union all
    select 2,89,76 union all
    select 3,90,75 union all
    select 4,77,90 union all
    select 5,78,91 union all
    select 6,79,90select 
    min(序号) as 序号,
    max(case when 序号%3=1 then 经度 else 0 end) as 经度1,
    max(case when 序号%3=1 then 纬度 else 0 end) as 纬度1,
    max(case when 序号%3=2 then 经度 else 0 end) as 经度2,
    max(case when 序号%3=2 then 纬度 else 0 end) as 纬度2,
    max(case when 序号%3=0 then 经度 else 0 end) as 经度3,
    max(case when 序号%3=0 then 纬度 else 0 end) as 纬度3
    from @T group by ceiling(序号/3.0)
    /*
    序号          经度1         纬度1         经度2         纬度2         经度3         纬度3
    ----------- ----------- ----------- ----------- ----------- ----------- -----------
    1           88          76          89          76          90          75
    4           77          90          78          91          79          90
    */
      

  2.   

    转换按钮事件就是换一下数据即可。
    --转换前的数据
    select * from tablename
    --转换后的数据
    select 
        min(序号) as 序号,
        max(case when 序号%3=1 then 经度 else 0 end) as 经度1,
        max(case when 序号%3=1 then 纬度 else 0 end) as 纬度1,
        max(case when 序号%3=2 then 经度 else 0 end) as 经度2,
        max(case when 序号%3=2 then 纬度 else 0 end) as 纬度2,
        max(case when 序号%3=0 then 经度 else 0 end) as 经度3,
        max(case when 序号%3=0 then 纬度 else 0 end) as 纬度3
    from tablename group by ceiling(序号/3.0)
      

  3.   

    谢谢叶子的回答,不过,原来从来没有做过这样的SQL,学习了。我先试试。有没有比较好理解的做法啊。
      

  4.   

    叶子女侠,你说是SQL SERVER中操作吧,我现在是操作EXCEL呢,这样好像不行的。
      

  5.   

    //添加数据     
                for (int i = 0; i < this.dataGridView1.Rows.Count-1; i++)
                {
                    n = int.Parse(this.dataGridView1.Rows[i].Cells[0].Value.ToString());
                    if (n%3==1)
                    {
                        identifier=this.dataGridView1.Rows[i].Cells[0].Value.ToString();
                        longitude1=this.dataGridView1.Rows[i].Cells[2].Value.ToString();
                        latitude1=this.dataGridView1.Rows[i].Cells[3].Value.ToString();
                        int j=0;
                        j=i+1;
                        longitude2=this.dataGridView1.Rows[j].Cells[2].Value.ToString();
                        latitude2=this.dataGridView1.Rows[j].Cells[3].Value.ToString();
                        int k=0;
                        k=k+j;
                        longitude3=this.dataGridView1.Rows[k].Cells[2].Value.ToString();
                        latitude3=this.dataGridView1.Rows[k].Cells[3].Value.ToString();
                        this.dataGridView2.Rows.Add(identifier,longitude1,latitude1,longitude2,latitude2,longitude3,latitude3);
                    }
                    else
                    {                }//谢谢叶子,我还是采用笨一些的办法,手工把数据整理好,写入DataGridView.