经改良后的vb.net导出excel代码
2006-11-09 20:12
639 查看
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rowIndex, colIndex As Integer
Dim TDS As DataSet
Dim TSQL As String
TSQL = "select * from transfers"
TDS = GetDataFromDB(TSQL)
rowIndex = 1
colIndex = 0
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
Dim Table As New DataTable
Table = TDS.Tables(0)
'将所得到的表的列名,赋值给单元格
Dim Col As DataColumn
Dim col1 As DataColumn
Dim i As Integer
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 i = 0 To Table.Columns.Count - 1
For Each col1 In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(rowIndex, colIndex) = Row(col1.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
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim rowIndex, colIndex As Integer
Dim TDS As DataSet
Dim TSQL As String
TSQL = "select * from transfers"
TDS = GetDataFromDB(TSQL)
rowIndex = 1
colIndex = 0
xlBook = xlApp.Workbooks().Add
xlSheet = xlBook.Worksheets("sheet1")
Dim Table As New DataTable
Table = TDS.Tables(0)
'将所得到的表的列名,赋值给单元格
Dim Col As DataColumn
Dim col1 As DataColumn
Dim i As Integer
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 i = 0 To Table.Columns.Count - 1
For Each col1 In Table.Columns
colIndex = colIndex + 1
xlApp.Cells(rowIndex, colIndex) = Row(col1.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
相关文章推荐
- VB.Net导出SQLServer数据到Excel中代码架构段
- vb.net中导出至excel代码
- SQL.MDB数据库记录Rs导出到Excel.Sheet中代码(VBA+VB.Net)
- vb.net导出数据到Excel代码
- asp.net中Excel导入导出控件 2.0升级版本和1.0版本代码开源
- VB.NET机房收费系统导出Excel
- asp.net GridView导出到Excel代码
- VB.net导出花式Excel
- asp.net 导出Excel时,解决纯数字字符串变成类似这样的 2.00908E+18 形式的代码
- 【转】ASP.NET导出word和excel通用代码
- Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- vb.net开发中使用excel插件代码
- [VB.NET]急.....使用OWC11导出Excel,怎么生成多个工作薄..
- 用C#.net编写导入导出EXCEL文件的代码。编绎运行后,错误提示: 找不到可安装的ISAM
- vb.net 2010/2008 导出到excel,无需引用
- VB.NET 导出数据到EXCEL中
- VB.net 将dataGridView中的数据导出到excel
- vb.net 导出为excel及邮件群发
- 在VB.NET中,将数据库里的数据导出到Excel中
- NET页面导出Excel实例代码