Professional
Excel
®
Services
Shahar Prish
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page iii
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page ii
Professional
Excel
®
Services
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page i
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page ii
Professional
Excel
®
Services
Shahar Prish
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page iii
Professional Excel
®
Services
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-10486-6
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
Library of Congress Cataloging-in-Publication Data
Prish, Shahar, 1975-
Professional Excel Services / Shahar Prish.
p. cm.
Includes bibliographical references.
ISBN 978-0-470-10486-6 (paper/website)
1. Microsoft Excel (Computer file)--Handbooks, manuals, etc. 2. Business--Computer programs. I. Title.
HF5548.4.M523P76 2007
005.54--dc22
2007006215
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by
any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under
Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the
Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center,
222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permis-
sion should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis,
IN 46256, (317) 572-3447, fax (317) 572-4355, or online at />LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO
REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE
CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT
LIMITATION WARRANTIES OF FITNESS FOR A PARTI CULAR PURPOSE. NO WARRANTY MAY BE CREATED
OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED
HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTAND-
ING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PRO-
FESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT
PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL
BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS
REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMA-
TION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE
ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READ-
ERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR
DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services please contact our Customer Care Department within
the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress
are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and
other countries, and may not be used without written permission. Microsoft and Excel are registered trademarks
of Microsoft Corporation in the United States and/or other countries. All other trademarks are the property of their
respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be avail-
able in electronic books.
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page iv
To my parents, Tzila and Danny — thank you
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page v
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page vi
About the Author
Shahar Prish was born and raised in Israel. He was playing around with computers for 10 years or
so when suddenly and unexpectedly it became a useful skill to have. At the age of 16, Shahar started
working in a company that did various backup and encryption applications for the PC. When he turned
18 he was drafted to the army where he defended his country from behind a keyboard, a monitor, and
way too much non-dairy chocolate bread-spread. When he got out of the army, he tried to get a degree
in CS, but failed miserably due to work getting in the way. He worked for a big telecommunication
company (where he developed software for international calls fraud detection) and in a small startup
(which did something with browsers — it was never really clear). In 1998 he was the first employee in a
small company called “Maximal Innovative Intelligence” that developed Business Intelligence software.
In 2001 Maximal was purchased by Microsoft and the product it was selling was rebranded and sold as
“Microsoft Data Analyzer”. From Maximal, eleven employees moved over to the US where they started
working in the Office group. A year after moving, Shahar started working with the rest of the team on
Excel Services (then called Excel Server). After spending most of his life writing code, Shahar intends
to keep doing so until the medics pry the keyboard from his dead cold hands.
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page vii
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page viii
Credits
Acquisitions Editor
Katie Mohr
Senior Development Editor
Tom Dinse
Technical Editor
Justin Rockwood
Production Editor
Debra Banninger
Copy Editor
Foxxe Editorial Services
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Joseph B. Wikert
Compositor
Laurie Stewart, Happenstance Type-O-Rama
Proofreader
Sossity Smith
Indexer
Melanie Belkin
Anniversary Logo Design
Richard Pacifico
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page ix
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page x
Acknowledgments
Thanks to:
❑ The team at Wrox and especially Katie and Tom for making this book possible
❑ Justin for giving me great feedback when editing the book and for making sure all my t’s were
crossed
❑ Eran for helping when it was difficult to transform what I was thinking into words and for help-
ing with ideas for some of the solutions in the book
❑ The Excel Services team for helping with some of the issues that came up during the writing of
the book.
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page xi
04866ffirs.qxd:04866ffirs 3/15/07 8:31 PM Page xii
Content
Acknowledgments xi
Introduction xxiii
Part I: Introducing Excel Services 1
Chapter 1: Introduction to Excel Services 3
Why Use Excel Services 3
The Excel Client Farms 4
Rewriting the Models 4
How Excel Services Comes to the Rescue 5
Excel Services Goals 5
First Goal — 100% Fidelity with Excel 6
Second Goal — Security 6
Third Goal — Robustness and Reliability 6
Chapter 2: User and Administrator Cheat Sheet 7
Excel 2007 and Excel Services 7
Object Model Support 7
Addin Model Support 8
Excel Services Out of the Box 8
Excel Web Access 8
Topologies 9
Departmental Deployment 10
Large Scale Deployment 10
Unsupported Features 10
Old File Format 10
VBA 10
Query Tables 11
External Workbooks 11
Real-Time Data 11
Images and Shapes 11
Protection/Encryption 12
Other Unsupported Features 12
Roundtrip-Only Features 12
04866ftoc.qxd:04866ftoc 3/15/07 8:32 PM Page xiii
xiv
Contents
How to Administer Excel Services 12
Excel Services Settings 13
Trusted File Locations 14
User Defined Function Assemblies 15
Summary 15
Chapter 3: Inside Excel Services 17
Session, State, and Workbooks 17
Caches 18
Workbook Caches 18
Data Caches 19
Caching Calculations 19
Summary 20
Chapter 4: Programmability Options 21
Excel Web Services 21
Opening and Closing the Workbook 21
Getting Values from the Workbook 22
Writing Values to the Workbook 22
Workbook-level Interaction 22
Getting a Whole Workbook 23
Miscellaneous Methods 23
User Defined Function Assemblies 23
EWA Web Part 23
Summary 23
Part II: Excel Web Services 25
Chapter 5: Hello World Sample 27
Hello World Overview 27
Preparing the Project and Opening a Workbook 28
Making Sure the File Can Be Accessed by Excel Services 28
Preparing the Project for Use with Excel Services 30
Opening a Workbook on the Server 32
Getting a Cell from the Workbook 34
Writing Values into Cells in the Workbook 35
Using the Model Part of the Workbook 36
04866ftoc.qxd:04866ftoc 3/15/07 8:32 PM Page xiv
xv
Contents
Finishing the Sample Project 36
Updating the Model 37
Extras 38
Final Program 38
C# Hello World Program 38
VB.NET Sample Code 40
Summary 41
Chapter 6: Excel Web Services Reference 43
Direct Linking 43
Notations Used in the API Reference 43
Session Management Methods 44
OpenWorkbook 44
CloseWorkbook 46
CancelRequest 46
GetSessionInfo 48
Cell Retrieval Methods 49
Common Characteristics for GetXXX Methods 49
GetCell 52
GetCellA1 53
GetRange 54
GetRangeA1 55
Workbook Retrieval 56
GetWorkbook 57
Writing Values into Cells 57
Type and Content Restrictions with Set Methods 57
SetCell 58
SetCellA1 59
SetRange 59
SetRangeA1 60
Workbook Updating Methods 61
Calculate 62
CalculateA1 63
CalculateWorkbook 64
Refresh 64
Support Classes and Enumerations 66
RangeCoordinates 66
Status 66
04866ftoc.qxd:04866ftoc 3/15/07 8:32 PM Page xv
xvi
Contents
Exceptions and Errors 66
SoapException 67
SubCode List 68
Summary 70
Chapter 7: Building the Excel Services Library (ESL) 71
Project Design 72
Abstraction Classes 72
Namespace 73
Support Classes 74
Method Call Abstraction 76
IExcelServiceWrapper 77
IWrapperCredentials 78
Modifying ExcelServiceSoap 78
Exception Classes 80
ExcelServices Class 80
ExcelServicesType 80
Implementing ExcelServices 80
ExcelServices.Open() 82
ExcelServices.FromSessionId() 83
The Session Class 83
Dispose Code 85
Refresh Functionality 87
GetRangeA1 Functionality 88
Session Class Summary 89
The RangeResult Class 89
Example 90
How RangeResult Works 91
WorkbookData Class 96
Summary 97
Part III: User Defined Functions 99
Chapter 8: UDF Sample 101
What This Sample Contains 101
Creating a UDF Assembly 101
Required References 102
Adding the UDF Class 103
Adding the First UDF 103
Optional Parameters 108
04866ftoc.qxd:04866ftoc 3/15/07 8:32 PM Page xvi
xvii
Contents
Getting External Data 109
Using Ranges with UDFs 111
Caching Information 115
Summary 118
Chapter 9: UDF Reference 119
History 119
Excel Services UDFs 120
Execution of UDFs 120
UDF Requirements 120
The Assembly Loading Process under the Hood 121
UDF Classes’ Lifetime 122
UDF Methods 122
Cell Ranges 127
UDFs and Interop 128
Thread Safety 128
Volatility of UDFs 129
Personal Data UDFs 130
Caching Information in UDF Classes 132
Loading UDFs on the Server 134
Updating Assemblies 134
Troubleshooting UDFs 136
Debugging UDFs 138
Summary 140
Chapter 10: Client Support for Server UDFs 141
Modifying UDFs to Run on the Client 141
Add COM Support to a UDF Assembly 142
Registering the Assembly 144
Using the UDF inside Excel Client 144
Adding Client Support for GetMultiStockQuote 145
Conclusion 147
Generic Client UDF Support 147
Configuration Application 148
UdfProxy 149
CUdfAutomation 159
Final Steps 169
Improvements 170
Other Generic Solutions 170
Summary 171
04866ftoc.qxd:04866ftoc 3/15/07 8:32 PM Page xvii
xviii
Contents
Part IV: Excel Web Access 173
Chapter 11: Using Excel Web Access 175
EWA Usage 175
Web Part Properties 177
Part to Part Communication 179
Inside EWA 181
Frame Communication 182
Session Cookie 182
Summary 182
Part V: Reusable Excel Web Services Projects 183
Chapter 12: Utilizing Web Services in UDFs 185
Library Component 185
How It Works 186
About Code Generation 186
WebServiceMethodCollection and WebServiceMethod Classes 187
WebServiceParameter and WebServiceParameterCollection Classes 189
UdfDefinition Class 189
GeneratedResult Class 195
Helper Class 200
ExcelServicesHelper Class 203
ExcelHelper Class 206
UI Component 209
Summary 211
Chapter 13: Custom Web Services 213
Overview 213
Usage Example 214
Using the Custom Web Service 218
Updating the Model 219
More Type Safety 219
Generating UDFs 219
Topology 219
Coding the Solution 221
Solution Structure 221
Library 222
04866ftoc.qxd:04866ftoc 3/15/07 8:32 PM Page xviii
xix
Contents
Excel Addin 223
Web Application 230
Enumerating Web Services 233
Summary 234
Chapter 14: RSS over Excel Services 235
Overview 235
What Is RSS 236
Usage Example 236
Coding the Solution 245
Solution Structure 245
Library 245
Excel Addin 247
RSS Server 250
Summary 257
Chapter 15: Excel Services as an RTD Server 259
Overview 260
Use 260
Coding the Solution 262
How RTD Works on the Client 262
ClientSideRtd 263
Summary 271
Chapter 16: Real Time Data UDF 273
Overview 273
Use 274
Dangers of Using RTD in Excel Services 275
Coding the Solution 276
RtdUdf Project 277
Summary 285
Chapter 17: Directly ParameterizedWorkbooks 287
Overview 287
Use 287
CustomWorkbooks Project 291
Summary 295
04866ftoc.qxd:04866ftoc 3/15/07 8:32 PM Page xix
xx
Contents
Chapter 18: SQL Tables and SharePoint Lists 297
Overview 297
Use 298
Coding the Solution 306
Pes.DataUdfs Project 306
OleDbUdfs Class 311
SharePointUdfs Class 311
Summary 316
Chapter 19: External Workbook References 319
Overview 319
Use 321
Coding the Solution 328
Pes.DataUdfs Project 328
Utils Class 337
Summary 337
Chapter 20: Excel Services Workflows 339
Overview 339
Use 340
Coding the Solution 347
Pes.ExcelWorkflows Project 347
Deploying the Solution 365
Summary 368
Chapter 21: EWA Projects 369
Parameterized EWA Links 369
How It Works 369
How to Build 370
Custom XLViewer 376
How It Works 376
Coding the Solution 379
Manipulating the EWA Session 382
How It Works 382
Coding the Solution 384
Summary 392
04866ftoc.qxd:04866ftoc 3/15/07 8:32 PM Page xx
xxi
Contents
Chapter 22: Excel Data Filter 393
Overview 393
Use 394
Coding the Solution 401
The Excel Services AJAX library 401
How Part to Part Works 406
Pes.Filter Project 407
Summary 418
Chapter 23: Excel Services Mashup 419
Overview 419
Use 419
Coding the Solution 423
Workbook Model 423
EWA Page 424
UDF Assembly 425
HTML 425
Summary 430
Index 431
04866ftoc.qxd:04866ftoc 3/15/07 8:32 PM Page xxi
04866ftoc.qxd:04866ftoc 3/15/07 8:32 PM Page xxii