473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using .Net to create Excel Pivot table

Hi All,
I'm struggling to find the correct syntax for creating a Pivot table in
an excel file via VB . Net 2005

The Excel help file is not particularly helpful where parameters are
concerned (It rarely mentiones the required data types etc)

The following is as far as I can get;

Dim excelApp As Excel.Application
Dim wkBook As Excel.Workbook, wkSheet As Excel.Worksheet

excelApp = New Excel.Application
excelApp.Visible = False

wkBook = excelApp.Workbooks.Add()
wkSheet = wkBook.Worksheets("Sheet1")

Dim connectionStr as String = ...
Dim tableName as String = "myDb.dbo.TableName"

wkBook.Connections.Add("MyConnection", "Description", connectionStr,
tableName, 3)
wkBook.Connections("MyConnection").Refresh()

Dim pvtCache As Excel.PivotCache =
wkBook.PivotCaches.Create(Excel.XlPivotTableSource Type.xlExternal,
wkBook.Connections("MyConnection"), _
Excel.XlPivotTableVersionList.xlPivotTableVersion1 1)

' The above part works fine

' but creating the Pivot Table throws up a ComException .. if I play with
the parameters I get a very unhelpful ArgumentException telling me "The
argument is incorrect" .. but which one ?

Dim pvtTable As Excel.PivotTable

' This version throws an ArgumentException
pvtTable = wkSheet.PivotTables.Add(PivotCache:=pvtCache,
TableDestination:=Excel.XlParameterType.xlRange, TableName:="PivotTable1")
' This version (and multiple others) throw a ComException
pvtTable = wkSheet.PivotTables.Add(PivotCache:=pvtCache,
TableDestination:=wkSheet.Range("A3"), TableName:="PivotTable1") ' Throws a
ComException

Can anyone help with the correct syntax please ?

Thanks

Gary

Oct 4 '08 #1
2 9364
Hi,

Best way is to record a macro in Excel of you manually creating the pivot
table, then copy paste the VBA code to VB and modify as needed.

--

Rod Gill

"Gary Dunne" <ga********@AT.hotmail.dot.comwrote in message
news:Ot**************@TK2MSFTNGP02.phx.gbl...
Hi All,
I'm struggling to find the correct syntax for creating a Pivot table in
an excel file via VB . Net 2005

The Excel help file is not particularly helpful where parameters are
concerned (It rarely mentiones the required data types etc)

The following is as far as I can get;

Dim excelApp As Excel.Application
Dim wkBook As Excel.Workbook, wkSheet As Excel.Worksheet

excelApp = New Excel.Application
excelApp.Visible = False

wkBook = excelApp.Workbooks.Add()
wkSheet = wkBook.Worksheets("Sheet1")

Dim connectionStr as String = ...
Dim tableName as String = "myDb.dbo.TableName"

wkBook.Connections.Add("MyConnection", "Description", connectionStr,
tableName, 3)
wkBook.Connections("MyConnection").Refresh()

Dim pvtCache As Excel.PivotCache =
wkBook.PivotCaches.Create(Excel.XlPivotTableSource Type.xlExternal,
wkBook.Connections("MyConnection"), _
Excel.XlPivotTableVersionList.xlPivotTableVersion1 1)

' The above part works fine

' but creating the Pivot Table throws up a ComException .. if I play with
the parameters I get a very unhelpful ArgumentException telling me "The
argument is incorrect" .. but which one ?

Dim pvtTable As Excel.PivotTable

' This version throws an ArgumentException
pvtTable = wkSheet.PivotTables.Add(PivotCache:=pvtCache,
TableDestination:=Excel.XlParameterType.xlRange, TableName:="PivotTable1")
' This version (and multiple others) throw a ComException
pvtTable = wkSheet.PivotTables.Add(PivotCache:=pvtCache,
TableDestination:=wkSheet.Range("A3"), TableName:="PivotTable1") ' Throws
a ComException

Can anyone help with the correct syntax please ?

Thanks

Gary


Oct 4 '08 #2
Thanks Rod,

I found some VB6 code online that I was able to alter to suit my needs.
(The record macro idea is also very helpful for fillig in the blanks)

Gary

"Rod Gill" <rodATproject-systemsDOTcoDOTnzwrote in message
news:ex**************@TK2MSFTNGP03.phx.gbl...
Hi,

Best way is to record a macro in Excel of you manually creating the pivot
table, then copy paste the VBA code to VB and modify as needed.

--

Rod Gill

"Gary Dunne" <ga********@AT.hotmail.dot.comwrote in message
news:Ot**************@TK2MSFTNGP02.phx.gbl...
>Hi All,
I'm struggling to find the correct syntax for creating a Pivot table in
an excel file via VB . Net 2005

The Excel help file is not particularly helpful where parameters are
concerned (It rarely mentiones the required data types etc)

The following is as far as I can get;

Dim excelApp As Excel.Application
Dim wkBook As Excel.Workbook, wkSheet As Excel.Worksheet

excelApp = New Excel.Application
excelApp.Visible = False

wkBook = excelApp.Workbooks.Add()
wkSheet = wkBook.Worksheets("Sheet1")

Dim connectionStr as String = ...
Dim tableName as String = "myDb.dbo.TableName"

wkBook.Connections.Add("MyConnection", "Description", connectionStr,
tableName, 3)
wkBook.Connections("MyConnection").Refresh()

Dim pvtCache As Excel.PivotCache =
wkBook.PivotCaches.Create(Excel.XlPivotTableSourc eType.xlExternal,
wkBook.Connections("MyConnection"), _
Excel.XlPivotTableVersionList.xlPivotTableVersion 11)

' The above part works fine

' but creating the Pivot Table throws up a ComException .. if I play with
the parameters I get a very unhelpful ArgumentException telling me "The
argument is incorrect" .. but which one ?

Dim pvtTable As Excel.PivotTable

' This version throws an ArgumentException
pvtTable = wkSheet.PivotTables.Add(PivotCache:=pvtCache,
TableDestination:=Excel.XlParameterType.xlRange ,
TableName:="PivotTable1")
' This version (and multiple others) throw a ComException
pvtTable = wkSheet.PivotTables.Add(PivotCache:=pvtCache,
TableDestination:=wkSheet.Range("A3"), TableName:="PivotTable1") ' Throws
a ComException

Can anyone help with the correct syntax please ?

Thanks

Gary



Oct 7 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alexander Mandl | last post by:
Hello I have a pivot table in an Access form and open the pivot Table (Excel OLE Object) from within Access. In the Excel Table is a macro (signed) (in teh open event) running when opening the...
9
by: [Yosi] | last post by:
Can I make an Excel file without having Excel in my PC ? I want to create Excel files from my C# application , then open those files later in another PC who have Excel installed . As we can open...
3
by: Grey | last post by:
My requirement is The user has input data in the asp.net application and the data has been stored in MSDE. After the user click the save data in MSDE, the web application will start the client's...
1
by: Grey | last post by:
How to export a dataset to excel file. I need the exported data should be named with variable in excel as I need to do pivot table in the excel. So do I need to created pivot table before and only...
1
by: Jerome Ranch | last post by:
I consider myself an Excel PT wizard of sorts, but now I have a situation with so much infromationthat I need to categorize and summarize, that I will use access to manage it. Interestingly,...
8
by: Jerome Ranch | last post by:
Okay So I've got pivot tables setup in Access 2003. Only about 30K records in the current 2005 databases...the pivots summarize the info in a number of nice ways. I need to get the pivot tables...
2
by: LittlePhil via AccessMonster.com | last post by:
Someone please help before i start to cry. I'm trying to export from Access to Excel, then create a new excel sheet with a pivot table to display the data held in columns A:P. I get the error...
1
benchpolo
by: benchpolo | last post by:
I have data extracted from Access db to Excel with a pivot table. Somehow, I am having issues with the pivot table were it doesnt update the totals. For example, the first extract i did in Access...
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.