Excel转DataSet应用


 

ExcelDataSet应用



namespace exceldataset

{

    publicpartial class Index : System.Web.UI.Page

    {

       public DataSet myDataSet;

 

       public string FileName = string.Empty;

 

       public string FilePath = string.Empty;

       //sheet数组

       public ArrayList SheetNames = new ArrayList();

 

       protected void Page_Load(object sender, EventArgs e)

       {

           if (!IsPostBack)

           {

 

           }

       }

 

       public void FileLoad(string paramFileName, string paramFilePath)

       {

           this.FileName = paramFileName;

           this.FilePath = paramFilePath;

           myDataSet = new DataSet();

           this.Fill(myDataSet);

           #region 删除所有空行

           //由于导入excel底部容易产生空行,执行此处删除所有空行

           var emptyRows = (from r inmyDataSet.Tables[0].AsEnumerable()

                           where r.ItemArray.All(obj =>string.IsNullOrWhiteSpace(obj.ToString()))

                           select r).ToArray();

           foreach (DataRow dr in emptyRows)

           {

               myDataSet.Tables[0].Rows.Remove(dr);

           }

           myDataSet.Tables[0].AcceptChanges();

           #endregion

       }

 

 

       #region private void Fill(DataSet paramDataSet) 填充数据

       /// <summary>

       /// 填充数据

       /// </summary>

       /// <param name="paramDataSet">DataSet数据集</param>

       /// <param name="paramPath">路径</param>

       private void Fill(DataSet paramDataSet)

       {

           OleDbConnection myOleDbConnection = new OleDbConnection();

           string strCon = string.Empty;

           if (this.FilePath.EndsWith("xlsx"))

           {

               strCon =@"Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;DataSource=" + this.FilePath;

           }

           else

           {

               strCon =@"Provider=Microsoft.JET.OLEDB.4.0;Extended Properties=Excel 8.0;DataSource=" + this.FilePath;

           }

           //获取多sheet信息 Add By OuYang At2014-11-24 Start

           Microsoft.Office.Interop.Excel.ApplicationClass excelApp =new Microsoft.Office.Interop.Excel.ApplicationClass();

           Microsoft.Office.Interop.Excel.Workbooks wbs =excelApp.Workbooks;

           Microsoft.Office.Interop.Excel.Workbook wb =wbs.Open(this.FilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing,

           Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing,

           Type.Missing, Type.Missing, Type.Missing, Type.Missing);

           excelApp.Visible = false;

           int count = wb.Worksheets.Count;

           for (int i = 1; i <= count; i++)

           {

               SheetNames.Add(((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[i]).Name.ToLower());

           }

           wbs.Close();

           GC.Collect();

           try

           {

               myOleDbConnection = newOleDbConnection(strCon);

               myOleDbConnection.Open();

               //保留sheet1功能

               if (SheetNames.Contains("sheet1"))

               {

                   string strQuery = "SELECT * FROM [Sheet1$] ";

                   OleDbCommand myOleDbCommand =new OleDbCommand(strQuery, myOleDbConnection);

                   OleDbDataAdaptermyOleDbDataAdapter = new OleDbDataAdapter(myOleDbCommand);

                  myOleDbDataAdapter.Fill(paramDataSet);

               }

               else

               {

                   foreach (string sheetName inSheetNames)

                   {

                       string strQuery= " SELECT * FROM [" + sheetName + "$] ";

                       OleDbCommandmyOleDbCommand = new OleDbCommand(strQuery, myOleDbConnection);

                       OleDbDataAdaptermyOleDbDataAdapter = new OleDbDataAdapter(myOleDbCommand);

                      myOleDbDataAdapter.Fill(paramDataSet, sheetName);

                   }

               }

               paramDataSet.AcceptChanges();

               //获取多sheet信息 AddBy OuYang At 2014-11-24 End

           }

           catch (Exception myException)

           {

               throw myException;

           }

           finally

           {

               myOleDbConnection.Close();

               GC.Collect();

           }

       }

       #endregion

 

       protected void btnUpload_Click(object sender, EventArgs e)

       {

           string fileName = string.Empty;

           string filePath = string.Empty;

 

           fileName = "教师基本信息导入模版.xls";

           filePath = Server.MapPath("/ImportTemp/教师基本信息导入模版.xls");

 

           FileLoad(fileName, filePath);

 

           if (myDataSet.Tables[0].Rows.Count > 0)

           {

               this.Repeater1.DataSource =myDataSet.Tables[0].DefaultView;

               this.Repeater1.DataBind();

           }

       }

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三天不学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值