第一次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 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绑定数据源显示。请高手指点。
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
*/
--转换前的数据
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)
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.