本人用用ExecuteReader访问时间格式的列时,发现访问不到。
1)于是打开EXCEL文档,双击单元格后发现,日期发生了变化。比如2010-01-02变成了2010-1-2。
2)于是,将单无格切换成Text格式,发现列变成了40494这样的一串数字。
3)如果在该单元格输入文本格式的日期,就可以访问了。可见,是单元格格式的影响。
4)由于很多表格都是这种格式,而且以后也会有多种格式混合的状况,不知有没有好的办法根除呢?
最好不要用组件的那种,不稳定。附代码:  try
        {
            cn.Open();
            OleDbCommand cmd = new OleDbCommand(sqlSelect, cn);
            OleDbDataAdapter adpter = new OleDbDataAdapter();
            adpter.SelectCommand = cmd;
            System.Data.DataSet ds = new System.Data.DataSet();
            adpter.Fill(ds);
            //  reader = cmd.ExecuteReader();
            //  while (reader.Read())
            //  {
            //  lbFiles.Items.Add(reader[0].ToString());
            int count = ds.Tables[0].Rows.Count;
            if (count == 0) { Response.Write("<script>alert('无搜索结果!');window.close();</script>"); }
            GridView1.DataSource = ds.Tables[0];            GridView1.DataBind();
            //  }
        }
        catch
        {
            cn.Close();
            //     reader.Close();
        }
        finally
        {
            cn.Close();
            //  reader.Close();
        }

解决方案 »

  1.   

    判断格式啊LZ for (int r = 0; r < mydgv.Rows.Count; r++)
                    {
                        for (int i = 0; i < mydgv.ColumnCount; i++)
                        {
                            if (mydgv[i, r].ValueType == typeof(string))
                            {
                                worksheet.Cells[r + 2, i + 1] = "" + mydgv.Rows[r].Cells[i].Value;//将长数值转换成文本
                            }
                            else
                            {
                                worksheet.Cells[r + 2, i + 1] = mydgv.Rows[r].Cells[i].Value;
                            }
                        }
                        System.Windows.Forms.Application.DoEvents();
                    }
      

  2.   

    这行代码人""起什么作用?worksheet.Cells[r + 2, i + 1] = "" + mydgv.Rows[r].Cells[i].Value;//将长数值转换成文本