当前位置导航:炫浪网>>网络学院>>网页制作>>ASP.NET教程

Asp.net 保存并导入Excel数据

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
///


/// Excel操作基类
///

public class ExcelObj
{
 public ExcelObj()
 {

    }
    #region 保存并返回Excel中的数据
    ///


    /// 保存并返回Excel中的数据
    ///

    /// 保存的路径
    /// 上传控件
    ///
    public static DataSet SaveAndReturn(string SavePath, FileUpload myFileUpload)
    {
        DataSet myDs = new DataSet();
        string fileName = myFileUpload.FileName;
        if (fileName != "")
        {
            try
            {
                myFileUpload.SaveAs(SavePath + "\\" + fileName);
            }
            catch (Exception ee)
            {
                throw new Exception(ee.ToString());
            }
            string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '" + SavePath + "//" + fileName + "';Extended Properties=Excel 8.0";

            OleDbConnection cnnxls = new OleDbConnection(mystring);

            OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls);

            myDa.Fill(myDs);
        }
        return myDs;

     }
    #endregion
 }

相关内容
赞助商链接