电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> .NET
HOW TO:利用Excel的QueryTable下载网上数据-.NET教程,.NET Framework
作者:网友供稿 点击:13
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
author:水如烟
总目录:行政区划数据方案设计

这里所说的网上数据,是基于:
一、有固定网址发布最新数据的链接;
二、数据格式固定。

在去年的10月,曾写了个《全国县及县以上行政区划代码信息类 》
见:http://www.cnblogs.com/lzmtw/archive/2005/10/22/260066.html

现在仍以行政区划代码数据为例。

行政区划代码数据由国家统计局发布,网址为
http://www.stats.gov.cn/tjbz/xzqhdm/index.htm
数据格式是固定的:
如最新的为2005年12月31日
http://www.stats.gov.cn/tjbz/xzqhdm/t20041022_402301029.htm
最旧的为2001年10月的,
http://www.stats.gov.cn/tjbz/xzqhdm/t20021125_46781.htm

但是有例外,这在代码中说。

方案组织:
效果:


以下为代码:
netconst.vb
namespace net
    
public class netconst
        
private sub new()
        
end sub

        
public const gov_default as string = "www.stats.gov.cn"
        
public const gov_address as string = "http://www.stats.gov.cn/tjbz/xzqhdm/"
        
public const webtable_index as string = "9"
    
end class
end namespace
netinformation.vb
imports system.net
imports system.io
imports system.text.regularexpressions

namespace net
    
public class netinformation
        
private gnetupdateinformations(-1as netupdateinformationitem


        
public readonly property updateinformationstable() as datatable
            
get
                
return getupdateinformationstable()
            
end get
        
end property

        
private function getupdateinformationstable() as datatable
            
dim mdatatable as new datatable("updateinformations")
            
with mdatatable
                .columns.add(
"address")
                .columns.add(
"lastdate")
                
for each item as netupdateinformationitem in gnetupdateinformations
                    .rows.add(
new string() {item.address, item.lastdate})
                
next
                .acceptchanges()
            
end with
            
return mdatatable
        
end function

        
public sub downloadinformationsfromnet()

            
dim mregex as new regex("(?<date>2.*日)")

            
dim mnetupdateitems as netupdateitem() = getnetupdateitems()
            
dim mnetupdateinformationitem as netupdateinformationitem

            
dim tmp as netupdateitem
            
由于后两个不合规则,舍去不用。最后一个没有日期,倒数第二个提供的是附件数据。
            for i as integer = 0 to mnetupdateitems.length - 1 - 2
                tmp 
= mnetupdateitems(i)

                mnetupdateinformationitem 
= new netupdateinformationitem
                
with mnetupdateinformationitem
                    .address 
= tmp.address
                    .lastdate 
= ctype(mregex.match(tmp.content).value, date).tostring("yyyymmdd")
                
end with

                appenditem(of netupdateinformationitem)(mnetupdateinformationitem, gnetupdateinformations)
            
next
        
end sub

        
private function getnetupdateitems() as netupdateitem()

            
dim mresult(-1as netupdateitem

            
dim mregex as new regex("<a href=(?<href>.*) target=_blank >(?<content>.*行政区划代码.*)</a>")
            
dim mcollection as matchcollection

            
dim mclient as new webclient()

            
dim mstream as stream = mclient.openread(netconst.gov_address)
            
dim mreader as new streamreader(mstream, system.text.encoding.default)
            
dim mtext as string = mreader.readtoend

            mreader.close()
            mstream.close()
            mclient.dispose()

            mcollection 
= mregex.matches(mtext)

            
dim tmpitem as netupdateitem
            
for each m as match in mcollection
                tmpitem 
= new netupdateitem
                
with tmpitem
                    .address 
= netconst.gov_address & m.groups(1).value
                    .content 
= m.groups(2).value
                
end with

                appenditem(of netupdateitem)(tmpitem, mresult)
            
next

            
return mresult
        
end function

        
private structure netupdateitem
            
public address as string
            
public content as string
        
end structure

        
private structure netupdateinformationitem
            
public address as string
            
public lastdate as string
        
end structure

        
private sub appenditem(of t)(byval value as t, byref array as t())
            
redim preserve array(array.length)
            array(array.length 
- 1= value
        
end sub

    
end class

end namespace

excelquerytable.vb
option strict off

namespace net
    
public class excelquerytable
        
private gexcelapplication as object
        
private gworkbook as object
        
private gworksheet as object
        
private gquerytable as object

        
sub new()
            initialize()
        
end sub

        
private sub initialize()
            gexcelapplication 
= createobject("excel.application")
            gexcelapplication.displayalerts 
= false 使退出时不询问是否存盘
            gworkbook = gexcelapplication.workbooks.add
            gworksheet 
= gworkbook.worksheets.add
        
end sub

        
这里只作简单处理,详细处理在我的blog上有相关“文章”作过介绍
        public sub close()
            gworkbook.close()
            gworksheet 
= nothing
            gworkbook 
= nothing
            gexcelapplication.displayalerts 
= true
            gexcelapplication.quit()
            gexcelapplication 
= nothing
        
end sub

        
public function query(byval address as stringas datatable
            
dim mdatatable as datatable = getdatatable()

            gworksheet.cells.clear()

            gquerytable 
= gworksheet.querytables.add( _
                connection:
=string.format("url;{0}", address), _
                destination:
=gworksheet.range("a1"))

            
with gquerytable
                .webtables 
= netconst.webtable_index  这是固定的
                .refresh(backgroundquery:=false)
            
end with


            
dim mcell as object
            
dim mmaxrowindex as integer
            
dim line as object

            mmaxrowindex 
= gworksheet.cells.specialcells(11).row excel.xlcelltype.xlcelltypelastcell=11
            mcell = gworksheet.range("a1")

            
for i as integer = 0 to mmaxrowindex
                line 
= mcell.offset(i, 0).value
                
if line isnot nothing then
                    addrow(mdatatable, line.tostring)
                
end if
            
next

            gquerytable.delete()
            gquerytable 
= nothing

            
return mdatatable
        
end function

        
private sub addrow(byval table as datatable, byval line as string)
            line 
= line.trim
            
if line.length < 7 then exit sub

            
dim tmpcode as string
            
dim tmpname as string

            tmpcode 
= line.substring(06)
            tmpname 
= line.substring(6).trim

            
if not isnumeric(tmpcode) then exit sub 前六位需是数字

            table.rows.add(
new string() {tmpcode, tmpname})
        
end sub

        
private function getdatatable() as datatable
            
表的列名意义为:代码、名称
            dim mdatatable as new datatable("regionalcode")
            
with mdatatable.columns
                .add(
"code")
                .add(
"name")
            
end with
            
return mdatatable
        
end function

    
end class
end namespace

测试代码:
mainform.vb(界面部分省,在最后有整个方案供下载)
public class mainform
    
private gnetinformation as new regionalcodelibrary.net.netinformation
    
private gquerytable as regionalcodelibrary.net.excelquerytable


    
private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click

        
if not checknetworkisavailable() then exit sub

        showmessage(
"正在下载数据信息")

        gnetinformation.downloadinformationsfromnet()
        
with me.combobox1
            .datasource 
= gnetinformation.updateinformationstable
            .displaymember 
= "lastdate"
        
end with

        showmessage(
"")
    
end sub

    
private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click
        
if string.isnullorempty(me.combobox1.text) then exit sub

        
if not checknetworkisavailable() then exit sub

        
if gquerytable is nothing then

            showmessage(
"正在启动excel")
            gquerytable 
= new regionalcodelibrary.net.excelquerytable

        
end if

        
dim maddress as string = ctype(me.combobox1.selecteditem, datarowview).row.item("address").tostring

        showmessage(
string.format("正在下载{0}数据"me.combobox1.text))
        
me.datagridview1.datasource = gquerytable.query(maddress)

        showmessage(
string.format("{0}共有数据{1}项"me.combobox1.text, me.datagridview1.rowcount))
    
end sub

    
private sub button3_click(byval sender as system.object, byval e as system.eventargs) handles button3.click
        clearenvironment()
    
end sub

    
private function checknetworkisavailable() as boolean
        
dim mresult as boolean = false
        mresult 
= my.computer.network.isavailable

        
if not mresult then
            showmessage(
"本地连接无效")

        
else
            
try
                mresult 
= my.computer.network.ping(regionalcodelibrary.net.netconst.gov_default)
            
catch ex as exception
                mresult 
= false
            
end try

            
if not mresult then
                showmessage(
string.format("本机没有连接internet或发布网址{0}无效", regionalcodelibrary.net.netconst.gov_address))
            
end if
        
end if

        
return mresult
    
end function

    
private sub showmessage(byval msg as string)
        
if msg = "" then msg = "待命"
        
me.label1.text = string.format("消息:{0}", msg)
        
me.label1.refresh()
    
end sub

    
private sub mainform_formclosing(byval sender as objectbyval e as system.windows.forms.formclosingeventargs) handles me.formclosing
        clearenvironment()
    
end sub

    
private sub clearenvironment()
        
if gquerytable is nothing then exit sub
        gquerytable.close()
        gquerytable 
= nothing
    
end sub
end class


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·经典收藏之 - C++内存管理详解-.NET教程,C#语言
·Master Page 初探-.NET教程,评论及其它
·GDI+编程10个基本技巧-.NET教程,评论及其它
·VB.NET中让Textbox只能输入数字(二)-.NET教程,VB.Net语言
·stl应用小问题-.NET教程,评论及其它
·WIN32中颜色值(COLORREF)与.NET中颜色值(Color)的转换-ASP教程,系统相关
·打造自己的专业图像工具-Visual C++ 2005图像编程系列【三】-.NET教程,C#语言
·.Net中常见问题及解决方法归类-.NET教程,.NET Framework
·Lex和Yacc从入门到精通(3)--一个极其简单的lex和yacc程序-.NET教程,评论及其它
·VB下几个非常有用的函数-.NET教程,VB.Net语言

最新文章
·VC#初学入门:第一个Windows程序
·ASP.NET 2.0-选用DataSet或DataReader
·用.net 处理xmlHttp发送异步请求
·asp.net创建文件夹的IO类的问题
·asp.net 2.0 中加密web.config 文件中的配置节
·关于ASP.NET调用JavaScript的实现
·如何实现ASP.NET网站个性化
·Acegi安全系统的配置-.NET教程,评论及其它
·Spring安全系统:Acegi Security Acegi简介-.NET教程,评论及其它
·Biztalk 开发之 架构和实例的验证-.NET教程,评论及其它


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号