导入Excel
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Excel文件(*.xls)|*.xls";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string filename = ofd.FileName;
                ExcelToDataGridView(filename);
            }
        }        private void ExcelToDataGridView(string filename)
        {
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook workbook;
            Microsoft.Office.Interop.Excel.Worksheet worksheet;            object oMissing = System.Reflection.Missing.Value;            workbook = excel.Workbooks.Open(filename, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);            worksheet = (Worksheet)workbook.Worksheets[1];            int rowCount = worksheet.UsedRange.Rows.Count;
            int colCount = worksheet.UsedRange.Columns.Count;
            Microsoft.Office.Interop.Excel.Range range1;            System.Data.DataTable dt = new System.Data.DataTable();            for (int i = 0; i <= colCount; i++)
            {
                try
                {
                    range1 = worksheet.get_Range((object)worksheet.Cells[1, i + 1], (object)worksheet.Cells[1, i + 1]);
                    dt.Columns.Add(range1.Value2.ToString());                }
                catch (Exception)
                {                }
            }
            for (int j = 1; j < rowCount; j++)
            {
                DataRow dr = dt.NewRow();
                for (int i = 0; i <= colCount; i++)
                {
                    try
                    {
                        range1 = worksheet.get_Range((object)worksheet.Cells[j + 1, i + 1], (object)worksheet.Cells[j + 1, i + 1]);
                        dr[i] = range1.Value2.ToString();
                    }
                    catch (Exception)
                    { }
                }                dt.Rows.Add(dr);
            }
            dataGridView1.DataSource = dt;
            //for (int i = 0; i < dataGridView1.Rows.Count; i++)
            //{
            //   dataGridView1.Rows[i].Cells[5].Value = DateTime.Parse(dataGridView1.Rows[i].Cells[5].Value.ToString()).ToString("yyyy年m月d日");
            //}
            excel.Quit();
        }

解决方案 »

  1.   

    我把这个再从DATAGRIDVIEW导出到EXCEL中,又是日期。
    就到了DATAGRIDVIEW中是一串数字我是新手
      

  2.   

    以下是存入数据库 
    //datagridview存入数据库
          private void button3_Click(object sender, EventArgs e)
           {            
          fninsert();
           }
          public void fninsert()     
           {
               cZcMain zcMain = new cZcMain();
                   string a = string.Empty;
                   string b = string.Empty;
                   string c = string.Empty;
                   string d = string.Empty;
                   string e = string.Empty;
                   string f = string.Empty;               foreach (DataGridViewRow r in dataGridView1.Rows)
                   {
                       a = r.Cells[0].Value.ToString();
                       b = r.Cells[1].Value.ToString();
                       c = r.Cells[2].Value.ToString();
                       d = r.Cells[3].Value.ToString();
                       e = r.Cells[4].Value.ToString();
                       f = r.Cells[5].Value.ToString();
                       insert(a, b, c, d, e, f);//插入方法               }
           
          }
          public void insert(string a, string b, string c, string d, string e, string f)
          {          StringBuilder strSql = new StringBuilder();
              strSql.Append("insert into excel(name,tel,dep,yunyins,chongzhi,time) values (@a,@b,@c,@d,@e,@f)");                        
              SqlParameter[] parameters = { new SqlParameter("@a",SqlDbType.VarChar,50),                                          
                                          new SqlParameter("@b", SqlDbType.VarChar,50),                                          
                                          new SqlParameter("@c", SqlDbType.VarChar,50),
                                          new SqlParameter("@d", SqlDbType.VarChar,50),  
                                          new SqlParameter("@e", SqlDbType.VarChar,50),  
                                          new SqlParameter("@f", SqlDbType.VarChar,50),                                         
                                       };          parameters[0].Value = a;
              parameters[1].Value = b;
              parameters[2].Value = c;
              parameters[3].Value = d;
              parameters[4].Value = e;
              parameters[5].Value = f;
            string xsr = strSql.ToString();
              if (fnExecuteNonQuery(xsr.ToString(), parameters))  
              {                 
                  MessageBox.Show("已经成功导入数据库!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
              }      }
          public bool fnExecuteNonQuery(string xsr, SqlParameter[] parameters)
          {
              SqlConnection conn = new SqlConnection("Server=xxx;DataBase=xxx;User=xxx;Password=xxxx");//按照你的数据库配置连接           
              conn.Open();          SqlCommand sqlCom = new SqlCommand();
              {
                  sqlCom.Connection = conn;
                  sqlCom.CommandText = xsr.ToString();
                  foreach (SqlParameter sqlP in parameters)
                  {
                      sqlCom.Parameters.Add(sqlP);
                  }
                  try
                  {
                      sqlCom.ExecuteNonQuery();
                  }
                  catch (Exception e1)
                  {
                      conn.Close();
                      throw new Exception(e1.Message);                  //  return false;               
                  }
              }
              conn.Close();
              return true;
          }    }
    }