当前位置: 移动技术网 > IT编程>开发语言>.net > .NET com组件、OLEDB导入EXCEL

.NET com组件、OLEDB导入EXCEL

2019年05月10日  | 移动技术网IT编程  | 我要评论

个人域名备案,楚楚街9.9包邮,免费电子书txt全集

.net com


这种方法在计算机没有安装office套件时,也是能够使用的。所以不依赖于软件,

但是还是需要xcel.exe编译后的dll文件打包到相应的程序中来引用。这样将dll文件“

随身携带”,就可以了。还是挺不错的!

1.注册microsoft.office.interop.excel.dll

在office安装文件夹下找到excel.exe,路径d:\program files(x86)\microsoft

office\office15.将excel.exe文件复制到d:\programfiles (x86)\microsoft visual

studio 11.0\vc下。用visual studio 2012命令行工具切换到d:\program files

(x86)\microsoft visual studio11.0\vc,一般会自动切换。这时候执行tlbimp /

out:interop.excel.dll excel.exe。提示


\


2.引用interop.excel.dll

将编译好的dll文件复制到程序的bin文件下。添加引用


下面是我自己做的一个小demo。

private void openexcel(string strfilename)
    {
        object missing = system.reflection.missing.value;
        microsoft.office.interop.excel.application excel = new microsoft.office.interop.excel.application();//lauch excel application
        if (excel == null)
        {
            
        }
        else
        {
            excel.visible = false;  excel.usercontrol = true;
            // 以只读的形式打开excel文件
            workbook wb = excel.application.workbooks.open(strfilename, missing, true, missing, missing, missing,
             missing, missing, missing, true, missing, missing, missing, missing, missing);
            //取得第一个工作薄
            worksheet ws = (worksheet)wb.worksheets.get_item(1);

            //取得总记录行数   (包括标题列)
            int rowsint = ws.usedrange.cells.rows.count; //得到行数
            //int columnsint = mysheet.usedrange.cells.columns.count;//得到列数

            //取得数据范围区域  (不包括标题列)
            range rng1 = ws.cells.get_range("b2", "b" + rowsint);   //item

            range rng2 = ws.cells.get_range("k2", "k" + rowsint);  //customer
            object[,] arryitem= (object[,])rng1.value2;   //get range's value
            object[,] arrycus = (object[,])rng2.value2; 
            //将新值赋给一个数组
            string[,] arry = new string[rowsint-1, 2];
            for (int i = 1; i 

结果


\

\





oledb方式

这种方式就像平时使用sqlserver一样,将excel文件当成一个数据源来对待。只不过

这时候的是excel罢了,其实一样简单来看sqlserver也就是复杂化的excel所以这

种方式相对还是

比较常见的。

code

/// 
        /// 读取excel数据到ds
        /// 
        /// xls文件路径(服务器物理路径)string rootdir =server.mappath(system.web.httpcontext.current.request.applicationpath.tostring());//获取程序根目录
        /// 
        public dataset excelreader(string excelname)
        {
            // 拼写连接字符串,打开连接
            string strconn = "provider=microsoft.ace.oledb.12.0;" + "data source=" + excelname + ";extended properties='excel 8.0; hdr=yes; imex=1'";
            oledbconnection objconn = new oledbconnection(strconn);
            objconn.open();
            // 取得excel工作簿中所有工作表
            datatable schematable = objconn.getoledbschematable(oledbschemaguid.tables, null);
            oledbdataadapter sqlada = new oledbdataadapter();
            dataset ds = new dataset();

            // 遍历工作表取得数据并存入dataset
            foreach (datarow dr in schematable.rows)
            {
                string strsql = "select * from [" + dr[2].tostring().trim() + "]";
                oledbcommand objcmd = new oledbcommand(strsql, objconn);
                sqlada.selectcommand = objcmd;
                sqlada.fill(ds, dr[2].tostring().trim());
            }


            objconn.close();
            return ds;
        }



几个关键code句:

的垃圾回收:

   //得到excel所有的进程

     process[] procs = process.getprocessesbyname("excel");
     foreach (process pro in procs)
     {
         pro.kill();//
      }
     gc.collect();


com组件创建excel操作对象

  microsoft.office.interop.excel.application excel = new microsoft.office.interop.excel.application(); //以只读的形式打开excel文件
                workbook wb = excel.application.workbooks.open(strfilename, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing);
//取得第一个工作薄
worksheet ws = (worksheet)wb.worksheets.get_item(1);
//取得单元格的值
string cellstr = ws.cells[i][j].value;


oledb建立excel连接

// 拼写连接字符串,打开连接
            string strconn = "provider=microsoft.ace.oledb.12.0;" + "data source=" + excelname + ";extended properties='excel 8.0; hdr=yes; imex=1'";
            oledbconnection objconn = new oledbconnection(strconn);
objconn.open();
// 取得excel工作簿中所有工作表
datatable schematable = objconn.getoledbschematable(oledbschemaguid.tables, null);


第一种方法是创建excel对象,第二种方法是以excel为数据源。第一种的适用面更

广。还有一种是用二进制数据流的方式来读取,需要将excel文件转成csv文件。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网