Thursday, 4 February 2010

Export Excel sheet to PDF document using Aspose

Last month I stumbled upon a business issue, which would allow an application to extract a single worksheet out of an excel document and save it as a PDF document.

Using Aspose, this can be done easily!

public class XlsToPdf
    {
        public void ExportSheet()
        {

            ///lets say we have an xls document containing 6 sheets
            ///each strongly named like sheet1 => sheet6
            ///suppose that you need to save sheet5 as a pdf document.

            string sourceWorkBook = "source.xls";
            string destinationDocument = "Destination.pdf";

            Aspose.Cells.Workbook sourceWb = new Aspose.Cells.Workbook();
            sourceWb.Open(sourceWorkBook);

            foreach (Aspose.Cells.Worksheet workSheet in sourceWb.Worksheets)
            {
                if (!workSheet.Name.ToUpper().Contains("sheet5"))
                    workSheet.IsVisible = false;
            }


            ///First thing to do is to save the xls in xml format, 
            ///but the formattype used already refers to PDF
            string destinationDocumentXml = "destination.xml";
            sourceWb.Save(destinationDocumentXml, 
                Aspose.Cells.FileFormatType.AsposePdf);


            Aspose.Pdf.Pdf pdfDocument = new Aspose.Pdf.Pdf();
            //bind the created xml to the PDFDoc you intialized.
            pdfDocument.BindXML(destinationDocumentXml, null);
            //Save...
            pdfDocument.Save(destinationDocument);

            ///If necessary, you can cleanup and remove 
            ///the intermediate files created
            ///using DirectoryInfo and files delet techniques from the System.IO
            ///...code goes here
        }
    }
You can find the Aspose help here: Cells and PDF

Have fun

Friday, 29 May 2009

Parser error : “The async attribute on the page directive is not allowed in this page”

Last week I bumped upon this error while I was trying to do an asynchrounous WCF service call from my "MS Project 2007 Web Access"-custom user control.

Turns out that you cannot set the Async="true" atttibute in the page directive af an aspx page in the Project Web Access.

The reason for this? Very simple ... the that SharePoint’s SPPageParserFilter class blocks some page directive attributes. When you use other attrubutes than the following list, you will get the error "The 'xxx' attribute on the page directive is not allowed in this page."

The list:

  • autoeventwireup
  • buffer
  • classname
  • codebehind
  • codepage
  • compileroptions
  • contenttype
  • culture
  • debug
  • description
  • enabletheming
  • enableviewstate
  • enableviewstatemac
  • errorpage
  • explicit
  • inherits
  • language
  • lcid
  • lineparagmas
  • maintainscrollpositiononpostback
  • masterpagefile
  • responsencoding
  • smartnavigation
  • strict
  • stylesheettheme
  • targetschema
  • theme
  • title

To take care of this issue we chose to have a webservice call which will handle the Ascynhrounous WCF call, and from our web user control we called this webservice method sycnhrounous.... Maybe this is not the best solution, but we really had an issue, so a quick fix was necessary here...

To semi-colon or not to semi-colon

Do you need to end your T-SQL statements with a semi-colon (;)?
No, at least not yet. As stated in the following web page (http://msdn.microsoft.com/en-us/library/ms143729.aspx) "Not ending Transact-SQL statements with a semi-colon." will be replaced by "End Transact-SQL statements with a semi-colon ( ; )." in
future versions...

Is it a good practice to end your statements with a semi-colon?
Yes, because:
- it will be mandatory in the future.
- you need to use it with CTE.(where the preceeding statement needs to end with a ";")
- you need to end MERGE statements with a ";"

Tuesday, 12 May 2009

Community Day 2009

Third Edition of Microsoft Community Day, a networking and knowledge sharing event.
Register yourself and be there!