另外加分请大家不吝赐教

解决方案 »

  1.   

    http://www.dnbcw.com/biancheng/excel/ftvv80829.html
      

  2.   


    --like this:
    --fSourceFile is the excel file physical path.string ConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    "Data Source=" + fSourceFile + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";  
    OleDbConnection conn = new OleDbConnection(ConnectionStr);
    OleDbDataReader reader = null;
    try
    {
    conn.Open();
    DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] {null, null, null, "TABLE"}); string cmdstr = "SELECT * FROM ["+ dt.Rows[0]["TABLE_NAME"].ToString()+"]";   
    OleDbCommand cmd = new OleDbCommand(cmdstr, conn);//Using Data Reader:
    DataSet dst = new DataSet("ClaimSecData");
    innerData = new DataTable();
    dst.Tables.Add(innerData);
    innerData.TableName = "RawData";
    reader = cmd.ExecuteReader();  //all data columns defination can be found in this table
    DataTable colTable = reader.GetSchemaTable();    //read data from file
      while(reader.Read())
    {
    DataRow rRaw = innerData.NewRow();
      ....
      }  ... after data is read, you can do what ever you need ....
      
    再给你几个参考链接:
    http://blog.csdn.net/cpp2017/archive/2008/04/02/2244368.aspx 
    http://blog.csdn.net/cpp2017/archive/2008/04/02/2245276.aspx 
    http://blog.csdn.net/cpp2017/archive/2008/04/02/2245396.aspx http://www.icgbbs.com
      

  3.   


    --这个也许更明确点:
    /// 上传Excel文件
            /// </summary>
            /// <param name="inputfile">上传的控件名</param>
            /// <returns></returns>
            private string UpLoadXls(System.Web.UI.HtmlControls.HtmlInputFile inputfile)
             {
                string orifilename = string.Empty;
                string uploadfilepath = string.Empty;
                string modifyfilename = string.Empty;
                string fileExtend = "" ;//文件扩展名
                int fileSize = 0;//文件大小
                try
                 {
                    if(inputfile.Value != string.Empty)
                     {
                        //得到文件的大小
                         fileSize = inputfile.PostedFile.ContentLength;
                        if(fileSize == 0 )
                         {
                            throw new Exception("导入的Excel文件大小为0,请检查是否正确!");
                         }
                        //得到扩展名
                         fileExtend = inputfile.Value.Substring(inputfile.Value.LastIndexOf(".")+1);
                        if(fileExtend.ToLower() != "xls")
                         {                        
                            throw new Exception("你选择的文件格式不正确,只能导入EXCEL文件!");
                         }
                        //路径
                         uploadfilepath = Server.MapPath("~/Service/GraduateChannel/GraduateApply/ImgUpLoads");
                        //新文件名
                         modifyfilename = System.Guid.NewGuid().ToString();
                         modifyfilename += "."+inputfile.Value.Substring(inputfile.Value.LastIndexOf(".")+1);
                        //判断是否有该目录
                         System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(uploadfilepath);                    
                        if (!dir.Exists)
                         {
                             dir.Create();
                         }
                         orifilename = uploadfilepath+"\\"+modifyfilename;
                        //如果存在,删除文件
                        if(File.Exists(orifilename))
                         {
                             File.Delete(orifilename);
                         }
                        // 上传文件
                         inputfile.PostedFile.SaveAs(orifilename);
                     }
                    else
                     {
                        throw new Exception("请选择要导入的Excel文件!");                        
                     }
                 }
                catch(Exception ex)
                 {
                    throw ex;
                 }
                return orifilename;
             }
            
            /// <summary>
            /// 将Dataset的数据导入数据库
            /// </summary>
            /// <param name="pds">数据集</param>
            /// <param name="Cols">数据集列数</param>
            /// <returns></returns>
            private bool AddDatasetToSQL(DataSet pds,int Cols)
             {
                int ic,ir;
                 ic = pds.Tables[0].Columns.Count;
                if (pds.Tables[0].Columns.Count < Cols)
                 {
                    throw new Exception("导入Excel格式错误!Excel只有" + ic.ToString() + "列");                
                 }
                 ir = pds.Tables[0].Rows.Count;
                if (pds != null && pds.Tables[0].Rows.Count > 0)
                 {
                    for (int i = 1;i < pds.Tables[0].Rows.Count;i++)
                     {
                         Add(pds.Tables[0].Rows[i][1].ToString(),
                             pds.Tables[0].Rows[i][2].ToString(),pds.Tables[0].Rows[i][3].ToString(),
                             pds.Tables[0].Rows[i][4].ToString(),pds.Tables[0].Rows[i][5].ToString(),
                             pds.Tables[0].Rows[i][6].ToString(),pds.Tables[0].Rows[i][7].ToString(),
                             pds.Tables[0].Rows[i][8].ToString(),pds.Tables[0].Rows[i][9].ToString(),
                             pds.Tables[0].Rows[i][10].ToString(),pds.Tables[0].Rows[i][11].ToString(),
                             pds.Tables[0].Rows[i][12].ToString(),pds.Tables[0].Rows[i][13].ToString());
                     }
                 }
                else
                 {                
                    throw new Exception("导入数据为空!");    
                 }
                return true;
             }