您的位置:程序门 -> .net技术 -> vb.net



怎样在vs 2005.net中导入excel表?急!!!!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


怎样在vs 2005.net中导入excel表?急!!!![已结贴,结贴人:fengwujie]
发表于:2007-08-23 09:26:42 楼主
我是初学者,请各位兄弟帮小弟解决一下啊!界面有一个按钮,点击要导入excel表,表名不定,路径不定,怎样导入excel表,并在该界面的datagrid中显示出来!请知道的大哥大姐多多帮忙,小弟感激不尽!!!
发表于:2007-08-23 09:32:201楼 得分:0
http://www.cnblogs.com/fengrengui/archive/2007/02/08/397899.html
发表于:2007-08-23 09:33:592楼 得分:0
winform   or   webform?

网上不是有很多么
发表于:2007-08-23 09:34:003楼 得分:0
http://www.cnblogs.com/stswordman/archive/2006/08/24/485641.html
发表于:2007-08-23 09:35:444楼 得分:0
谢谢各位仁兄,可是我看他们的问题好象是固定的表名和路径呢!
发表于:2007-08-23 09:40:525楼 得分:0
谢谢各位仁兄,可是我看他们的问题好象是固定的表名和路径呢!
————————————————————————————————
先上传,再读取
发表于:2007-08-23 09:43:286楼 得分:0
哪位兄弟有代码的请不吝赠送给小弟,是vb.net语言,web窗口开发环境的.
发表于:2007-08-23 09:44:247楼 得分:30
///   <summary>
        ///   excel文件解析器,需要在运行环境上正确安装excel的驱动
        ///   </summary>
        public   class   excelresolver
        {
                private   string   m_filepath;
                private   list <string>   m_worksheetnames;
                private   string   querystring;
                private   string   connstr;
                private   oledbconnection   conn;
                public   string   filepath
                {
                        set   {   m_filepath   =   value;   }
                        get   {   return   m_filepath;   }
                }
                ///   <summary>
                ///   获取worksheet的名称集合
                ///   </summary>
                public   list <string>   worksheetname
                {
                        get
                        {
                                if   (this.m_worksheetnames   ==   null)
                                        initworksheetname();
                                return   m_worksheetnames;
                        }
                }

                public   void   initconnstringetc(string   _filepath)
                {
                       
                        conn   =   new   oledbconnection(connstr);
                        connstr   =   string.format( "provider=microsoft.jet.oledb.4.0; "   +   "data   source={0} "   +   "; "   +   "extended   properties=excel   8.0; ",   _filepath);
                        conn.connectionstring   =   connstr;
                }

                ///   <summary>
                ///   excel文件路径
                ///   </summary>
                ///   <param   name= "_filepath "> </param>
                public   excelresolver(string   _filepath)
                {
                        initconnstringetc(_filepath);
                        string[]   strs   =   null;
                        try
                        {
                                strs   =   _filepath.split( '. ');
                                if   (strs[strs.length   -   1].tolower()   !=   "xls ")
                                        throw   new   exception( "请传入正确的excel2003格式的文件 ");
                                else
                                        if   (strs[strs.length   -   1].tolower()   ==   "xlsx ")
                                                throw   new   exception( "为了保持良好的兼容性,请上传excel2003及以下版本的excel文件 ");
                                this.m_filepath   =   _filepath;
                        }
                        catch   (exception   ee)   {   ee.tostring();   }
                }
                public   excelresolver(string   _filepath,   string   _querystring)
                {
                        initconnstringetc(_filepath);
                        string[]   strs   =   _filepath.split( '. ');
                        if   (strs[strs.length   -   1].tolower()   !=   "xls ")
                                throw   new   exception( "请传入正确的excel2003格式的文件 ");
                        else
                                if   (strs[strs.length   -   1].tolower()   ==   "xlsx ")
                                        throw   new   exception( "为了保持良好的兼容性,请上传excel2003及以下版本的excel文件 ");
                        this.m_filepath   =   _filepath;
                        this.querystring   =   _querystring;
                }

                public   dataset   getexceldataset()
                {

                        oledbdataadapter   ada   =   new   oledbdataadapter(querystring,   conn);
                        dataset   ds   =   new   dataset();
                        ada.fill(ds);
                        return   ds;
                }
                public   void   initworksheetname()
                {
                        m_worksheetnames   =   new   list <string> ();
                        conn.open();
                        system.data.datatable   dt   =   conn.getoledbschematable(oledbschemaguid.tables,   null);
                        conn.close();
                        foreach   (datarow   dr   in   dt.rows)
                        {
                                string   temp   =   dr[ "table_name "].tostring();
                                if   (temp.startswith( " ' ")   &&   temp.endswith( " ' "))
                                {
                                        m_worksheetnames.add(temp.substring(1,   temp.length   -   3));
                                }
                        }
                }
        }
//这个是com方式读取excel表还有一种是用ole方式接入,不过ole方式接入的不能动态识别表名
发表于:2007-08-23 09:48:288楼 得分:0
谢谢给代码的兄弟,小弟这就试看看!
发表于:2007-08-23 09:55:249楼 得分:0
closetome(即鹿无虞,惟入于林中。君子几,不如舍。往吝。)   这位仁兄,为什么代码加进去后都是语法错误呢!你给的代码是vb.net的吗?还是c#的呢!有哪些地方要根据我的情况修改的吗??请给出明确的指示!
发表于:2007-08-23 09:56:1810楼 得分:0
c#的。你还要导入excel命名空间啊。
发表于:2007-08-23 09:57:2011楼 得分:0
microsoft.office.interop.excel
发表于:2007-08-23 10:00:5412楼 得分:0
哦,我要的是vb.net。不过还是很感谢你的热心。我会给你加分的。a   li   ga   to       go   za   i   ma   si!!!!
发表于:2007-08-23 10:16:5413楼 得分:0
vb的?我给你贴一点,我刚用过的
发表于:2007-08-23 10:20:3814楼 得分:0
好啊!谢谢这位仁兄,我急用呢!
发表于:2007-08-23 10:21:4915楼 得分:50
'gridview导入到excel中
                dim   sw   as   stringwriter   =   new   stringwriter
                dim   htw   as   htmltextwriter   =   new   htmltextwriter(sw)
                gview.rendercontrol(htw)
                dim   strhtml   as   string   =   sw.tostring.trim

                dim   excelfilename   as   string   =   "foodlist.xls "
                dim   filephysicialpathname   as   string   =   request.physicalapplicationpath

                '   生成的excel文件名
                dim   objectexcelfilename   as   string   =   path.combine(filephysicialpathname,   excelfilename)

                if   (file.exists(objectexcelfilename))   then
                        file.delete(objectexcelfilename)
                end   if
                dim   fs   as   filestream   =   new   filestream(objectexcelfilename,   filemode.create)
                dim   bw   as   binarywriter   =   new   binarywriter(fs,   encoding.getencoding( "gb18030 "))
                bw.write(strhtml)
                bw.close()
                fs.close()
这是其中一个方法,不过需要另外添加这个方法,如果出错的话,添加下面的.

        public   overrides   sub   verifyrenderinginserverform(byval   control   as   control)

              ' '         'base.verifyrenderinginserverform(control);

        end   sub
发表于:2007-08-23 10:26:3016楼 得分:0
需要的命名空间
imports   system.io
imports   microsoft.office
imports   microsoft.office.interop
可能有部分不需要,我都贴上了.这个区别于上一个方法是可以控制到excel的具体单元格


protected   sub   ibtnprint_click(byval   sender   as   object,   byval   e   as   system.web.ui.imageclickeventargs)   handles   ibtnprint.click
                dim   oexcel   as   new   excel.application
                dim   obooks   as   excel.workbooks,   obook   as   excel.workbook
                dim   osheets   as   excel.sheets,   osheet   as   excel.worksheet
                dim   ocells   as   excel.range
                dim   sfile   as   string,   stemplate   as   string
                '定义一个datatable
                'dim   dt   as   datatable   =   ctype(application.item( "mydatatable "),   datatable)
                'dim   dt   as   datatable   =   ctype(gview.datasource,   datatable)

                'sfile   =   server.mappath(request.applicationpath)   &   "\myexcel.xls "   ' "../myexcel.xls "   '
                sfile   =   server.mappath( ". ")   &   "\myexcel.xls "
                '定义模版文件
                'stemplate   =   server.mappath(request.applicationpath)   &   "\mytemplate.xls "   ' "../mytemplate.xls "
                stemplate   =   server.mappath( ". ")   &   "\mytemplate.xls "
                oexcel.visible   =   false
                oexcel.displayalerts   =   false
                '定义一个新的工作簿
                obooks   =   oexcel.workbooks
                'obooks.open(server.mappath(request.applicationpath)   &   "\mytemplate.xls ")
                obooks.open(server.mappath( ". ")   &   "\mytemplate.xls ")
                obook   =   obooks.item(1)
                osheets   =   obook.worksheets
                osheet   =   ctype(osheets.item(1),   excel.worksheet)

                '命名该sheet
                osheet.name   =   "first   sheet "
                ocells   =   osheet.cells
                '调用dumpdata过程,将数据导入到excel中去
                dumpdata(ocells)
                '保存
                osheet.saveas(sfile)
                'osheet.saveas( "../myexcel.xls ")
                obook.close()

                '退出excel,并且释放调用的com资源
                oexcel.quit()
                releasecomobject(ocells)   :   releasecomobject(osheet)
                releasecomobject(osheets)   :   releasecomobject(obook)
                releasecomobject(obooks)   :   releasecomobject(oexcel)
                oexcel   =   nothing   :   obooks   =   nothing   :   obook   =   nothing
                osheets   =   nothing   :   osheet   =   nothing   :   ocells   =   nothing
                system.gc.collect()
                response.redirect( "../wellb/myexcel.xls ")
exit   sub


        '将datatable的内容导出到excel的单元格中去
        private   function   dumpdata(byval   ocells   as   excel.range)   as   string
                dim   dr   as   gridviewrow   ',   ary()   as   object
                dim   irow   as   integer,   icol   as   integer

                '输出列标题
                ocells(1,   1)   =   "济北 "   +   ddpzyq.selecteditem.text.trim   +   "作业区油井综合日报 "
                ocells(3,   1)   =   format(cdate(txtdate.text.trim),   "yyyy-mm-dd ")

                for   icol   =   0   to   gview.columns.count   -   1
                        ocells(4,   icol   +   1)   =   gview.columns(icol).tostring
                next

                '将数据导出到相应的单元格
                for   irow   =   0   to   gview.rows.count   -   1
                        dr   =   gview.rows.item(irow)
                        'ary   =   dr.cells.count
                        for   icol   =   0   to   dr.cells.count   -   1   'ubound(ary)
                                ocells(irow   +   5,   icol   +   1)   =   dr.cells.item(icol).text.tostring.trim
                                'response.write(dr.cells.item(icol).text.tostring.trim   &   vbtab)
                        next
                next
end   function
发表于:2007-08-23 10:28:3417楼 得分:0
你这个是从datagrid导出到excel的吧!这个我也要,不过我现在得先把数据从excel表导入datagrid里面呢!这个有吗?哪些要根据具体情况修改的请明示一下,谢谢,我是初学者,有的代码都还看不懂呢!
发表于:2007-08-23 10:29:5218楼 得分:0
gview是gridview控件,你可以导入别的数据源到excel中
发表于:2007-08-23 10:33:2819楼 得分:0
我给你说一下,你看看你能不能做吧,你说的这个不难实现,可以首先生成一个datatable去接收excel里面的数据,最简单的是做一个小循环,循环的结构可以参考   private   function   dumpdata(byval   ocells   as   excel.range)   as   string   这个方法。
发表于:2007-08-23 10:42:5320楼 得分:0
哎呀!基础差真是苦啊!真郁闷啊!这个东西我都找了两天还没搞定!真快要晕死了啊!
发表于:2007-08-23 11:13:4621楼 得分:0
要不你可以直接将excel导入到数据库表当中去,这样省事,方便。
发表于:2007-08-25 20:30:0222楼 得分:0
我也象上面说的那样做过,可是有个缺点,就是你导进去以后好多需要修改以后才可以被.net接受。
顺便问问啊,把数据表中的生成的报表啊之类其他对象怎么弄到.net啊,???
发表于:2007-08-26 11:59:2123楼 得分:20
imports   system.text
imports   system.windows.forms.savefiledialog
public   class   class_dgtoexcel
        public   sub   getdgdatatoexcel(byval   dg   as   datagrid,   byval   savefiledialog   as   savefiledialog)
                dim   xlsheet   as   excel.worksheet
                dim   xlapp   as   new   excel.application
                dim   xlbook   as   excel.workbook
                dim   xldbtable   as   excel.datatable
                try
                        dim   strflname   as   string
                        savefiledialog.title   =   "保存为 "
                        savefiledialog.filter   =   "xls工作薄 ¦*.xls "
                        if   savefiledialog.showdialog   =   windows.forms.dialogresult.ok   then
                                if   savefiledialog.filename   <>   " "   then
                                        strflname   =   savefiledialog.filename()
                                else
                                        exit   sub
                                        gc.collect()
                                end   if
                        else
                                exit   sub
                        end   if
                        xlbook   =   xlapp.workbooks.add
                        xlsheet   =   xlbook.worksheets( "sheet1 ")
                        dim   table   as   new   datatable
                        table   =   ctype(dg.datasource,   datatable)   '将datagrid中的数据填充到datatable中
                        dim   col   as   datacolumn
                        dim   colindex   as   integer
                        for   each   col   in   table.columns
                                colindex   =   colindex   +   1
                                xlapp.cells(1,   colindex)   =   col.columnname   '获取列名
                        next
                        dim   row   as   datarow
                        dim   rowindex   as   integer
                        rowindex   =   1   '从第二行开始填充数据
                        dim   rowint   as   integer
                        for   each   row   in   table.rows   '获取数据
                                rowindex   =   rowindex   +   1
                                colindex   =   0
                                for   each   col   in   table.columns
                                        colindex   =   colindex   +   1
                                        xlapp.cells(rowindex,   colindex)   =   row(col.columnname)
                                next
                        next
                        xlbook.saveas(strflname,   xlapp.activeworkbook.fileformat,   " ",   " ",   xlapp.activeworkbook.readonlyrecommended,   xlapp.activeworkbook.createbackup,   excel.xlsaveasaccessmode.xlshared.xlshared,   xlapp.activeworkbook.conflictresolution,   false,   " ",   " ")
                catch   ex   as   exception
                        msgbox(ex.message)
                        xlbook.close()
                        xlapp.quit()
                        xlapp   =   nothing
                end   try
                xlbook.close()
                xlapp.quit()
                xlapp   =   nothing
        end   sub
end   class
发表于:2007-10-19 16:51:5524楼 得分:0
问题解决了吗?帮顶一下,楼主解决了是不是可以给我一段代码,谢谢!


快速检索

最新资讯
热门点击