控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

将Excel导入到数据库

作者:佚名 出处:互联网 2011年03月17日 阅读:

将Excel导入到数据库

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelToDataSet
{
    /// <summary>
    /// 目前测试的com是office2003
    /// 应用com组件:Microsoft Excel 11.0 Object Library
    /// </summary>
    public partial class ExcelToDataSet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        protected void btnInputExcel_Click(object sender, EventArgs e)
        {
            string FileType = this.FileUpExcel.PostedFile.ContentType;

            //if (FileType == "application/vnd.ms-excel") 
            if(FileType=="application/octet-stream")
            {
                this.GvExcel.DataSource = CreateDataSource();
                this.GvExcel.DataBind();
            }
            else
            {
                this.lbMsg.Text = "<font color='red'>请输入Excel文件!</font>";
            }

        }
        //创建数据源
        private DataSet CreateDataSource()
        {
            //客户端路径
            //string path = this.FileUpExcel.PostedFile.FileName;
            string path = (string)Session["path"];
            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0;";
            OleDbConnection olecon = new OleDbConnection(strCon);
            //OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strCon);
            OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [" + this.ddlExcelSheetName.SelectedValue + "$]", strCon);
            DataSet myds = new DataSet();
            myda.Fill(myds);
            return myds;
        }

        //加载sheet表名到dropdown中
        protected void AddSheetNameToDrop()
        {
            this.ddlExcelSheetName.Items.Clear();//清空DropDownList中的值
            if (this.FileUpExcel.HasFile)
            {
                string pathName = this.FileUpExcel.PostedFile.FileName;
                Session["path"] = pathName;
                object MissingValue = Type.Missing;
                Excel.Application ep = new Excel.ApplicationClass();
                Excel.Workbook ew = ep.Workbooks.Open(pathName, MissingValue,
                MissingValue, MissingValue, MissingValue,
                MissingValue, MissingValue, MissingValue,
                MissingValue, MissingValue, MissingValue,
                MissingValue, MissingValue, MissingValue,
                MissingValue);
                // Excel.Worksheet ews;
                int count = ew.Worksheets.Count;
                //读出表的数量
                //Response.Write(count.ToString());
                //遍历出excel文件中的所有的表名
                for (int i = 1; i <= count; i++)
                {
                    // ews = (Excel.Worksheet)ew.Worksheets[i];
                    string sheetName = ((Excel.Worksheet)ew.Worksheets[i]).Name;
                    //绑定到ddl控件中
                    this.ddlExcelSheetName.Items.Add(sheetName);
                }
                //把excel进程干掉,恨死我拉搞了n长时间
                ew.Close(false, Type.Missing, Type.Missing);
                ep.Workbooks.Close();
                ep.Quit();         
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ep);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ew);
                ep = null;
                ew = null;
                GC.Collect();//强制对所有代码垃圾进行回收   
            }
        }

        protected void btnAddSheet_Click(object sender, EventArgs e)
        {
            AddSheetNameToDrop();
        }
              
    }
}

 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统