首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

啊EXCEL导入datagridview, 日期显示的是一串字母。咋回事啊代码见内

2013-07-04 
啊,求助,EXCEL导入datagridview, 日期显示的是一串字母。。。怎么回事啊?代码见内。导入Excelprivate void but

啊,求助,EXCEL导入datagridview, 日期显示的是一串字母。。。怎么回事啊?代码见内。
导入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();
        }

[解决办法]
    //建议用这种方式读取EXCEL  (excel版本为2007)
  public System.Data.DataTable Open(string filePath, string sheetName)
        {



            if (!sheetName.Contains("$"))
            {
                sheetName = sheetName + "$";
            }
          
            OleDbConnection connection = null;
            try
            {
                DataSet ds = new DataSet();             
                string strExcelNew = "";
                strExcelNew = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties="Excel 12.0;HDR=YES;IMEX=1"", filePath);
               
                connection = new OleDbConnection(strExcelNew);
                connection.Open();
                OleDbDataAdapter adapter = new OleDbDataAdapter("select * from [" + sheetName + "]", connection);
                adapter.SelectCommand.CommandTimeout = 0;
                adapter.Fill(ds, sheetName);
                return ds.Tables[0];
            }
            catch (Exception ex) {
}

//调用
  private void button3_Click(object sender, EventArgs e)
       {    
string filepath="文件路径名";  
string sheetname="文件Sheet页名";      
      dataGridView1.DataSource = Open(filepath,sheetname)
       }

接分了!!!!
[解决办法]
读取存储时将日期格式化

热点排行