您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)VB.NET中怎么導(dǎo)出數(shù)據(jù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
VB.NET開發(fā)語言為我們平時的開發(fā)方式帶來了非常的改變。很多時候可以利用Excel的數(shù)據(jù)透視表導(dǎo)出你想要的報表格式。那么VB.NET導(dǎo)出數(shù)據(jù)該如何實(shí)現(xiàn)呢?下面的代碼可以從數(shù)據(jù)庫中取出數(shù)據(jù)然后導(dǎo)入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.
PivotCacheDim xPivotTable As Excel.
PivotTableDim xPivotField As Excel.
PivotFieldDim cnnsr As String, sql
As StringDim RowFields() As String =
{"", "", ""}Dim PageFields() As String =
{"", "", "", "", "", ""}'SERVER 是服務(wù)器名或服務(wù)器的IP地址
'DATABASE 是數(shù)據(jù)庫名
'Table 是表名
Try
開始實(shí)現(xiàn)VB.NET導(dǎo)出數(shù)據(jù)
cnnsr = "ODBC;DRIVER=SQL
Server;SERVER=" + SERVERcnnsrcnnsr = cnnsr + ";UID=;
APP=Report Tools;WSID=ReportClient;
DATABASE=" + DATABASEcnnsrcnnsr = 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:=
"數(shù)據(jù)透視表1", DefaultVersion:=1)
準(zhǔn)備行字段
RowFields(0) = "字段1" RowFields(1) = "字段2" RowFields(2) = "字段3"
準(zhǔn)備頁面字段
PageFields(0) = "字段4"
PageFields(1) = "字段5"
PageFields(2) = "字段6"
PageFields(3) = "字段7"
PageFields(4) = "字段8"
PageFields(5) = "字段9"
xPivotTable.AddFields(RowFields
RowFields:=RowFields, PageFields
PageFields:=PageFields)xPivotField = xPivotTable.
PivotFields("數(shù)量")xPivotField.Orientation = 4
關(guān)閉工具條
'xBk.ShowPivotTableFieldList
= False'excel.CommandBars("PivotTable")
.visible = Falseexcel.Visible = True
Catch ex As Exception
If cnn.State = ConnectionState
.Open Thencnn.Close()
End If
xBk.Close(0)
excel.Quit()
MessageBox.Show(ex.Message,
"報表工具", MessageBoxButtons.
OK, MessageBoxIcon.Warning)End Try
關(guān)于VB.NET中怎么導(dǎo)出數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。