多个GridView数据导入Excel中

来源:互联网 发布:防火墙如何开启403端口 编辑:程序博客网 时间:2024/04/20 03:02

             int i = 2;
             object filename = "Sheet.xls";
             string strConToexcel = "server=.;database=WildDrug;uid=sa;pwd=sa;";
             //string strComToexcel = "select   *   from   M_SpecimenEnt where   id='" +  + "'";
             SqlConnection conLoginToexcel = new SqlConnection(strConToexcel);
            // SqlCommand comLoginToexcel = new SqlCommand(strComToexcel, conLoginToexcel);

             SqlCommand myCommand = new SqlCommand("sp_selsum", conLoginToexcel);
             myCommand.CommandType = CommandType.StoredProcedure;
             myCommand.Parameters.Add("@ret", SqlDbType.Int ,4).Value = 0;
             myCommand.Parameters.Add("@errmsg", SqlDbType.VarChar , 100).Value = 0;
             myCommand.Parameters.Add("@curserid", SqlDbType.Int , 4).Value = adminID;

             conLoginToexcel.Open();
             SqlDataReader dtrToexcel = myCommand.ExecuteReader();
            // dtrToexcel.NextResult();
             //dtrToexcel.
      

             Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
             Microsoft.Office.Interop.Excel.Workbook xlWorkbook = excel.Application.Workbooks.Add(true);
             object objNull = System.Reflection.Missing.Value;


             excel.Cells[1, 1] = "地区名称";
             excel.Cells[1, 2] = "室外编号";
             excel.Cells[1, 3] = "调查地点";
             excel.Cells[1, 4] = "测区面积";
             excel.Cells[1, 5] = "群落名称";
             excel.Cells[1, 6] = "群落面积";
             excel.Cells[1, 7] = "经度";
             excel.Cells[1, 8] = "纬度";
             excel.Cells[1, 9] = "海拔";
             excel.Cells[1, 10] = "坡向";
             excel.Cells[1, 11] = "坡度";
             excel.Cells[1, 12] = "群落高";
             excel.Cells[1, 13] = "总盖度";
             excel.Cells[1, 14] = "突出的生态现象";
             excel.Cells[1, 15] = "小地形及样地周围环境";
             excel.Cells[1, 16] = "土壤及其他";
             excel.Cells[1, 17] = "人为影响方式和程度";
             excel.Cells[1, 18] = "填写时间";
             excel.Cells[1, 19] = "填报人";
             excel.Cells[1, 20] = "审核人";
            
             while (dtrToexcel.Read())
             {

                 excel.Cells[i, 1] = dtrToexcel[0].ToString();
                 excel.Cells[i, 2] = dtrToexcel[1].ToString();
                 excel.Cells[i, 3] = dtrToexcel[2].ToString();
                 excel.Cells[i, 4] = dtrToexcel[3].ToString();
                 excel.Cells[i, 5] = dtrToexcel[4].ToString();
                 excel.Cells[i, 6] = dtrToexcel[5].ToString();
                 excel.Cells[i, 7] = dtrToexcel[6].ToString();
                 excel.Cells[i, 8] = dtrToexcel[7].ToString();
                 excel.Cells[i, 9] = dtrToexcel[8].ToString();
                 excel.Cells[i, 10] = dtrToexcel[9].ToString();
                 excel.Cells[i, 11] = dtrToexcel[10].ToString();
                 excel.Cells[i, 12] = dtrToexcel[11].ToString();
                 excel.Cells[i, 13] = dtrToexcel[12].ToString();
                 excel.Cells[i, 14] = dtrToexcel[13].ToString();
                 excel.Cells[i, 15] = dtrToexcel[14].ToString();
                 excel.Cells[i, 16] = dtrToexcel[15].ToString();
                 excel.Cells[i, 17] = dtrToexcel[16].ToString();
                 excel.Cells[i, 18] = dtrToexcel[17].ToString();
                 excel.Cells[i, 19] = dtrToexcel[18].ToString();
                 excel.Cells[i, 20] = dtrToexcel[19].ToString();
                 i++;

             }
             i = i + 2;
             excel.Cells[i, 1] = "样方编号";
             excel.Cells[i, 2] = "平均生长年限";
             excel.Cells[i, 3] = "经济株数";
             excel.Cells[i, 4] = "总株数";
             excel.Cells[i, 5] = "湿重(经济)";
             excel.Cells[i, 6] = "干重(经济)";
             excel.Cells[i, 7] = "湿总重量";
             excel.Cells[i, 8] = "干总重量";
             excel.Cells[i, 9] = "茎    高";
             excel.Cells[i, 10] = "茎    粗";
             excel.Cells[i, 11] = "药    名";
             excel.Cells[i, 12] = "部位名称";

             i++;
             if (dtrToexcel.NextResult())
             {
                 while (dtrToexcel.Read())
                 {

                     excel.Cells[i, 1] = dtrToexcel[0].ToString();
                     excel.Cells[i, 2] = dtrToexcel[1].ToString();
                     excel.Cells[i, 3] = dtrToexcel[2].ToString();
                     excel.Cells[i, 4] = dtrToexcel[3].ToString();
                     excel.Cells[i, 5] = dtrToexcel[4].ToString();
                     excel.Cells[i, 6] = dtrToexcel[5].ToString();
                     excel.Cells[i, 7] = dtrToexcel[6].ToString();
                     excel.Cells[i, 8] = dtrToexcel[7].ToString();
                     excel.Cells[i, 9] = dtrToexcel[8].ToString();
                     excel.Cells[i, 10] = dtrToexcel[9].ToString();
                     excel.Cells[i, 11] = dtrToexcel[10].ToString();
                     excel.Cells[i, 12] = dtrToexcel[11].ToString();

                     i++;

                 }
                 i = i + 2;
                 if (dtrToexcel.NextResult())
                 {
                     excel.Cells[i, 1] = "平均生长年限";
                     excel.Cells[i, 2] = "平均株高(米)";
                     excel.Cells[i, 3] = "平均茎精(厘米)";
                     excel.Cells[i, 4] = "平均经济株密度(株/公顷)";
                     excel.Cells[i, 5] = "平均密度总数(株/公顷)";
                     excel.Cells[i, 6] = "平均经济株重(克/株)";
                     excel.Cells[i, 7] = "平均株总重(克/株)";
                     excel.Cells[i, 8] = "单位面积经济蕴藏量(公斤/公顷)";
                     excel.Cells[i, 9] = "单位面积蕴藏量总量(公斤/公顷)";
                 i++;
                 while (dtrToexcel.Read())
                 {

                     excel.Cells[i, 1] = dtrToexcel[0].ToString();
                     excel.Cells[i, 2] = dtrToexcel[1].ToString();
                     excel.Cells[i, 3] = dtrToexcel[2].ToString();
                     excel.Cells[i, 4] = dtrToexcel[3].ToString();
                     excel.Cells[i, 5] = dtrToexcel[4].ToString();
                     excel.Cells[i, 6] = dtrToexcel[5].ToString();
                     excel.Cells[i, 7] = dtrToexcel[6].ToString();
                     excel.Cells[i, 8] = dtrToexcel[7].ToString();
                     excel.Cells[i, 9] = dtrToexcel[8].ToString();
                    

                     i++;

                 }

                 }

             }
           
             excel.SaveWorkspace(filename);  
             excel.Application.Workbooks.Close();  
             excel.Application.Quit();
             excel.Quit();
 

 

原创粉丝点击