DataSet ds = new DataSet();
myCommand.Fill(ds, "newtable");
我的数据都已经存放在ds内,并且数据的记录数和excel一致。问题出现在从ds里面读取数据的时候
for (int i = 0; i < ds.Tables["newtable"].Rows.Count; i++)
{
我是循环读取。
抓取数据的代码是ds.Tables["newtable"].Rows[i][2].ToString()。这个只能读取60000001这样的数据,而字母开头的无法读取,为什么?比如A0000001。
}

解决方案 »

  1.   

     string fileName = System.IO.Path.GetFileName(File1.PostedFile.FileName);
                string filePath = "";
                if (this.File1.Value == "")
                {
                    // Comm.Jscript.Alert("请先选择您要导入的文件!"); 
                }
                else
                {
                    int index = fileName.LastIndexOf(".");
                    if (index > 0)
                    {
                        if (fileName.Substring(index) == ".xls")
                        {
                            DateTime now = DateTime.Now;
                            fileName = now.ToShortDateString() + now.ToLongTimeString();
                            fileName = fileName.Replace("-", "").Replace(":", "").Replace("   ", "");
                            filePath = @"../Example/uploads/" + fileName + ".xls"; 
                            this.File1.PostedFile.SaveAs(Server.MapPath(filePath));                    }
                        else
                        {
                            //Comm.Jscript.Alert("读入的文件不是XLS"); 
                        }
                    }
                }
                if(filePath   !=   "") 

    fileName   =   Request.MapPath(filePath); 
    // string    strConn ="Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Dir + "\\"+fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\"
    string   strConn   =   "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+  fileName   +";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"; 
    string   query   =   "SELECT   *   FROM   [Sheet1$]"
                            ; 
    OleDbCommand   oleCommand   =   new   OleDbCommand(query,   new   OleDbConnection(strConn)); 
    OleDbDataAdapter   oleAdapter   =   new   OleDbDataAdapter(oleCommand); 
    DataSet   myDataSet   =   new   DataSet();  //   将   Excel   的[Sheet1]表内容填充到   DataSet   对象 
    try 

    oleAdapter.Fill(myDataSet,"[Sheet1$]"); 
    //   数据绑定 
    this.GridView1.DataSource   =   myDataSet;
                            this.GridView1.DataMember = "[Sheet1$]";
                            this.GridView1.DataBind();
                            this.GridView1.Visible = true; 
    this.btnSave.Visible   =   true; 

    catch(Exception   exx) 

    Response.Write(exx.Message); 
    //Comm.Jscript.Alert("注意:请用默认的Sheet1$页名称!"); 

    finally 

    if(File.Exists(filePath)) 

    File.Delete(filePath); 




    }代码给你了,自己调试吧
      

  2.   

    感谢你对我帖子的回复!我读取excel数据能读到,我就是不明白为什么字母开头的无法读取呢?为啥呢?难道是传说中的“喂小米”吗?
      

  3.   

    HDR(YES/ON/TRUE/1/NO/OFF/FALSE/0)設定是否將Excel表中第一行的內容做為字段,默認為是; 
    IMEX設定是否將混合型做為文本讀取。0-輸出模式;1-輸入模式;2-鏈接模式(完全更新能力)。 
      

  4.   

    因為那一列既有數值型又有文本型,屬於混合類型,所以只能讀到與第一行類型相同的數據。設置成IMEX=1可以解決
      

  5.   

    我就不在感谢了。我们设置excel单元格格式的时候,是统一设置成文本格式的。
      

  6.   

    冒昧的问下,IMEX在哪设置?是程序中吗?还是excel
      

  7.   

    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFilePath + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
    OleDbConnection conn = new OleDbConnection(strConn);
    conn.Open();DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    string strSheetName = dt.Rows[0][2].ToString().Trim();string strExcel = "select * from [" + strSheetName + "]";
    OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
    adapter.Fill(ds, strSheetName);
    conn.Close();//IMEX:将该列作为文字列  
    因为你第一行该列为数字,所以自动将其作为数字处理
    好像IMEX也不能保证都正确,设置IMEX后,根据前8行中所占比例来确定该列类型,如果数字比列占多,那么也会将该列作为数字来处理,所以也不是万全之策
      

  8.   

    把Excel的Sheet作为表来处理,就会遇到这样的问题,我也遇到过,一直没有解决
    后来我通过Range先把数据转换成数组,再来处理的