从datagridview com到Excel时,我的数据会被更改

时间:2014-01-19 16:53:17

标签: c# datagridview

我有程序,但是当我想从datagridview将它们发送到Excel时,有些列可以在Excel中更改

如果有朋友可以帮助我,我将不胜感激。

您可以在下面看到代码

Colm Cm-1 Till Rm-12

中的上述问题

它显示在Excel 0,1,2,3,......

private void button1_Click(object sender, EventArgs e)//Export To Exel
{
        OleDbConnection ocon;
        OleDbCommand ocom;
        OleDbDataAdapter oda;
        OleDbCommandBuilder ocb;



        saveFileDialog1.ShowDialog();

        ocon = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + saveFileDialog1.FileName + "; Extended Properties=Excel 8.0;");
        ocom = ocon.CreateCommand();

        ocon.Open();
        ocom.CommandText = "CREATE table [Consumption](Geo_Market char(20),location char(20),segment_id char(20),Parent_store_Name char(20),store_Name char(20),store_Level char(20),Sub_Sub_Segment char(20),part_no char(20),Part_Description char(20),part_Type char(20),QTY Integer,UOM char(20),price Integer,Averag_Stock_Value Integer,min_level Integer,max_level  Integer,part_location char(20),[CM-1] Integer,[CM-2] Integer,[CM-3] Integer,[CM-4] Integer,[CM-5] Integer,[CM-6] Integer,[CM-7] Integer,[CM-8] Integer,[CM-9] Integer,[CM-10] Integer,[CM-11] Integer,[CM-12] Integer,[RM-1] Integer,[RM-2] Integer,[RM-3] Integer,[RM-4] Integer,[RM-5] Integer,[RM-6] Integer,[RM-7] Integer,[RM-8] Integer,[RM-9] Integer,[RM-10] Integer,[RM-11] Integer,[RM-12] Integer,last_issue_date Char(20),Excess_qty Integer,Excess_value Integer,Slow_Move char(20))";

        ocom.ExecuteNonQuery();
        ocom.Parameters.Add("Geo_Market", OleDbType.Char);//0
        ocom.Parameters.Add("location", OleDbType.Char);//1
        ocom.Parameters.Add("segment_id", OleDbType.Char);//2
        ocom.Parameters.Add("Parent_store_Name", OleDbType.Char);//3
        ocom.Parameters.Add("store_Name", OleDbType.Char);//4
        ocom.Parameters.Add("store_Level", OleDbType.Char);//5
        ocom.Parameters.Add("Sub_Sub_Segment", OleDbType.Char);//6
        ocom.Parameters.Add("part_no", OleDbType.Char);//7
        ocom.Parameters.Add("Part_Description", OleDbType.Char);//8
        ocom.Parameters.Add("part_Type", OleDbType.Char);//9
        ocom.Parameters.Add("QTY", OleDbType.Integer);//10
        ocom.Parameters.Add("UOM", OleDbType.Char);//11
        ocom.Parameters.Add("price", OleDbType.Decimal);//12
        ocom.Parameters.Add("Averag_Stock_Value", OleDbType.Decimal);//13
        ocom.Parameters.Add("min_level", OleDbType.Integer);//14
        ocom.Parameters.Add("min_level", OleDbType.Integer);//15
        ocom.Parameters.Add("part_location", OleDbType.Char);//16
        ocom.Parameters.Add("CM-1", OleDbType.Integer);//17
        ocom.Parameters.Add("CM-2", OleDbType.Integer);//18
        ocom.Parameters.Add("CM-3", OleDbType.Integer);//19
        ocom.Parameters.Add("CM-4", OleDbType.Integer);//20
        ocom.Parameters.Add("CM-5", OleDbType.Integer);//21
        ocom.Parameters.Add("CM-6", OleDbType.Integer);//22
        ocom.Parameters.Add("CM-7", OleDbType.Integer);//23
        ocom.Parameters.Add("CM-8", OleDbType.Integer);//24
        ocom.Parameters.Add("CM-9", OleDbType.Integer);//25
        ocom.Parameters.Add("CM-10", OleDbType.Integer);//26
        ocom.Parameters.Add("CM-11", OleDbType.Integer);//27
        ocom.Parameters.Add("CM-12", OleDbType.Integer);//28
        ocom.Parameters.Add("RM-1", OleDbType.Integer);//29
        ocom.Parameters.Add("RM-2", OleDbType.Integer);//30
        ocom.Parameters.Add("RM-3", OleDbType.Integer);//31
        ocom.Parameters.Add("RM-4", OleDbType.Integer);//32
        ocom.Parameters.Add("RM-5", OleDbType.Integer);//33
        ocom.Parameters.Add("RM-6", OleDbType.Integer);//34
        ocom.Parameters.Add("RM-7", OleDbType.Integer);//35
        ocom.Parameters.Add("RM-8", OleDbType.Integer);//36
        ocom.Parameters.Add("RM-9", OleDbType.Integer);//37
        ocom.Parameters.Add("RM-10", OleDbType.Integer);//38
        ocom.Parameters.Add("RM-11", OleDbType.Integer);//39
        ocom.Parameters.Add("RM-12", OleDbType.Integer);//40
        ocom.Parameters.Add("last_issue_date", OleDbType.Char);//41
        ocom.Parameters.Add("Excess_qty", OleDbType.Integer);//42
        ocom.Parameters.Add("Excess_value", OleDbType.Integer);//43
        ocom.Parameters.Add("Slow_Move", OleDbType.Char);//44




        //// چون که اکسپور شود  دکمه گو را  توی  Go buttom ra dar yek dat2 ke dar bala creat kardam rikhtam  

        for (int dr = 0; dr < dataGridView1.Rows.Count - 1; dr++)
        {


            ocom.Parameters[0].Value = (string)dataGridView1.Rows[dr].Cells[0].Value;
            ocom.Parameters[1].Value = (string)dataGridView1.Rows[dr].Cells[1].Value;
            ocom.Parameters[2].Value = (string)dataGridView1.Rows[dr].Cells[2].Value;
            ocom.Parameters[3].Value = (string)dataGridView1.Rows[dr].Cells[3].Value;
            ocom.Parameters[4].Value = (string)dataGridView1.Rows[dr].Cells[4].Value;
            ocom.Parameters[5].Value = (string)dataGridView1.Rows[dr].Cells[5].Value;
            ocom.Parameters[6].Value = (string)dataGridView1.Rows[dr].Cells[6].Value;
            ocom.Parameters[7].Value = (string)dataGridView1.Rows[dr].Cells[7].Value;
            ocom.Parameters[8].Value = (string)dataGridView1.Rows[dr].Cells[8].Value;//(Int64)
            ocom.Parameters[9].Value = (string)dataGridView1.Rows[dr].Cells[9].Value;//(Int64)
            ocom.Parameters[10].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[10].Value);//(int)
            ocom.Parameters[11].Value = (string)dataGridView1.Rows[dr].Cells[11].Value;//(int)
            ocom.Parameters[12].Value = Convert.ToDecimal(dataGridView1.Rows[dr].Cells[12].Value);
            ocom.Parameters[13].Value = Convert.ToDecimal(dataGridView1.Rows[dr].Cells[13].Value);
            ocom.Parameters[14].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[14].Value);
            ocom.Parameters[15].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[15].Value);
            ocom.Parameters[16].Value = (string)dataGridView1.Rows[dr].Cells[16].Value;
            ocom.Parameters[17].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[17].Value);//cm1
            ocom.Parameters[18].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[18].Value);
            ocom.Parameters[19].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[19].Value);
            ocom.Parameters[20].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[20].Value);
            ocom.Parameters[21].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[21].Value);
            ocom.Parameters[22].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[22].Value);
            ocom.Parameters[23].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[23].Value);
            ocom.Parameters[24].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[24].Value);
            ocom.Parameters[25].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[25].Value);
            ocom.Parameters[26].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[26].Value);
            ocom.Parameters[27].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[27].Value);
            ocom.Parameters[28].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[28].Value);
            ocom.Parameters[29].Value = (string)dataGridView1.Rows[dr].Cells[29].Value;
            ocom.Parameters[30].Value = (string)dataGridView1.Rows[dr].Cells[30].Value;
            ocom.Parameters[31].Value = (string)dataGridView1.Rows[dr].Cells[31].Value;
            ocom.Parameters[32].Value = (string)dataGridView1.Rows[dr].Cells[32].Value;
            ocom.Parameters[33].Value = (string)dataGridView1.Rows[dr].Cells[33].Value;
            ocom.Parameters[34].Value = (string)dataGridView1.Rows[dr].Cells[34].Value;
            ocom.Parameters[35].Value = (string)dataGridView1.Rows[dr].Cells[35].Value;
            ocom.Parameters[36].Value = (string)dataGridView1.Rows[dr].Cells[36].Value;
            ocom.Parameters[37].Value = (string)dataGridView1.Rows[dr].Cells[37].Value;
            ocom.Parameters[38].Value = (string)dataGridView1.Rows[dr].Cells[38].Value;
            ocom.Parameters[39].Value = (string)dataGridView1.Rows[dr].Cells[39].Value;
            ocom.Parameters[40].Value = (string)dataGridView1.Rows[dr].Cells[40].Value;
            ocom.Parameters[41].Value = (string)dataGridView1.Rows[dr].Cells[41].Value;
            ocom.Parameters[42].Value = Convert.ToInt32(dataGridView1.Rows[dr].Cells[42].Value);
            ocom.Parameters[43].Value = Convert.ToDecimal(dataGridView1.Rows[dr].Cells[43].Value);
            ocom.Parameters[44].Value = (string)dataGridView1.Rows[dr].Cells[44].Value;


            ocom.CommandText = ("Insert into [Consumption] (Geo_Market,location,segment_id,Parent_store_Name,store_Name,store_Level,Sub_Sub_Segment,part_no,Part_Description,part_Type,QTY,UOM,price,Averag_Stock_Value,min_level,max_level,part_location,[CM-1],[CM-2],[CM-3],[CM-4],[CM-5],[CM-6],[CM-7],[CM-8],[CM-9],[CM-10],[CM-11],[CM-12],[RM-1],[RM-2],[RM-3],[RM-4],[RM-5],[RM-6],[RM-7],[RM-8],[RM-9],[RM-10],[RM-11],[RM-12],last_issue_date,Excess_qty,Excess_value,Slow_Move) VALUES(@Geo_Market,@location,@segment_id,@Parent_store_Name,@store_Name,@store_Level,@Sub_Sub_Segment,@part_no,@Part_Description,@part_Type,@QTY,@UOM,@price,@Averag_Stock_Value,@min_level,@max_level,@part_location,@CM-1,@CM-2,@CM-3,@CM-4,@CM-5,@CM-6,@CM-7,@CM-8,@CM-9,@CM-10,@CM-11,@CM-12,@RM-1,@RM-2,@RM-3,@RM-4,@RM-5,@RM-6,@RM-7,@RM-8,@RM-9,@RM-10,@RM-11,@RM-12,@last_issue_date,@Excess_qty,@Excess_value,@Slow_Move)");

            ocom.ExecuteNonQuery();
        }

        ocon.Close();
        MessageBox.Show("Your Report Has creat And Exported To Excel");

    }

1 个答案:

答案 0 :(得分:0)

您可以尝试更改comum名称&#34; CM-1&#34;类似于&#34; CM_1&#34;?