DataGrid中的数据导入到Word和Excel_VB
2024-07-21 02:22:51
供稿:网友
imports system
imports system.collections
imports system.componentmodel
imports system.data
imports system.drawing
imports system.web
imports system.web.sessionstate
imports system.web.ui
imports system.web.ui.webcontrols
imports system.web.ui.htmlcontrols
imports system.io
imports system.data.sqlclient
imports system.text
public class webform1
inherits system.web.ui.page
#region " web 窗体设计器生成的代码 "
'该调用是 web 窗体设计器所必需的。
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
end sub
protected withevents datagrid1 as system.web.ui.webcontrols.datagrid
protected withevents btn_import_excel as system.web.ui.webcontrols.button
protected withevents btnimportword as system.web.ui.webcontrols.button
'注意: 以下占位符声明是 web 窗体设计器所必需的。
'不要删除或移动它。
private designerplaceholderdeclaration as system.object
private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
'codegen: 此方法调用是 web 窗体设计器所必需的
'不要使用代码编辑器修改它。
initializecomponent()
end sub
#end region
private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
'在此处放置初始化页的用户代码
createdataset()
end sub
private sub exportdatagrid(byval filetype as string, byval filename as string) '从datagrid导出
response.charset = "gb2312"
response.contentencoding = system.text.encoding.getencoding("gb2312")
response.appendheader("content-disposition", "attachment;filename=" & httputility.urlencode(filename, system.text.encoding.utf8))
response.contenttype = filetype
me.enableviewstate = false
dim tw as system.io.stringwriter
tw = new system.io.stringwriter
dim hw as system.web.ui.htmltextwriter
hw = new htmltextwriter(tw)
datagrid1.rendercontrol(hw)
response.write(tw.tostring())
response.end()
end sub
private sub createdataset() '创建dataset
dim mydataset as dataset
mydataset = new dataset("anewdataset")
dim table1 as datatable
table1 = maketable("id", "name")
mydataset.tables.add(table1)
datagrid1.datasource = mydataset
datagrid1.databind()
end sub
private function maketable(byval c1name as string, byval c2name as string) as datatable '创建表
dim i as integer
dim mytable as new datatable
dim mycolumn as datacolumn
' add two datacolumns
mycolumn = new datacolumn(c1name, system.type.gettype("system.int32")) '创建字段和设置类型
mytable.columns.add(mycolumn)
mycolumn = new datacolumn(c2name, system.type.gettype("system.string"))
mytable.columns.add(mycolumn)
dim dr as datarow
for i = 1 to 10
dr = mytable.newrow()
dr(0) = i
dr(1) = "name" + i.tostring()
mytable.rows.add(dr)
next i
maketable = mytable
end function
private sub btnimportword_click(byval sender as system.object, byval e as system.eventargs) handles btnimportword.click
call exportdatagrid("application/ms-word", "指数列表.doc") '导到word
end sub
private sub btn_import_excel_click(byval sender as system.object, byval e as system.eventargs) handles btn_import_excel.click
call exportdatagrid("application/ms-excel", "指数列表.xls") '导到excel
end sub
end class