Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu Jul 14, 2011 7:29 pm

Is it possible to convert "XML Spreadsheet" (.xml) format to PDF? If yes, please provide code samples.

I tried following code to convert:
Code: Select all
        Dim workbook As New Workbook()
        workbook.LoadFromFile(file)


But getting the exception:
Code: Select all
[ArgumentNullException: Value cannot be null.
Parameter name: root]
   Spire.CompoundFile.XLS.Native.Storage..ctor(IStorage root, String storageName) +171
   Spire.CompoundFile.XLS.Native.Storage..ctor(IStorage root) +35
   Spire.CompoundFile.XLS.Native.CompoundFile.a(Stream A_0) +187
   Spire.Xls.Core.Spreadsheet.XlsApplication.CreateCompoundFile(Stream stream) +62
   Spire.Xls.Core.Spreadsheet.XlsWorkbook.a(Stream A_0, String A_1, ExcelVersion A_2, ExcelParseOptions A_3) +104
   Spire.Xls.Core.Spreadsheet.XlsWorkbook.a(String A_0, String A_1, ExcelVersion A_2, ExcelParseOptions A_3) +82
   Spire.Xls.Core.Spreadsheet.XlsWorkbook..ctor(IInnerApp application, Object parent, String strFileName, ExcelParseOptions options, Boolean bReadOnly, String password, ExcelVersion version) +294
   Spire.Xls.Core.Spreadsheet.XlsApplication.CreateWorkbook(Object parent, String strTemplateFile, ExcelParseOptions options, Boolean bReadOnly, String password, ExcelVersion version) +51
   Spire.Xls.Core.Spreadsheet.Collections.XlsWorkbooksCollection.Open(String fileName, ExcelParseOptions options, Boolean isReadOnly, String password, ExcelVersion version) +42
   Spire.Xls.Workbook.LoadFromFile(String fileName, ExcelVersion version) +124
   Spire.Xls.Workbook.LoadFromFile(String fileName) +39
   Test.XlsToPdf(String ArgInputFile, String ArgOutputFile) +37
   Test.Button1_Click(Object sender, EventArgs e) +72
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

doknek
 
Posts: 3
Joined: Thu Jul 14, 2011 7:16 pm

Fri Jul 15, 2011 1:39 am

Dear doknek,
Thanks for your inquiry.

It's OK to do so. But you need to use Spire.Xls.
Firstly, you can convert xml to Excel.
Sencondly, you can convert Excel to pdf.
Here is a sample code.
Code: Select all
            Workbook workbook = new Workbook();
            workbook.LoadFromXml("Xml Spreadsheet.xml");
            PdfConverter pdfConverter = new PdfConverter(workbook);

            PdfDocument pdfDocument = new PdfDocument();
            pdfDocument.PageSettings.Orientation = pdf.PdfPageOrientation.Landscape;
            pdfDocument.PageSettings.Width = 970;
            pdfDocument.PageSettings.Height = 850;

            PdfConverterSettings settings = new PdfConverterSettings();
            settings.TemplateDocument = pdfDocument;
            pdfDocument = pdfConverter.Convert(settings);

            pdfDocument.SaveToFile("test.pdf");
        }
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar

Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Fri Jul 15, 2011 2:28 pm

This works great.

1. Can we force all the columns from the spreadsheet to fit on the pdf page? Half of the columns are appearing on the next page.

2. XML worksheet has settings for WindowHeight, WindowWidth, WindowTopX, WindowTopY. Can we use these settings for PDF to make the spreadsheet fit correctly?

doknek
 
Posts: 3
Joined: Thu Jul 14, 2011 7:16 pm

Mon Jul 18, 2011 3:45 am

Dear doknek,
Thanks for your inquiry.

You can set wider pdfWidth to do so.
Code: Select all
//set wider width
pdfDocument.PageSettings.Width = 1500;
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar

Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Mon Jul 18, 2011 1:41 pm

Can I set the width automatically?

doknek
 
Posts: 3
Joined: Thu Jul 14, 2011 7:16 pm

Tue Jul 19, 2011 3:46 am

Dear doknek,
Thanks for your inquiry.
It's ok to set the width automatically.
Here is a sample code
Code: Select all
pdfDocument.PageSettings.Width = (float)getWidth(sheet);
private static double getWidth(Worksheet worksheet)
        {
            double width = 0;
            foreach (var cell in worksheet.Columns)
            {
                width += cell.ColumnWidth;
            }
            return width * 8.5;
        }
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar

Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Return to Spire.XLS