注册 登录
编程论坛 SQL Server论坛

[求助] C#里面,怎么将DateGridView 里面的数据导出到Excel表里面啊?

606liutong 发布于 2007-08-11 18:16, 1347 次点击
C#里面怎么将DateGridView 里面的数据导出到Excel表里面啊? 查了好多资料 都不能运行

我的程序里面就三个控件 一个dataGridView1 一个button1 【一个TextBox1 (是否需要???)】

现在有两种想法

1: 直接导出,下载 不要路径了 让用户自己选择,点击button1 将dataGridView1 里面的数据导出到Excel表里面

2: TextBox1里面是Excel的路径 ,按照路径存储,点击button1 将dataGridView1 里面的数据导出到Excel表里面

大虾们,谁做过这个数据的导出啊 帮帮忙了 谢谢了


[此贴子已经被作者于2007-8-11 18:17:04编辑过]

1 回复
#2
sm1050964962007-08-15 12:30

<%
sub excel()
dim filename,fs,myfile,x
Set fs = server.CreateObject("scripting.filesystemobject")
'--假设你想让生成的EXCEL文件做如下的存放
filename = Server.MapPath("excel/baobiao.xls")
'--如果原来的EXCEL文件存在的话删除它
if fs.FileExists(filename) then
fs.DeleteFile(filename)
end if
'--创建EXCEL文件
set myfile = fs.CreateTextFile(filename,true)

if rs.EOF and rs.BOF then
else
dim strLine,responsestr
strLine=""
For each x in rs.fields
strLine = strLine & x.name & chr(9)
Next
'--将表的列名先写入EXCEL
myfile.writeline strLine
Do while Not rs.EOF
strLine=""
for each x in rs.Fields
strLine = strLine & x.value & chr(9)
next
'--将表的数据写入EXCEL
myfile.writeline strLine
rs.MoveNext
loop
end if

end sub

%>

1