A simple Example of data processing from Excel as the datasource

来源:互联网 发布:拍立得什么牌子好 知乎 编辑:程序博客网 时间:2024/05/20 05:08

This Example is about data processing using dataset.The datasource is an xls file,In the following content,you'll see how to connect to  the datasource,how to load data to dataset  from Excel and how to deal with your  loaded data,after this,we'll write our data result to a new xls file using stream.

This article assumes you're familiar with C#.

If you're a beginner to learn about DataSet, it may somehow help u.The code was written in 2 hours,so the format was not very well.This may more or less influence your reading,so I remarked in Chinese,hope this can make any help.

If this article really doed any help,I'll be glad to here that,meanwhile,my job makes some sense.

Background:

I made such an example because one friend of mine has to treat with experiment data(theodolite) using calculator ,this is really fussy work,which you should do many calculations using your hand.My friend asked me to do something to make the experiment data treating to be automatic,and it will be much better if the treating result can be recorded and printed.I made all his request become reality.Here is how:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
//命名空间
namespace ReadExcelTest
{
    
//Form类
    public partial class Form1 : Form
    
{
        
//变量定义
        private int i = 0, j = 0;
        
private double[] Pi = null;
        
private double[] Circle = null;
        
private OleDbDataAdapter Adap1;
        
private OleDbDataAdapter Adap2;
        
private OleDbDataAdapter Adap3;
        
private OleDbDataAdapter Adap4;
        
//构造函数
        public Form1()
        
{
            InitializeComponent();
        }

        
//类内部函数区
        Function Area
        
//Form1_Load函数
        private void Form1_Load(object sender, EventArgs e)
        
{
            Pi 
= new double[318000 };
            Circle 
= new double[336000 };
            OleDbConnection OleDbCon 
= new OleDbConnection(this.strCon);
            
string query1 = "select * from [Sheet1$]";
            
string query2 = "select * from [Sheet2$]";
            
string query3 = "select * from [Sheet3$]";
            
string query4 = "select * from [Sheet4$]";
            OleDbCon.Open();
            Adap1 
= new OleDbDataAdapter(query1, OleDbCon);
            Adap2 
= new OleDbDataAdapter(query2, OleDbCon);
            Adap3 
= new OleDbDataAdapter(query3, OleDbCon);
            Adap4 
= new OleDbDataAdapter(query4, OleDbCon);
            Adap1.Fill(ds, 
"[Sheet1$]");
            Adap2.Fill(ds, 
"[Sheet2$]");
            Adap3.Fill(ds, 
"[Sheet3$]");
            Adap4.Fill(ds, 
"[Sheet4$]");
            OleDbCon.Close();
        }

        
//calc ave of Lij and Rij
        private void button1_Click(object sender, EventArgs e)
        
{
            
double Ave_Lij = 0;
            
double Ave_Rij = 0;
            
double L_temp_1 = 0;
            
double L_temp_2 = 0;
            
double R_temp_1 = 0;
            
double R_temp_2 = 0;
            
for (i = 4; i < this.ds.Tables[0].Rows.Count; i++)
            
{
                
if (this.ds.Tables[0].Rows[i][1].ToString() == "" || ds.Tables[0].Rows[i][1].ToString() == null)
                    
break;
                
try
                
{
                    L_temp_1 
= double.Parse(ds.Tables[0].Rows[i][4].ToString());
                    L_temp_2 
= double.Parse(ds.Tables[0].Rows[i][5].ToString());
                    R_temp_1 
= double.Parse(ds.Tables[0].Rows[i][9].ToString());
                    R_temp_2 
= double.Parse(ds.Tables[0].Rows[i][10].ToString());
                }

                
catch (System.FormatException e1)
                
{
                }

                Ave_Lij 
= (L_temp_1 + L_temp_2) / 2;
                Ave_Rij 
= (R_temp_1 + R_temp_2) / 2;
                
this.ds.Tables[0].Rows[i][6= Ave_Lij;
                
this.ds.Tables[0].Rows[i][11= Ave_Rij;

            }

            
this.dataGridView1.DataSource = this.ds.Tables[0];
            
this.tabPage1.Show();

        }


        
//选中某个tabpage更新相关表格并显示
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        
{
            
if (tabControl1.SelectedIndex == 0)
            
{
                
this.tabPage1.Select();
                
this.dataGridView1.DataSource = this.ds.Tables[0];
                
this.tabPage1.Show();
            }

            
if (tabControl1.SelectedIndex == 1)
            
{
                
this.tabPage2.Select();
                
this.dataGridView2.DataSource = this.ds.Tables[1];
                
this.tabPage2.Show();
            }

            
if (tabControl1.SelectedIndex == 2)
            
{
                
this.tabPage3.Select();
                
this.dataGridView3.DataSource = this.ds.Tables[2];
                
this.tabPage3.Show();
            }

            
if (tabControl1.SelectedIndex == 3)
            
{
                
this.tabPage4.Select();
                
this.dataGridView4.DataSource = this.ds.Tables[3];
                
this.tabPage4.Show();
            }

        }

        
//calc (I)j
        private void button2_Click(object sender, EventArgs e)
        
{
             
double[] L = new double[3];
            
double[] R = new double[3];
            
double[] Ij = new double[3];
            
for (i = 4; i < this.ds.Tables[0].Rows.Count; i++)
            
{
                
if (this.ds.Tables[0].Rows[i][1].ToString() == "" || ds.Tables[0].Rows[i][1].ToString() == null)
                    
break;
                
try
                
{
                    L[
0= double.Parse(ds.Tables[0].Rows[i][2].ToString());
                    L[
1= double.Parse(ds.Tables[0].Rows[i][3].ToString());
                    L[
2= double.Parse(ds.Tables[0].Rows[i][6].ToString());
                    R[
0= double.Parse(ds.Tables[0].Rows[i][7].ToString());
                    R[
1= double.Parse(ds.Tables[0].Rows[i][8].ToString());
                    R[
2= double.Parse(ds.Tables[0].Rows[i][11].ToString());

                }

                
catch (System.FormatException e2)
                
{ }
                Ij 
= CalcAngel(L, R, "+"false);
                
if (L[0> R[0])
                
{
                    Ij 
= CalcAngel(Ij, Pi, "+"true);
                    ds.Tables[
0].Rows[i][12= Ij[0];
                    ds.Tables[
0].Rows[i][13= Ij[1];
                    ds.Tables[
0].Rows[i][14= Ij[2];
                }

                
else
                
{

                    Ij 
= CalcAngel(Ij, Pi, "-"true);
                    ds.Tables[
0].Rows[i][12= Ij[0];
                    ds.Tables[
0].Rows[i][13= Ij[1];
                    ds.Tables[
0].Rows[i][14= Ij[2];
                }
                
            }

            
this.dataGridView1.DataSource = this.ds.Tables[0];
            
this.tabPage1.Show();
        }

        
//calc (I)'j
        private void button3_Click(object sender, EventArgs e)
        
{
            
double[] Ij2 = new double[3];
            
double[] down1 = new double[3];//被减数共有三个 down1,down2,down3
            double[] down2 = new double[3];
            
double[] down3 = new double[3];
            
double[] up = new double[3];
            
int groupcount = 0;//返回组别个数(5个一组)
            for (i = 4; i < this.ds.Tables[0].Rows.Count; i++)
            
{
                
if (this.ds.Tables[0].Rows[i][1].ToString() == "" || ds.Tables[0].Rows[i][1].ToString() == null)
                
{
                    groupcount 
= i;
                    groupcount 
= (groupcount - 4/ 5;
                    
break;
                }

            }

            
//对每一组进行处理
            for (j = 0; j < groupcount; j++)
            
{
                    i
=4;
                    ds.Tables[
0].Rows[i+j*5][15= 0;
                    ds.Tables[
0].Rows[i + j * 5][16= 0;
                    ds.Tables[
0].Rows[i + j * 5][17= 0;
                    ds.Tables[
0].Rows[i + j * 5 + 4][15= null;
                    ds.Tables[
0].Rows[i + j * 5 + 4][16= null;
                    ds.Tables[
0].Rows[i + j * 5 + 4][17= null;
                    
try
                    
{
                        up[
0= double.Parse(ds.Tables[0].Rows[i+j*5][12].ToString());
                        up[
1= double.Parse(ds.Tables[0].Rows[i + j * 5][13].ToString());
                        up[
2= double.Parse(ds.Tables[0].Rows[i + j * 5][14].ToString());
                        down1[
0= double.Parse(ds.Tables[0].Rows[i+1+j*5][12].ToString());
                        down1[
1= double.Parse(ds.Tables[0].Rows[i + 1 + j * 5][13].ToString());
                        down1[
2= double.Parse(ds.Tables[0].Rows[i + 1 + j * 5][14].ToString());
                        down2[
0= double.Parse(ds.Tables[0].Rows[i + 2 + j * 5][12].ToString());
                        down2[
1= double.Parse(ds.Tables[0].Rows[i + 2 + j * 5][13].ToString());
                        down2[
2= double.Parse(ds.Tables[0].Rows[i + 2 + j * 5][14].ToString());
                        down3[
0= double.Parse(ds.Tables[0].Rows[i + 3 + j * 5][12].ToString());
                        down3[
1= double.Parse(ds.Tables[0].Rows[i + 3 + j * 5][13].ToString());
                        down3[
2= double.Parse(ds.Tables[0].Rows[i + 3 + j * 5][14].ToString());
                    }

                    
catch(System.FormatException e4)
                    
{}
                    Ij2 
= CalcAngel(down1, up, "-"false);
                    ds.Tables[
0].Rows[i + 1 + j * 5][15= Ij2[0];
                    ds.Tables[
0].Rows[i + 1 + j * 5][16= Ij2[1];
                    ds.Tables[
0].Rows[i + 1 + j * 5][17= Ij2[2];
                    Ij2 
= CalcAngel(down2, up, "-"false);
                    ds.Tables[
0].Rows[i + 2 + j * 5][15= Ij2[0];
                    ds.Tables[
0].Rows[i + 2 + j * 5][16= Ij2[1];
                    ds.Tables[
0].Rows[i + 2 + j * 5][17= Ij2[2];
                    Ij2 
= CalcAngel(down3, up, "-"false);
                    ds.Tables[
0].Rows[i + 3 + j * 5][15= Ij2[0];
                    ds.Tables[
0].Rows[i + 3 + j * 5][16= Ij2[1];
                    ds.Tables[
0].Rows[i + 3 + j * 5][17= Ij2[2];
                }

                
this.dataGridView1.DataSource = this.ds.Tables[0];
                
this.tabPage1.Show();
            }

        
//calc L-R-180
        private void button4_Click(object sender, EventArgs e)
        
{
            
double[] L = new double[3];
            
double[] R = new double[3];
            
double[] Ij = new double[3];
            
for (i = 4; i < this.ds.Tables[0].Rows.Count; i++)
            
{
                
if (this.ds.Tables[0].Rows[i][1].ToString() == "" || ds.Tables[0].Rows[i][1].ToString() == null)
                    
break;
                
try
                
{
                    L[
0= double.Parse(ds.Tables[0].Rows[i][2].ToString());
                    L[
1= double.Parse(ds.Tables[0].Rows[i][3].ToString());
                    L[
2= double.Parse(ds.Tables[0].Rows[i][6].ToString());
                    R[
0= double.Parse(ds.Tables[0].Rows[i][7].ToString());
                    R[
1= double.Parse(ds.Tables[0].Rows[i][8].ToString());
                    R[
2= double.Parse(ds.Tables[0].Rows[i][11].ToString());

                }

                
catch (System.FormatException e2)
                
{ }
                Ij 
= CalcAngel(R, Pi, "+"false);
                Ij 
= CalcAngel(L, Ij, "-"false);
                ds.Tables[
0].Rows[i][18= Ij[0];
                ds.Tables[
0].Rows[i][19= Ij[1];
                ds.Tables[
0].Rows[i][20= Ij[2];
                dataGridView1[
20, i].Style.BackColor = Color.CornflowerBlue;
            }

            
this.dataGridView1.DataSource = this.ds.Tables[0]; 
            
this.tabPage1.Show();
        }

        
//2C Variety
        private void button5_Click(object sender, EventArgs e)
        
{
            
double[] Max = new double[3];
            
double[] Min = new double[3];
            
double[] first = new double[3];
            
double[] second = new double[3];
            
double[] third = new double[3];
            
double[] forth = new double[3];
            
double[] fifth = new double[3];
            
double max = 0;
            
double min = 0;
            
double t_firt = 0;
            
double t_sec = 0;
            
double t_thir = 0;
            
double t_forth = 0;
            
double t_fif = 0;
            
int groupcount = 0;//返回组别个数(5个一组)
            for (i = 4; i < this.ds.Tables[0].Rows.Count; i++)
            
{
                
if (this.ds.Tables[0].Rows[i][1].ToString() == "" || ds.Tables[0].Rows[i][1].ToString() == null)
                
{
                    groupcount 
= i;
                    groupcount 
= (groupcount - 4/ 5;
                    
break;
                }

            }

            
//对每一组进行处理
            for (j = 0; j < groupcount; j++)
            
{
                i 
= 4;
                
try
                
{
                    first[
0= Math.Abs(double.Parse(ds.Tables[0].Rows[i + j * 5][18].ToString()));
                    first[
1= double.Parse(ds.Tables[0].Rows[i + j * 5][19].ToString());
                    first[
2= double.Parse(ds.Tables[0].Rows[i + j * 5][20].ToString());
                    second[
0= Math.Abs(double.Parse(ds.Tables[0].Rows[i + 1 + j * 5][18].ToString()));
                    second[
1= double.Parse(ds.Tables[0].Rows[i + 1 + j * 5][19].ToString());
                    second[
2= double.Parse(ds.Tables[0].Rows[i + 1 + j * 5][20].ToString());
                    third[
0= Math.Abs(double.Parse(ds.Tables[0].Rows[i + 2 + j * 5][18].ToString()));
                    third[
1= double.Parse(ds.Tables[0].Rows[i + 2 + j * 5][19].ToString());
                    third[
2= double.Parse(ds.Tables[0].Rows[i + 2 + j * 5][20].ToString());
                    forth[
0= Math.Abs(double.Parse(ds.Tables[0].Rows[i + 3 + j * 5][18].ToString()));
                    forth[
1= double.Parse(ds.Tables[0].Rows[i + 3 + j * 5][19].ToString());
                    forth[
2= double.Parse(ds.Tables[0].Rows[i + 3 + j * 5][20].ToString());
                    fifth[
0= Math.Abs(double.Parse(ds.Tables[0].Rows[i + 4 + j * 5][18].ToString()));
                    fifth[
1= double.Parse(ds.Tables[0].Rows[i + 4 + j * 5][19].ToString());
                    fifth[
2= double.Parse(ds.Tables[0].Rows[i + 4 + j * 5][20].ToString());
                    
                }

                
catch (System.FormatException e5)
                
{ }
                t_firt 
= first[0+ first[1/ 60 + first[2/ 3600;
                t_sec 
= second[0+ second[1/ 60 + second[2/ 3600;
                t_thir 
= third[0+ third[1/ 60 + third[2/ 3600;
                t_forth 
= forth[0+ forth[1/ 60 + forth[2/ 3600;
                t_fif 
= fifth[0+ fifth[1/ 60 + fifth[2/ 3600;
                
double[] temp = new double[] {t_firt,t_sec,t_thir,t_forth,t_fif };
                Array.Sort(temp);                
                max 
= temp[4];
                min 
= temp[0];
                
double temp2 = max - min;
                
double du= Math.Ceiling(temp2)-1;
                
double fen = Math.Ceiling((temp2 - du) * 60- 1;
                
double miao = Math.Round((temp2 - du - fen / 60* 3600,1);
                
if (miao == 60)
                
{
                    miao 
= 0;
                    fen 
+= 1;                    
                }

                
if (fen == 60)
                
{
                    fen 
= 0;
                    du 
+= 1;
                }

                ds.Tables[
0].Rows[i + j * 5][21= du;
                ds.Tables[
0].Rows[i + j * 5][22= fen;
                ds.Tables[
0].Rows[i + j * 5][23= miao;
                dataGridView1[
23, i+j*5].Style.BackColor = Color.CornflowerBlue;
            }

            
this.dataGridView1.DataSource = this.ds.Tables[0];
            
this.tabPage1.Show();
        }

        
//save data ,update datasource using dataset
        private void button6_Click(object sender, EventArgs e)
        
{
        
            
this.saveFileDialog1.Filter = "xls Files|.xls";
            
if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName != null)
            
{
                
try
                
{
                    PumpToExcel(saveFileDialog1.FileName, 
"aa.txt", ds,0);
                }

                
catch (System.Exception e7)
                
{
                    MessageBox.Show(e7.ToString());
                }

            }

            
        }

        
//Load Data from sheet1
        private void button10_Click(object sender, EventArgs e)
        
{
            
for (int k = 0; k < 6; k++)//共6个测回
            {
                ds.Tables[
1].Rows[4 + k][3= ds.Tables[0].Rows[5 + k*5][15];
                ds.Tables[
1].Rows[4 + k][4= ds.Tables[0].Rows[5 + k * 5][16];
                ds.Tables[
1].Rows[4 + k][5= ds.Tables[0].Rows[5 + k * 5][17];

                ds.Tables[
1].Rows[4 + k][6= ds.Tables[0].Rows[6 + k * 5][15];
                ds.Tables[
1].Rows[4 + k][7= ds.Tables[0].Rows[6 + k * 5][16];
                ds.Tables[
1].Rows[4 + k][8= ds.Tables[0].Rows[6 + k * 5][17];

                ds.Tables[
1].Rows[4 + k][9= ds.Tables[0].Rows[7 + k * 5][15];
                ds.Tables[
1].Rows[4 + k][10= ds.Tables[0].Rows[7 + k * 5][16];
                ds.Tables[
1].Rows[4 + k][11= ds.Tables[0].Rows[7 + k * 5][17];
            }

            
this.dataGridView2.DataSource = ds.Tables[1];
            
this.tabPage2.Show();
        }

        
//计算平均值
        private void button12_Click(object sender, EventArgs e)
        
{
            
            
            
for (i = 0; i < 3; i++)
            
{
                
double[] Sum = new double[] 000 };
                
for(j=0;j<6;j++)
                
{
                    
double[] sum = new double[3];
                    
try
                    
{
                        
                        sum[
0= double.Parse(ds.Tables[1].Rows[j + 4][3+i*3].ToString());
                        sum[
1= double.Parse(ds.Tables[1].Rows[j + 4][4+i*3].ToString());
                        sum[
2= double.Parse(ds.Tables[1].Rows[j + 4][5+i*3].ToString());
                    }

                    
catch (System.FormatException ee)
                    
{ }
                    Sum 
= CalcAngel(Sum, sum, "+"false);
                }

                Sum 
= DivAve(Sum,6);
                ds.Tables[
1].Rows[10][3 + i * 3]=Sum[0];
                ds.Tables[
1].Rows[10][4 + i * 3]=Sum[1];
                ds.Tables[
1].Rows[10][5 + i * 3= Sum[2];
            }

            dataGridView2.DataSource 
= ds.Tables[1];
            
this.tabPage2.Show();
        }

        
//计算delta变化值
        private void button11_Click(object sender, EventArgs e)
        
{
            
for (i = 0; i < 3; i++)
            
{
                
                
for (j = 0; j < 6; j++)
                
{
                    
double[] temp = new double[3];                 
                    
double[] A = new double[3];
                    
double[] B = new double[3];
                    
try
                    
{
                        A[
0= double.Parse(ds.Tables[1].Rows[4+j][3+i*3].ToString());
                        A[
1= double.Parse(ds.Tables[1].Rows[4 + j][4 + i * 3].ToString());
                        A[
2= double.Parse(ds.Tables[1].Rows[4 + j][5 + i * 3].ToString());
                        B[
0= double.Parse(ds.Tables[1].Rows[10][3+i*3].ToString());
                        B[
1= double.Parse(ds.Tables[1].Rows[10][4 + i * 3].ToString());
                        B[
2= double.Parse(ds.Tables[1].Rows[10][5 + i * 3].ToString());
                    }

                    
catch(System.FormatException e7)
                    
{}
                    temp 
= CalcAngel(A, B, "-"false);
                    temp[
2= Math.Round(temp[2], 1);
                    ds.Tables[
1].Rows[4 + j][14 + i] = temp[2];
                    
                }

            }

            dataGridView2.DataSource 
= ds.Tables[1];
            
this.tabPage2.Show();
        }

        
//计算列的平方和
        private void button9_Click(object sender, EventArgs e)
        
{
            
            
for (i = 0; i < 3; i++)
            
{
                
double temp = 0;
                
for (j = 0; j < 6; j++)
                
{
                    
double temp_1 = 0;
                    
try
                    
{
                        temp_1 
= double.Parse(ds.Tables[1].Rows[4 + j][14 + i].ToString());
                    }

                    
catch (System.FormatException ee)
                    
{ }
                    temp 
+= temp_1 * temp_1;
                }

                temp 
= Math.Round(temp, 2);
                ds.Tables[
1].Rows[10][14 + i] = temp;
            }
            
            
this.dataGridView2.DataSource = ds.Tables[1];
            
this.tabPage2.Show();
        }

        
//计算行的Delta之和
        private void button8_Click(object sender, EventArgs e)
        
{
            
for (i = 0; i < 6; i++)
            
{
                
double temp = 0;
                
for (j = 0; j < 3; j++)
                
{
                    
double temp_1 = 0;
                    
try
                    
{
                        temp_1 
= double.Parse(ds.Tables[1].Rows[i + 4][j + 14].ToString());
                    }

                    
catch (System.FormatException eee)
                    
{ }
                    temp 
+= temp_1; 
                }

                ds.Tables[
1].Rows[i + 4][17= temp;
            }

            
this.dataGridView2.DataSource = ds.Tables[1];
            
this.tabPage2.Show();
        }

        
//计算【VV】和mH
        private void button13_Click(object sender, EventArgs e)
        
{
            
double A = 0;
            
double B = 0;
            
double VV = 0;
            
double mH = 0;
            
for (i = 0; i < 3; i++)
            
{
                
double A_1 = 0;
                
try
                
{
                    A_1 
= double.Parse(ds.Tables[1].Rows[10][i + 14].ToString());
                }

                
catch (System.FormatException ee)
                
{ }
                A 
+= A_1;
            }

            
for (i = 0; i < 6; i++)
            
{
                
double B_1 = 0;
                
try
                
{
                    B_1 
= double.Parse(ds.Tables[1].Rows[i + 4][17].ToString());
                }

                
catch (System.FormatException eee)
                
{ }
                B_1 
= B_1 * B_1;
                B 
+= B_1;                
            }

            VV 
= A - B / 4;
            VV 
= Math.Round(VV, 2);
            mH 
= Math.Sqrt(VV / 15);
            mH 
= Math.Round(mH, 2);
            ds.Tables[
1].Rows[4][18= "[VV]=" + VV.ToString();
            ds.Tables[
1].Rows[5][18= "mH=" + mH.ToString();
            
this.dataGridView2.DataSource = ds.Tables[1];
            
this.tabPage2.Show();
        }

        
//保存数据到xls文件
        private void button7_Click(object sender, EventArgs e)
        
{
            
this.saveFileDialog1.Filter = "xls Files|.xls";
            
if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName != null)
            
{
                
try
                
{
                    PumpToExcel(saveFileDialog1.FileName, 
"aa.txt", ds,1);
                }

                
catch (System.Exception e7)
                
{
                    MessageBox.Show(e7.ToString());
                }

            }

        }

        
//计算表3平均值
        private void button14_Click(object sender, EventArgs e)
        
{
            
double L1 = 0;
            
double L2 = 0;
            
double R1 = 0;
            
double R2 = 0;
            
double Ave_L = 0;
            
double Ave_R = 0;
            
            
for (i = 4; i < ds.Tables[2].Rows.Count; i++)
            
{
                
if (ds.Tables[2].Rows[i][1].ToString() == "" || ds.Tables[2].Rows[i][1].ToString() == null)
                    
break;
                
try
                
{
                    L1 
= double.Parse(ds.Tables[2].Rows[i ][4].ToString());
                    L2 
= double.Parse(ds.Tables[2].Rows[i ][5].ToString());
                    R1 
= double.Parse(ds.Tables[2].Rows[i ][9].ToString());
                    R2 
= double.Parse(ds.Tables[2].Rows[i ][10].ToString());
                }

                
catch (System.FormatException ee)
                
{ }
                Ave_L 
= (L1 + L2) / 2;
                Ave_L
=Math.Round(Ave_L, 0);
                Ave_R 
= (R1 + R2) / 2;
                Ave_R 
= Math.Round(Ave_R, 0);
                
if ((((L1 > 50&& (L2 < 10)) || ((L2 > 50&& (L1 < 10))) && ((L1 + L2) > 60))
                
{
                    Ave_L 
= (L1 + L2 - 60/ 2;
                    Ave_L 
= Math.Round(Ave_L, 0);
                    
                }

                
if ((((L1 > 50&& (L2 < 10)) || ((L2 > 50&& (L1 < 10))) && ((L1 + L2) == 59))
                
{
                    Ave_L 
= 0;
                    
                }

                
if ((((L1 > 50&& (L2 < 10)) || ((L2 > 50&& (L1 < 10))) && ((L1 + L2) < 59))
                
{
                    Ave_L 
= (L1 + L2 + 60/ 2;
                    Ave_L 
= Math.Round(Ave_L, 0);                    
                }

                
if ((((R1 > 50&& (R2 < 10)) || ((R2 > 50&& (R1 < 10))) && ((R1 + R2) > 60))
                
{
                    Ave_R 
= (R1 + R2 - 60/ 2;
                    Ave_R 
= Math.Round(Ave_R, 0);
                    
                }

                
if ((((R1 > 50&& (R2 < 10)) || ((R2 > 50&& (R1 < 10))) && ((R1 + R2) == 59))
                
{
                    Ave_R 
= 0;
                   
                }

                
if ((((R1 > 50&& (R2 < 10)) || ((R2 > 50&& (R1 < 10))) && ((R1 + R2) < 59))
                
{
                    Ave_R 
= (R1 + R2 + 60/ 2;
                    Ave_R 
= Math.Round(Ave_R, 0);
                }

                ds.Tables[
2].Rows[i ][6= Ave_L;
                ds.Tables[
2].Rows[i ][11= Ave_R;
            }

            dataGridView3.DataSource 
= ds.Tables[2];
            
this.tabPage3.Show();
        }

        
//计算指标差Iij和Zij
        private void button15_Click(object sender, EventArgs e)
        
{
            
double[] L = new double[3];
            
double[] R = new double[3];
            
double[] Iij = new double[3];
            
double[] Zij = new double[3];
            
for (i = 4; i < ds.Tables[2].Rows.Count; i++)
            
{
                
if (ds.Tables[2].Rows[i][1].ToString() == "" || ds.Tables[2].Rows[i][1].ToString() == null)
                    
break;
                
try
                
{
                    L[
0= double.Parse(ds.Tables[2].Rows[i][2].ToString());
                    L[
1= double.Parse(ds.Tables[2].Rows[i][3].ToString());
                    L[
2= double.Parse(ds.Tables[2].Rows[i][6].ToString());
                    R[
0= double.Parse(ds.Tables[2].Rows[i][7].ToString());
                    R[
1= double.Parse(ds.Tables[2].Rows[i][8].ToString());
                    R[
2= double.Parse(ds.Tables[2].Rows[i][11].ToString());
                }

                
catch(System.FormatException e4)
                
{}
                Iij 
= CalcAngel(L, R, "+"false);
                Iij 
= CalcAngel(Iij, Circle, "-"true);
                ds.Tables[
2].Rows[i][12= Math.Round(Iij[2], 0);
                Zij 
= CalcAngel(L, Circle, "+"false);
                Zij 
= CalcAngel(Zij, R, "-"true);
                Zij[
2= Math.Round(Zij[2], 0);
                ds.Tables[
2].Rows[i][13= Zij[0];
                ds.Tables[
2].Rows[i][14= Zij[1];
                ds.Tables[
2].Rows[i][15= Zij[2];

            }

            dataGridView3.DataSource 
= ds.Tables[2];
            
this.tabPage3.Show();
        }

        
//保存表3
        private void button16_Click(object sender, EventArgs e)
        
{
            
this.saveFileDialog1.Filter = "xls Files|.xls";
            
if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName != null)
            
{
                
try
                
{
                    PumpToExcel(saveFileDialog1.FileName, 
"aa.txt", ds,2);
                }

                
catch (System.Exception e7)
                
{
                    MessageBox.Show(e7.ToString());
                }

            }

        }

        
//计算表4平均值Zi
        private void button21_Click(object sender, EventArgs e)
        
{
                       
            
for(j=0;j<5;j++)
            
{
                
double[] Sum = new double[3]; 
                
for (i = 0; i < 4; i++)
                
{
                    
double[] Z = new double[3];
                    
try
                    
{
                        Z[
0= double.Parse(ds.Tables[3].Rows[i+2][1+j*4].ToString());
                        Z[
1= double.Parse(ds.Tables[3].Rows[i + 2][2 + j * 4].ToString());
                        Z[
2= double.Parse(ds.Tables[3].Rows[i + 2][3 + j * 4].ToString());
                    }

                    
catch(System.FormatException ee)
                    
{}
                    Sum 
= CalcAngel(Sum, Z, "+"false);     
                }

                Sum 
= DivAve(Sum, 4);
                ds.Tables[
3].Rows[6][1 + j * 4= Sum[0];
                ds.Tables[
3].Rows[6][2 + j * 4= Sum[1];
                ds.Tables[
3].Rows[6][3 + j * 4= Sum[2];
            }

            dataGridView4.DataSource 
= ds.Tables[3];
            
this.tabPage4.Show();
        }

        
//LoadData
        private void button22_Click(object sender, EventArgs e)
        
{
            
for (i = 0; i < 5; i++)
            
{
                
for (j = 0; j < 4; j++)
                
{
                    ds.Tables[
3].Rows[j + 2][1+i*4= ds.Tables[2].Rows[i+ 4+j*5][13];
                    ds.Tables[
3].Rows[j + 2][2 + i * 4= ds.Tables[2].Rows[i+ 4 + j * 5][14];
                    ds.Tables[
3].Rows[j + 2][3 + i * 4= ds.Tables[2].Rows[i+ 4 + j * 5][15];
                }

            }

            dataGridView4.DataSource 
= ds.Tables[3];
            
this.tabPage4.Show();            
        }

        
//计算Vij
        private void button20_Click(object sender, EventArgs e)
        
{
            
double[] Z = new double[3];
            
double[] Zi = new double[3];
            
double[] result = new double[3];
            
for (i = 0; i < 5; i++)
            
{
                
try
                
{
                    Z[
0= double.Parse(ds.Tables[3].Rows[6][1+i*4].ToString());
                    Z[
1= double.Parse(ds.Tables[3].Rows[6][2 + i * 4].ToString());
                    Z[
2= double.Parse(ds.Tables[3].Rows[6][3 + i * 4].ToString());
                }

                
catch(System.FormatException ee){}
                
for (j = 0; j < 4; j++)
                
{
                    
try
                    
{
                        Zi[
0= double.Parse(ds.Tables[3].Rows[2+j][1 + i * 4].ToString());
                        Zi[
1= double.Parse(ds.Tables[3].Rows[2 + j][2 + i * 4].ToString());
                        Zi[
2= double.Parse(ds.Tables[3].Rows[2 + j][3 + i * 4].ToString());
                    }

                    
catch (System.FormatException eee) { }
                    result 
= CalcAngel(Zi, Z, "-"false);
                    ds.Tables[
3].Rows[2 + j][4 + i * 4= Math.Round(result[2], 1);
                }

            }

            dataGridView4.DataSource 
= ds.Tables[3];
            
this.tabPage4.Show(); 
        }

        
//计算Vij平方
        private void button18_Click(object sender, EventArgs e)
        
{                     
            
for (i = 0; i < 5; i++)
            
{
                
double Sum = 0;                
                
for (j = 0; j < 4; j++)
                
{
                    
double temp = 0;
                    
try
                    
{
                        temp 
= double.Parse(ds.Tables[3].Rows[2 + j][4 + i * 4].ToString());
                    }

                    
catch (System.FormatException eee) { }
                    temp 
*= temp;
                    Sum 
+= temp;
                    Sum 
= Math.Round(Sum, 2);
                }

                ds.Tables[
3].Rows[7][4 + i * 4= Sum;
            }

            dataGridView4.DataSource 
= ds.Tables[3];
            
this.tabPage4.Show();
        }

        
//计算mv
        private void button17_Click(object sender, EventArgs e)
        
{
            
double sum = 0;
            
double temp = 0;
            
double mv = 0;
            
for (i = 0; i < 5; i++)
            
{
                
try
                
{
                    temp
=double.Parse(ds.Tables[3].Rows[7][4 * i + 4].ToString());
                }

                
catch (System.FormatException ee) { }
                sum 
+= temp;
            }

            mv 
= Math.Sqrt(sum / 20);
            mv 
= Math.Round(mv, 2);
            ds.Tables[
3].Rows[8][1= mv;
            dataGridView4.DataSource 
= ds.Tables[3];
            
this.tabPage4.Show();
        }

        
//保存表4
        private void button19_Click(object sender, EventArgs e)
        
{
            
this.saveFileDialog1.Filter = "xls Files|.xls";
            
if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName != null)
            
{
                
try
                
{
                    PumpToExcel(saveFileDialog1.FileName, 
"aa.txt", ds, 3);
                }

                
catch (System.Exception e7)
                
{
                    MessageBox.Show(e7.ToString());
                }

            }

        }


    }


}

 

Sorry,here is the connect string to contact with datasource ,I wrote this sentence in Designer.cs.

 

private string strCon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + System.Windows.Forms.Application.StartupPath + "/source/经纬仪水平方向标准偏差及竖直角标准偏差数据.xls;" + "Extended Properties=Excel 8.0";

 

The Running Result Pics:

 

                                                                                                         Nasky

原创粉丝点击