导入代码如下
  public System.Data.DataTable ExcelToDataTable(string strExcelFileName)
        {
            string sheet = GetExcelSheetNames(strExcelFileName);
                //根据路径打开一个Excel文件并将数据填充到DataSet中
             string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strExcelFileName + ";Extended Properties = 'Excel 12.0 Xml;HDR=yes'";
                string strExcel = "";
                strExcel = "select  * from   [sheet1$]";
                OleDbConnection conn = null;
                DataSet ds = new DataSet();
             try
            {   using (conn = new OleDbConnection(strConn))
                {
                    conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
                    adapter.Fill(ds, "table1");
                    conn.Close();
                }
            return ds.Tables["table1"];
            }
            catch(Exception)
            {
                throw;
            }
             finally
             {
                 // Clean up.
                 if (conn != null)
                 {
                     conn.Close();
                     conn.Dispose();
                 }
                 if (ds != null)
                 {
                     ds.Dispose();
                 }
             }
                
           
        }
利用如上的代码  将excel导入到datagridview   
如果excel中的日期格式为“2009年3月5日”,导入进去后就变成了“2009-3-5”,如何才能让导入的格式不发生变化仍然保持为“2009年3月5日”!谢谢各位!!!

解决方案 »

  1.   

    自己再转一下,可以format一下
                dataGridView1.Columns[1].DefaultCellStyle.Format = "yyyy年m月d日";
      

  2.   

    dataGridView1.Columns["columnName"].DefaultCellStyle.Format = "yyyy年MM月dd日"; 
      

  3.   

    还不行的话,就转完了,在强制转换一下,table也可以这么转
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells["time"].Value = DateTime.Parse(dataGridView1.Rows[i].Cells["time"].Value.ToString()).ToString("yyyy年m月d日");
                }
      

  4.   

      for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells["time"].Value = DateTime.Parse(dataGridView1.Rows[i].Cells["time"].Value.ToString()).ToString("yyyy年m月d日");
                }