http://www.soasp.net/FilePage/200806/20080610212625.htm不是用fileload座的,但实现了相同的功能

解决方案 »

  1.   

                string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + url + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1;\"";
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                string tableName = dt.Rows[0][2].ToString().Trim();
                string strSql = @"Select * from [" + tableName + "]";
                OleDbCommand cmd = new OleDbCommand(strSql, conn);
                OleDbDataAdapter oledar = new OleDbDataAdapter(cmd);
                DataSet ds = new DataSet();
                oledar.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
      

  2.   

    SQL Server 2005 中提供了. Excel倒入数据到SQL Server中啊...
      

  3.   


    //连接串string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + [EXCEL文件,含路径] + ";";OleDbConnection conn = new OleDbConnection(strConn); conn.Open();          DataTable dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null, null, null, "TABLE"});DataSet ds = new DataSet();//一个EXCEL文件可能有多个工作表,遍历之foreach( DataRow dr in dtSchema.Rows ){   string table = dr["TABLE_NAME"].ToString();   string strExcel = "SELECT * FROM [" + table + "]";   ds.Tables.Add(table);    OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel,conn);    myCommand.Fill(ds,table);} conn.Close(); 这样,读取出来的数据
      

  4.   

    http://support.microsoft.com/kb/321686/zh-cn
      

  5.   

    用ADO.NET。代码如下:
    //连接串
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + [EXCEL文件,含路径] + ";";
    OleDbConnection conn = new OleDbConnection(strConn); 
    conn.Open();
    DataTable dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null, null, null, "TABLE"});
    DataSet ds = new DataSet();
    //一个EXCEL文件可能有多个工作表,遍历之
    foreach( DataRow dr in dtSchema.Rows )
    {
       string table = dr["TABLE_NAME"].ToString();
       string strExcel = "SELECT * FROM [" + table + "]";
       ds.Tables.Add(table);
       OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel,conn); 
       myCommand.Fill(ds,table);
    }
    conn.Close();这样,读取出来的数据就藏在DataSet里了。
    采用这种方式,数据库所在机器不必装有EXCEL。
      

  6.   


            /// <summary>
            /// 导入数据
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnAdd_Click(object sender, EventArgs e)
            {
                if (this.IsValid)
                {
                    //this.fileUploadPicTitle.PostedFile.FileName 是本地的绝对路径
                    string fullFileName = this.fileUploadExcel.PostedFile.FileName;
                    //取得文件名 从全路径的最后一个\往后取字符
                    //string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);
                    string fileName = "";
                    //判断扩展名
                    string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);
                    if (type == "xls")
                    {
                        //上传文件可以用文件的原始名称
                        //我们通常的习惯是让程序自动生成不重复的文件名
                        //由年月日时分秒以及三位随机数组成文件名                    //用时间和随机数生成一个文件名
                        Random objRandom;
                        objRandom = new Random();
                        int m;
                        //生成一个大于1000小于9999的随机数
                        m = objRandom.Next(1000, 9999);
                        //把文件名设置为日期的年月日和时间加上随机数再防止重名
                        fileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + m.ToString();                    //上传文件
                        //这里是传到网站根目录下的uploadfile文件夹下,所以使用了 MapPath("/uploadfile") 这样的路径 如果不写 / 则是上传到当前目录的 uploadfile 目录下
                        //设置目录 用年加月做文件夹名称
                        string strDate = System.DateTime.Now.ToString("yyyyMM");                    try
                        {
                            string fileDirectory = Server.MapPath("/UploadFile/") + strDate;
                            //如果不存在此目录 则创建
                            if (!System.IO.Directory.Exists(fileDirectory))
                            {
                                System.IO.Directory.CreateDirectory(fileDirectory);
                            }
                            this.fileUploadExcel.PostedFile.SaveAs(fileDirectory + "/" + fileName + "." + type);
                            //this.lblUploadMsg.Text = "上传成功!";
                            //把输入框的文本设置为上传文件的文件名
                            //this.txtPicTitle.Text = strDate + "/" + fileName + "." + type;
                        }
                        catch
                        {
                            QYFY.Common.MsgBox.Alert("上传文件失败!请重新操作!", Request.Url.ToString());
                        }
                        //获取文件路径,导入数据
                        //try
                        //{
                            string FilePath = "/UploadFile/" + strDate + "/" + fileName + "." + type;
                            this.DataInput(FilePath);
                        //}
                        //catch
                        //{
                        //    QYFY.Common.MsgBox.Alert("读取数据失败!请重新操作!", Request.Url.ToString());
                        //}                }
                    else
                    {
                        QYFY.Common.MsgBox.Alert("文件格式错误!后缀名必须为xls");
                        return;
                    }
                }
            }        /// <summary>
            /// 导入入库数据
            /// </summary>
            /// <param name="FilePath">入库文件路径</param>
            private void DataInput(string FilePath)
            {
                OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(FilePath) + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';");
                conn.Open();
                string strSQL = "select * from [sheet1$]";
                OleDbCommand cmd = new OleDbCommand(strSQL, conn);
                OleDbDataReader odr = cmd.ExecuteReader();                        
                while (odr.Read())
                {
                    if (odr["物资编号"].ToString() != "")
                    {
                        //添加物资入库详细记录
                        try
                        {
                            QYFY.Model.Main.StoreDetail mStoreDetail = new QYFY.Model.Main.StoreDetail();
                            mStoreDetail.AmountOfMoney = decimal.Parse(odr["成本金额"].ToString());
                            mStoreDetail.MaterialCode = odr["物资编号"].ToString();
                            mStoreDetail.SquadID = squadID;
                            mStoreDetail.StoreNoteID = intStoreNoteID;
                            mStoreDetail.UnitPrice = decimal.Parse(odr["成本价"].ToString());
                            mStoreDetail.VaryNumber = int.Parse(odr["数量"].ToString());
                            mStoreDetail.VaryType = "1";
                            bllStoreDetail.Add(mStoreDetail);                                            }
                        catch
                        {
                            QYFY.Common.MsgBox.Alert("导入数据错误!请检查数据格式", Request.Url.ToString());
                        }                    
                    }
                }            odr.Close();
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }