xlsgen > overview > Print, print preview |
xlsgen has a server and a client capability related to printing :
A PrintWith()
method is available for any worksheet, and upon being called will print said worksheet to the passed printer device name.
There are actually two parameters to this method, the second is optional and can be used to specify an actual filename, should the print job be performed by a virtual printer which in turn saves the sheets in a file instead of doing a real paper ink print. If you are not targeting a virtual printer, you can pass an empty string.
PDFCreator is an example of virtual printer suited for PDF creation purposes. It can do the work without showing user interface (which would otherwise make it unsuitable for automated scenarios).
Alternatively to actual print jobs, you can preview a print. See the section below.
To print a worksheet, here is the source code to use :
Java code |
// print this worksheet on printer "Lexmark Z700" XlsWorksheet wksht = wbk.getWorksheetByName( "sheet1" ); wksht.PrintWith("Lexmark Z700", ""); // print this worksheet on a virtual PDF printer (assuming it is installed) wksht.PrintWith("PDFPrinter", "c:\output.pdf"); ... |
VB code |
' print this worksheet on printer "Lexmark Z700" Dim wksht As IXlsWorksheet Set wksht = wbk.getWorksheetByName( "sheet1" ) wksht.PrintWith("Lexmark Z700", "") ' print this worksheet on a virtual PDF printer (assuming it is installed) wksht.PrintWith("PDFPrinter", "c:\output.pdf") ... |
C# code |
// print this worksheet on printer "Lexmark Z700" IXlsWorksheet wksht = wbk.get_WorksheetByName( "sheet1" ); wksht.PrintWith("Lexmark Z700", ""); // print this worksheet on a virtual PDF printer (assuming it is installed) wksht.PrintWith("PDFPrinter", @"c:\output.pdf"); ... |
C/C++ code |
// print this worksheet on printer "Lexmark Z700" xlsgen::IXlsWorksheetPtr wksht = wbk->WorksheetByName[L"sheet1"]; wksht.PrintWith(L"Lexmark Z700", L""); // print this worksheet on a virtual PDF printer (assuming it is installed) wksht.PrintWith(L"PDFPrinter", L"c:\\output.pdf"); ... |
In addition to actual printing, xlsgen exposes print preview method from the worksheet interface. Print preview is a way to generate reusable pictures of pages as if they were printed.
Print previews produce pictures using the PNG file format, which can be viewed with virtually any picture viewer.
Since the processing unit of a print preview is a page, producing one page at a time, a client code must iterate a method call accordingly. The corresponding source code is :
Java code |
// print preview the first worksheet // // running this code will produce a set of files of the form : // picture1.emf // picture2.emf // ... XlsWorkbook wbk = engine.Open("myfile.xls", ""); XlsWorksheet wksht = wbk.getWorksheetByIndex(1); XlsPrintPreview preview = wksht.getPrintPreview(); int i = 1; boolean bContinue; do { String s = "picture" + Integer.toString(i++) + ".emf"; bContinue = preview.PreviewNextPageAsMetafile(s); } while (bContinue); wbk.Close(); |
VB code |
' print preview the first worksheet ' ' running this code will produce a set of files of the form : ' picture1.emf ' picture2.emf ' ... Dim engine As xlsgen.CoXlsEngine Set engine = CreateObject("ExcelGenerator.ARsTDesign") Dim wbk As IXlsWorkbook Set wbk = engine.Open("myfile.xls", "") Dim wksht As IXlsWorksheet Set wksht = wbk.WorksheetByIndex(1) Dim preview As IXlsPrintPreview Set preview = wksht.PrintPreview Dim bContinue As Boolean Dim i i = 1 Do Dim s As String s = "picture" & i & ".emf" i = i + 1 bContinue = preview.PreviewNextPageAsMetafile(s) Loop While bContinue wbk.Close |
C# code |
// print preview the first worksheet // // running this code will produce a set of files of the form : // picture1.emf // picture2.emf // ... IXlsWorkbook wbk = engine.Open("myfile.xls", ""); IXlsWorksheet wksht = wbk.get_WorksheetByIndex(1); IXlsPrintPreview preview = wksht.getPrintPreview(); int i = 1; boolean bContinue; do { String s = "picture" + i.ToString() + ".emf"; i++; bContinue = preview.PreviewNextPageAsMetafile(s); } while (bContinue); wbk.Close(); |
C/C++ code |
// print preview the first worksheet // // running this code will produce a set of files of the form : // picture1.emf // picture2.emf // ... xlsgen::IXlsWorkbookPtr wbk = engine->Open(L"myfile.xls", L""); xlsgen::IXlsWorksheetPtr wksht = wbk->WorksheetByIndex[1]; xlsgen::IXlsPrintPreviewPtr preview = wksht->PrintPreview; int i = 1; BOOL bContinue = TRUE; do { WCHAR s[128]; swprintf(s, L"picture%d.emf", i++); bContinue = preview->PreviewNextPageAsMetafile(s); } while (bContinue); wbk->Close(); |
Previewing or exporting as PDF supports page setup properties exposed at the worksheet level. Client applications are encouraged to change page setup properties before making a call to create a preview or generate a PDF file. Included properties such as scaling in order to control how worksheets look like.
Aside the generic print and print preview provided for all xlsgen programming languages (VB, C/C++, Java, .NET, ...), there is an additional code sample called reportgen written with C# which allows to personalize print jobs and print previews. This is also an opportunity to get the full source code of a comprehensive client application that works on top of xlsgen.
Usage (silent print) :
// silent (server) print ReportEngine re = new ReportEngine(); ReportDocument rd = new ReportDocument(); rd.Font = new Font("Ariel", 10f); rd.OpenSpreadsheet(re, @".\..\..\Book1_print3.xls", "sheet1"); rd.PrintController = new StandardPrintController(); rd.Print(); rd.CloseSpreadsheet(); re.Release();
Usage (print preview) :
// print preview ReportEngine re = new ReportEngine(); ReportDocument rd = new ReportDocument(); rd.Font = new Font("Ariel", 10f); rd.OpenSpreadsheet(re, @".\..\..\Book1_print3.xls", "sheet1"); PrintPreviewDialog printWithDialogs = new PrintPreviewDialog(); printWithDialogs.Document = rd; printWithDialogs.WindowState = FormWindowState.Maximized; printWithDialogs.ShowDialog(); rd.CloseSpreadsheet(); re.Release();
Architecture :
Remarks :
To render charts in print, preview and PDF documents, xlsgen relies on a free charting component from Microsoft. This charting component mimics most of Excel charts.
The install of the charting component is not mandatory. If you don't care about charts, you can ignore it.
This charting component goes by the name Office Web Components (OWC). To download OWC11, please visit this web page on the Microsoft web site. It's a 18 MB download. To download OWC10 (an older version, but 100% compatible), please visit this web page on the Microsoft web site. It's a 8 MB download. Both can be installed, optionally, and in such case xlsgen will favor the use of OWC11 over OWC10.
Once downloaded, you can double-click on the executable file, owc11.exe or owc10.exe respectively. The install is silent, so you won't know whether the install went well. But xlsgen ships with a visual component discoverer tool that lists all visual components, this tool can be used to ensure that the install of the charting component actually went well. Start the visual component discoverer tool, available in the xlsgen program group. You should be able to see the corresponding entry in the Name column :
All of the above is obsolete given that xlsgen has now its own built-in chart rendering engine. Of course, if you'd rather rely on Microsoft OWC in order to render charts with xlsgen, you can do by setting the ChartEngine
property at the workbook level appropriately, and then the above becomes meaningful again.
xlsgen documentation. © ARsT Design all rights reserved.