Click here to Skip to main content
15,902,636 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have some application in C# convert from the MS Excel sheet to PDFusing the Microsoft.Office.Interop.Excel

Namespace and the code is as the following:

C#
object paramMissing = Type.Missing;
XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard;
bool paramOpenAfterPublish = true;
bool paramIncludeDocProps = true;
bool paramIgnorePrintAreas = true;
object paramFromPage = Type.Missing;
object paramToPage = Type.Missing;

// Open the source workbook.
excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath.ToString(), paramMissing, paramMissing, paramMissing,
paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing,
paramMissing, paramMissing, paramMissing, paramMissing, paramMissing);
// Save it in the target format.
if (excelWorkBook != null)
excelWorkBook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath, paramExportQuality, paramIncludeDocProps,
paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing);


The problem is that after conversion from XLS or XLSX files to PDF format, the resulting files in PDF are not correct.
I mean that if there's one sheet in the XLS file it's split to about 4 or 5 pages in the PDF every 3 or 4 columns in the xls sheet is converted to a separated page in PDF.

I just want it to become only page in the pdf.

Please, if any body gets what I mean and can help me, send me or tell me about some url which may help me in doing that.

regard
Anuj jain
Posted
Updated 27-Jul-11 23:22pm
v2
Comments
Dalek Dave 28-Jul-11 5:22am    
Edited for Grammar, Syntax, Code Block and Readability.

1 solution

Hi Anuj,


Use following its working perfectly ,

C++
protected void Button1_Click(object sender, EventArgs e)
    {
        ApplicationClass excelApplication = new ApplicationClass();
        Workbook excelWorkBook = null;
        string paramSourceBookPath = @"D:\test\EmpInfo.xls";
        object paramMissing = Type.Missing;
        string paramExportFilePath = @"D:\test\EmpInfo.pdf";
        XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
        XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard;
        bool paramOpenAfterPublish = false;
        bool paramIncludeDocProps = true;
        bool paramIgnorePrintAreas = true;
        object paramFromPage = Type.Missing;
        object paramToPage = Type.Missing;

        try
        {
            // Open the source workbook.
            excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath,
                paramMissing, paramMissing, paramMissing, paramMissing,
                paramMissing, paramMissing, paramMissing, paramMissing,
                paramMissing, paramMissing, paramMissing, paramMissing,
                paramMissing, paramMissing);

            // Save it in the target format.
            if (excelWorkBook != null)
                excelWorkBook.ExportAsFixedFormat(paramExportFormat,
                    paramExportFilePath, paramExportQuality,
                    paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage,
                    paramToPage, paramOpenAfterPublish,
                    paramMissing);
        }
        catch (Exception ex)
        {
            // Respond to the error.
        }
        finally
        {
            // Close the workbook object.
            if (excelWorkBook != null)
            {
                excelWorkBook.Close(false, paramMissing, paramMissing);
                excelWorkBook = null;
            }

            // Quit Excel and release the ApplicationClass object.
            if (excelApplication != null)
            {
                excelApplication.Quit();
                excelApplication = null;
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }


    }
 
Share this answer
 
v2
Comments
Anuj kumar jain 28-Jul-11 7:42am    
thank sir 4 ur quick rply,
i have implemented the same code but still i have the same problem

I mean that if there's one sheet in the XLS file it's split to about 4 or 5 pages in the PDF every 3 or 4 columns in the xls sheet is converted to a separated page in PDF.

I just want it to become only page in the pdf.

thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900