| Mick Lohan's profileThe 20 second commutePhotosBlogLists | Help |
The 20 second commuteLet's Make Programming Fun !! Code less - Declare More |
||||||||||||||||||||||||||||
|
January 27 Exporting a Multi-Table Query from SQL to ExcelThe story so far - we can filter and sort a database table and select columns to export to Excel. Ok , that's not bad .... but its not that great either ... what if you want to use a general query that combines fields from various tables? Funny enough , this didn't take much to implement either ... just a slight extension to the schema ..... then change the CmdString generation to use the sqlQuery attribute value - if a value is entered not forgetting to change the Column2Display List<Of String> ... which we use to generate the table rows .... The net result is if we use the following xml request .... contained in say <OrderDetails.xml> [Notice how we use C0 -> C12 in our query - which we can then use as the required column names in any order and only the ones we want] Then by entering the following at a command prompt [or better still - create a shortcut ...] C:\SQL2Excel OrderDetails.xmlwe get .... Download the latest version and have even more fun !! Jonathan Aneja a PM on the VB Team [these lads are really pulling out all the stops] has written a great article entitled XML Programming in Visual Basic 9.0 in the February 2008 edition of MSDN Magazine. Covers the subject very well in just 3 pages Slan [traditional Irish salutation - here's wishing you well] January 25 Exporting Sql Table Data to Excel use Linq to XML with FilteringThere I was sitting back all smug and happy with my routine until someone pointed out
Oh dear , back to the drawing board ..... It turned out that sorting and filtering was easy to include by including 2 extra attributes in the Tabledetails schema - a 'Where' attribute and an 'OrderBy' attribute where the user can either enter appropriate values or leave blank in which case the Where attribute defaults = '1=1' and the 'OrderBy' defaults to the Name value in the first Column i.e. CustomerID in this case. then the additions of 4 lines of code sets up the default values and extended the cmdString to query the database and I get
To sort out the last issue - took several cups of coffee - Internet chat with a fellow nerd - it's true what they say - a problem shared is a problem halved. - the solution ended up as being a simple cast of the appropriate attribute to string - if the attribute is null a blank value is placed in that cell. Ok - so what have we got now ? Ability to export columns from a filtered table taken from a database on your server into an excel spreadsheet[that should keep that bean-counter happy - just place the SQL2Excel.exe in his windows folder - set up an xml file containing the particular TableDetails amended to suit his query in his documents folder - create a shortcut with a command line of 'SQL2Excel <YourLayout.xml>' and as they say over here in Connemara - YOU'RE AWAY FOR SLATES [Cryptic translation : the house you're building is weather sealed and the only job left is to place the slates on the felt] Download the latest version and have some fun !! [I reserve the right to extend this app to allow the inclusion of a SQL Query to allow cross table querying - watch this space] January 21 Dynamically Export SQL Data to Excel using VB9 and LINQ to XML (Literally)Beth Massi a PM on the VB Team has been posting some great content on her blog. One in particular caught my eye - Quickly Import and Export Excel Data with LINQ to XML - as I was involved in a project where I needed to do just that. So , Beth's routine works great , but the lazy Irish Man in me took hold and I said to myself - as we're in the AGE of DECLARATIVE PROGRAMMINGWouldn't it be grand if you could use this XElement .... ... to produce this Excel Sheet
... Well - here's one solution..... download and unpack the zip file , read the SetupReadme.txt and you're on your way[This assumes you have the Northwind database loaded on a local SqlExpress server] So , how does this program work ? It works by carrying out the following Steps
In Step 4 we use Linq to XML and Expression holes to create an XElement - like sothis produces the followingThe only crib [Irishmen always complain - must be the weather] I have is that this must be saved to a file because the process.start expects to be passed a file - I wonder is there someway to pass the in memory object direct to excel .........
So , what do you have to do to produce a different report ?
My thanks to the VB team for continuing to make programming a pure joy and in particular I'd like to give a call out to Avner Aharomi who has helped to make VB Literals a reality. However , every now and then you come across a GEM of a book that really gets it where a new topic is concerned - that book in this case is LINQ in Action. It's available right now as an eBook for a measly $27.50 and compliments any good Tablet PC - you could almost say (and I will) that this is what the Tablet PC was made for. [Please read Module1.txt for an explanation of how this solution works and read Jim's Book for the complete story] September 04 Open XML Training
September 01 VBA -> Open XML Part II (cont) - Displaying a Dynamic Table
Wouldn't it be grand if you could use this XML snippet .... ... to produce this Word Document ....
... Well - here's one solution..... download and unpack the zip file , read the SetupReadme.txt and you're on your way.Being a very lazy West of Ireland guy , I never like having to do the same thing over and over again - after all, that's what programming is all about - code once, use many times. The snippet above contains 3 types of Sections (document sections if you will) - one produces the Report Title , then there's a line break section and finally there's the the table section - if you need any more types you just add to the case statement in the code (Line 81 in Module1.vb) - again extend once, use many times. With a little thinking (preferable on a hilltop over looking a stretch of water with a gentle westerly breeze in your face - and your mind full of the great ideas disseminated by a podcast from Dot Net Rocks that you've just listened to on your MP3 player) you'd be saying to yourself - All I have to do is change the Title , the SQL Query and the Column Captions and Widths and indicate if I want subtotals and I'd have a new report - but then you' ld say - Couldn't I just add a root element say <Printout> and then a 2nd element like the one above with the new details - save it off to a file and then call the file from my program [next installment] - then to produce further reports all I'd have to do is edit the xml file that is external to the program with Notepad. Don't you just love it when a plan comes together ...this is all made possible by the fact that the Office team produced their Open XML solution and the VB/C# teams collaborated on LINQ to XML - of course the VB guys just had to make it easier by extending the VB implementation with their XML literals and 'Expression holes'. A simple example should show this
[One of the most important issues in programming - is to be able to return to your code after say 6 months - and understand instantly it's intent - I would suggest that when it comes to Open XML VB is the winner - I know - I'm totally biased - but 10 years of using this technique with VB6 and VBA has converted me to the easy life - I do most of my coding in XML - isn't declarative programming great] This blog will mainly concern itself with the VB LINQ implementation and maybe a few words about Open XML but fear not an infinitely better source of Open XML knowledge is available in Wouter van Vugt's excellent free book Open XML The Markup Explained. By this stage I'm hoping that you've downloaded the zip file and have the program opened showing the Module1.vb code and the line numbers turned on [Tools - Options - Text Editor - All Languages - General - Display - Line Numbers] This module has 3 parts to it [isn't that like something out of Julius Caesar's Gallic Wars - Gaul is divided into 3 parts - boy did I not GROK Latin]
On line 46 we define an XElement variable [Printout] which contains the XML Snippet for our report - notice that there are no quotes around the XML and that in VS2008 the variable type is INFERRED from the data to the right of the equal sign. [This variable could equally be populated from an external file - Next Installment] Next on line 66 we set up our main XElement [docXML] which we'll attach directly to the root of our XDocument - like HTML Open XML uses a body element - the namespace used to interpret this is defined as w [see imports statement line 6] On Line 69 we set up an XElement [Xel] as a temporary container to hold the XML elements we will generate for each section type Now we're ready to start - this LINQ to XML really replaces XSLT - see Bill Burrows - Linq to XML videos and XLinq Portal for more info With XLINQ we can easily traverse thru' the various sections of the XML snippet using the FOR EACH statement on line 72 - note how we address the various section elements - see traversing XML in XLinq overview Let's take each section in turn
at the end of each iteration the Xel is added as an element to docXML. After the last section is traversed the resultant docXML is used to create the documentxml which is returned to the main function. The document is created and displayed. The End result - we've converted / transformed an XML Snippet into a WordprocessingML document without using a single line of incomprehensible XSLT. Fin. |
|||||||||||||||||||||||||||
|
|