温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VB.NET中怎么导出数据

发布时间:2021-08-06 15:40:51 阅读:205 作者:Leah 栏目:编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章将为大家详细讲解有关VB.NET中怎么导出数据,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

 VB.NET开发语言为我们平时的开发方式带来了非常的改变。很多时候可以利用Excel的数据透视表导出你想要的报表格式。那么VB.NET导出数据该如何实现呢?下面的代码可以从数据库中取出数据然后导入Excel。

Dim excel As Excel.Application  Dim xBk As Excel._Workbook  Dim xSt As Excel._Worksheet  Dim xRange As Excel.Range  Dim xPivotCache As Excel.PivotCache  Dim xPivotTable As Excel.PivotTable  Dim xPivotField As Excel.PivotField  Dim cnnsr As String, sql As String  Dim RowFields() As String = {""""""}  Dim PageFields() As String = {""""""""""""}  'SERVER 是服务器名或服务器的IP地址  'DATABASE 是数据库名  'Table 是表名  Try 

开始实现VB.NET导出数据

cnnsr = "ODBC;DRIVER=SQL Server;SERVER=" + SERVER   cnnsrcnnsr = cnnsr + ";UID=;APP=Report Tools;WSID=ReportClient;DATABASE=" + DATABASE  cnnsrcnnsr = cnnsr + ";Trusted_Connection=Yes"  excel = New Excel.ApplicationClass  xBk = excel.Workbooks.Add(True)  xSt = xBk.ActiveSheet  xRange = xSt.Range("A4")  xRange.Select() 

开始

xPivotCache = xBk.PivotCaches.Add(SourceType:=2)  xPivotCache.Connection = cnnsr xPivotCache.CommandType = 2 sql = "select * from " + Table  xPivotCache.CommandText = sql xPivotTable = xPivotCache.CreatePivotTable(TableDestination:="Sheet1!R3C1", TableName:="数据透视表1", DefaultVersion:=1

准备行字段

RowFields(0) = "字段1"  RowFields(1) = "字段2"  RowFields(2) = "字段3"

准备页面字段

PageFields(0) = "字段4"  PageFields(1) = "字段5"  PageFields(2) = "字段6"  PageFields(3) = "字段7"  PageFields(4) = "字段8"  PageFields(5) = "字段9"  xPivotTable.AddFields(RowFieldsRowFields:=RowFields, PageFieldsPageFields:=PageFields)  xPivotField = xPivotTable.PivotFields("数量")  xPivotField.Orientation = 4 

关闭工具条

'xBk.ShowPivotTableFieldList = False 'excel.CommandBars("PivotTable").visible = False excel.Visible = True Catch ex As Exception  If cnn.State = ConnectionState.Open Then  cnn.Close()  End If  xBk.Close(0)  excel.Quit()  MessageBox.Show(ex.Message, "报表工具", MessageBoxButtons.OK, MessageBoxIcon.Warning)  End Try 

关于VB.NET中怎么导出数据就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×