在web from上输出数据到excel有两种方法,一个是有数据库直接导出;另外一个方法是由datagrid直接输出到excel文件。下面得代码实现了这两个功能。注意:在使用时要引用microsoft office web components 9.0 com组件,另外注意设置要保存文件得目录具有匿名可修改的权限。
datagridtoexcel.aspx
inherits=”aspxweb.mengxianhui.com.datagridtoexcel”%>
datagridtoexcel.aspx.vb
imports system.data
imports system.data.oledb
imports owc
public class datagridtoexcel
inherits system.web.ui.page
protected withevents xlfile as system.web.ui.webcontrols.textbox
protected withevents datagrid1 as system.web.ui.webcontrols.datagrid
protected withevents exportdatagrid2excel as system.web.ui.webcontrols.button
protected withevents exportdatabase2excel as system.web.ui.webcontrols.button
protected withevents label1 as system.web.ui.webcontrols.label
protected mengxianhui as new htmlgenericcontrol()
private cnn as oledbconnection = new oledbconnection(“provider=microsoft.jet.oledb.4.0;data source=”_
+ server.mappath(“test.mdb”))
private sql as oledbcommand = new oledbcommand(“select top 50 title,author from document”, cnn)
#region ” web form designer generated code ”
this call is required by the web form designer.
end sub
private sub page_init(byval sender as system.object, byval e as system.eventargs)_
handles mybase.init
codegen: this method call is required by the web form designer
do not modify it using the code editor.
initializecomponent()
end sub
#end region
private sub page_load(byval sender as system.object, byval e as system.eventargs)_
handles mybase.load
label1.text = “请输入要保存得文件名字:”
exportdatagrid2excel.text = “由datagrid生成excel文件”
exportdatabase2excel.text = “数据库直接生成excel文件”
datagrid1.columns(0).headerstyle.horizontalalign = horizontalalign.center
datagrid1.columns(0).headertext = “文章名称”
datagrid1.columns(1).headertext = “作者”
datagrid1.columns(0).headerstyle.font.bold = true
datagrid1.style.add(“font-size”, “9pt”)
mengxianhui.innertext = “【孟宪会之精彩世界】- 将datagrid输出到excel文件”
me.binddatagrid()
end sub
private sub binddatagrid()
cnn.open()
dim reader as oledbdatareader = sql.executereader()
me.datagrid1.datasource = reader
me.datagrid1.databind()
reader.close()
cnn.close()
end sub
private sub writedatagrid2excel()
dim xlsheet as new spreadsheetclass()
cnn.open()
dim reader as oledbdatareader = me.sql.executereader()
dim numbercols as integer = reader.fieldcount
dim row as integer = 2
dim i as integer = 0
输出标题
for i = 0 to numbercols – 1
xlsheet.activesheet.cells(1, i + 1) = reader.getname(i).tostring()
next
输出字段内容
while (reader.read())
for i = 0 to numbercols – 1
xlsheet.activesheet.cells(row, i + 1) = reader.getvalue(i).tostring()
next
row = row + 1
end while
reader.close()
cnn.close()
try
xlsheet.activesheet.export(server.mappath(“.”) + “\images\” + me.xlfile.text,_
owc.sheetexportactionenum.ssexportactionnone)
catch e as system.runtime.interopservices.comexception
response.write(“错误:” + e.message)
end try
end sub
private sub writedatagrid2excel2()
dim xlsheet as new spreadsheetclass()
dim i as integer = 0
dim j as integer = 0
response.end()
输出标题
dim oitem as datagridcolumn
for each oitem in datagrid1.columns
xlsheet.activesheet.cells(1, i + 1) = oitem.headertext
xlsheet.activesheet.range(xlsheet.activesheet.cells(1, 1),_
xlsheet.activesheet.cells(1, i + 1)).font.bold = true
设置格式
xlsheet.range(xlsheet.cells(1, 1), xlsheet.cells(1, i + 1)).font.bold = true
xlsheet.range(xlsheet.cells(1, 1), xlsheet.cells(1, i + 1)).font.color = “red”
i = i + 1
next
dim numbercols as integer = datagrid1.items.item(0).cells.count
输出字段内容
for j = 0 to datagrid1.items.count – 1
for i = 0 to numbercols – 1
xlsheet.range(xlsheet.cells(2, 2), xlsheet.cells(j + 2, i + 1)).font.color = “blue”
xlsheet.range(“a2:b14″).wraptext = true
xlsheet.range(xlsheet.cells(2, 1), xlsheet.cells(j + 2, i + 1)).autofitcolumns()
xlsheet.activesheet.cells(j + 2, i + 1) = datagrid1.items.item(j).cells(i).text.replace(” “, ” “)
next
next
try
xlsheet.activesheet.export(server.mappath(“.”) + “\images\” + me.xlfile.text,_
owc.sheetexportactionenum.ssexportactionnone)
catch e as system.runtime.interopservices.comexception
response.write(“错误:” + e.message)
end try
end sub
private sub exportdatagrid2excel_click(byval sender as object,_
byval e as system.eventargs) handles exportdatagrid2excel.click
if (me.xlfile.text.trim() <> “”) then
me.writedatagrid2excel2()
end if
end sub
private sub exportdatabase2excel_click(byval sender as object, _
byval e as system.eventargs) handles exportdatabase2excel.click
if (me.xlfile.text.trim() <> “”) then
me.writedatagrid2excel()
end if
end sub
end class