datagrid输出到Excel2003?

liangwqtps 2005-11-01 04:38:21
在OFFICE 2000下运行很好的程序,因为升级到了OFFICE2003(SP1)而无法执行,似乎是找不到运行库.则将引用类型更改为Microsoft Office 11.0 Object Library和Microsoft Excel 11.0 Object Library.
在部分机器上可以运行,但仍然有些英文操作系统的机器报错.
郁闷...有遇到过这个问题的兄弟吗?

实际程序大致如下
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object

'Start a new workbook in Excel.
xlApp = CreateObject("Excel.Application")
'xlBook = xlApp.Workbooks.Add
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
xlApp.Visible = True
xlSheet = xlBook.Worksheets(1)

'Add data to cells of the first worksheet in the new workbook.

.....

...全文
345 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sheri28 2006-01-25
  • 打赏
  • 举报
回复
关注
hzg7361 2005-11-25
  • 打赏
  • 举报
回复
DreamOfEyes(风之语)
你的那个可以导出, 但是有个问题,能否给予解答,谢谢
导出后立即打开保存的那个文件没办法打开,因为excel进程没结束,现在怎样结束这个进程?
liangwqtps 2005-11-22
  • 打赏
  • 举报
回复
首先感谢各位的关心,所给的方法都是对的。
BUT:
这段时间我也在尝试,OFFICE2003调用EXCEL时还是在部分机器上有问题,估计和OFFICE2003的版本有关系。
再等等看。。。
DreamOfEyes 2005-11-22
  • 打赏
  • 举报
回复
Public Function ExportExcel(ByVal dtbTableName As DataTable) As Boolean
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Dim rowIndex, colIndex As Integer
rowIndex = 1
colIndex = 0

xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")

Dim Table As New DataTable
'Table = CreaTable()
Table = dtbTableName

'将所得到的表的列名,赋值给单元格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(1, colIndex) = Col.ColumnName
Next

'得到的表所有行,赋值给单元格
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

With xlSheet
'设标题为黑体字
.Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Name = "宋体"
'标题字体加粗
.Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Bold = True
'设表格边框样式
.Range(.Cells(1, 1), .Cells(rowIndex, colIndex)).Borders.LineStyle = 1

End With

With xlSheet.PageSetup
.LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:" ' & Gsmc
.CenterHeader = "&""楷体_GB2312,常规""公司人员情况表&""宋体,常规""" & Chr(10) & "&""楷体_GB2312,常规""&10日 期:"
.RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:"
.LeftFooter = "&""楷体_GB2312,常规""&10制表人:"
.CenterFooter = "&""楷体_GB2312,常规""&10制表日期:"
.RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页"
End With

xlApp.Visible = True

Return True

End Function
moodboy1982 2005-11-02
  • 打赏
  • 举报
回复
这么快,才一回,就有这么多人回了?
renyu732 2005-11-02
  • 打赏
  • 举报
回复
http://renyu732.cnblogs.com/archive/2005/06/15/174866.aspx
jxufewbt 2005-11-02
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/ShowDetail.aspx?id=8A4CBF47-B888-4832-3389-ED3A3A3C8AAB
snowjin78 2005-11-02
  • 打赏
  • 举报
回复
我试过了,确实可以!感谢!
qqchenjacky 2005-11-01
  • 打赏
  • 举报
回复
这是一个导出excel函数,


Dim myExcel As New Excel.Application
Public Sub ExportToExcel(ByVal mytable As DataTable)

Try
Dim datav As New DataView
datav = mytable.DefaultView
Dim i, j As Integer
Dim rows As Integer = datav.Table.Rows.Count
Dim cols As Integer = datav.Table.Columns.Count
Dim DataArray(rows - 1, cols - 1) As String
Dim myExcel As Excel.Application = New Excel.Application
For i = 0 To rows - 1
For j = 0 To cols - 1
If datav.Table.Rows(i).Item(j) Is System.DBNull.Value Then
Else
DataArray(i, j) = datav.Table.Rows(i).Item(j)
End If
Next
Next
myExcel.Application.Workbooks.Add(True)
myExcel.Visible = True
For j = 0 To cols - 1
myExcel.Cells(1, j + 1) = datav.Table.Columns(j).ColumnName
Next
myExcel.Range("A2").Resize(rows, cols).Value = DataArray
myExcel.Quit()

Catch exp As Exception
MessageBox.Show("数据导出失败!请查看是否已经安装了Excel")

End Try

End Sub
smallMage 2005-11-01
  • 打赏
  • 举报
回复
http://dotnet.asp.cc
zhangzhiwei 2005-11-01
  • 打赏
  • 举报
回复
up ~~~

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧