Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. export excel to pdf formate in c#

export excel to pdf formate in c#

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netcomsysadmin
2 Posts 1 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pankaj Saha
    wrote on last edited by
    #1

    Hi, I have an asp.net web application. I have an excel sheet and I have to export that excel sheet to a pdf file. I have done it, but still facing some problems. 1. Whenever I do export, its open excel file. I do not want to open the excel file. 2. I have to convert the pdf file to landscape. Here is the code

    private void ExportExcelToPDF()
    {

            string sourceFilePath = Server.MapPath("~/aaa.xlsx");
            string destinationFilePath = Server.MapPath("~/aaa.pdf");
    
            Microsoft.Office.Interop.Excel.Application myExcelApp;
    
            Microsoft.Office.Interop.Excel.Workbooks myExcelWorkbooks = null;
    
            Microsoft.Office.Interop.Excel.Workbook myExcelWorkbook = null;
    
            try
            {
    
                object misValue = System.Reflection.Missing.Value;
    
                myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
    
                myExcelApp.Visible = true;
                object varMissing = Type.Missing;
    
                myExcelWorkbooks = myExcelApp.Workbooks;
    
    
                //if file already exist then delete the file
                if (System.IO.File.Exists(destinationFilePath))
                {
                    System.IO.File.Delete(destinationFilePath);
                }
    
                myExcelWorkbook = myExcelWorkbooks.Open(sourceFilePath, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
    
                myExcelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF,
                    destinationFilePath, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard,
                    varMissing, false, varMissing, varMissing, false, varMissing);
    
                myExcelWorkbooks.Close();
    
                myExcelApp.Quit();
    
            }
            catch
            {
    
            }
            finally
            {
                myExcelApp = null;
            }
    
    
        }
    

    Pankaj

    P 1 Reply Last reply
    0
    • P Pankaj Saha

      Hi, I have an asp.net web application. I have an excel sheet and I have to export that excel sheet to a pdf file. I have done it, but still facing some problems. 1. Whenever I do export, its open excel file. I do not want to open the excel file. 2. I have to convert the pdf file to landscape. Here is the code

      private void ExportExcelToPDF()
      {

              string sourceFilePath = Server.MapPath("~/aaa.xlsx");
              string destinationFilePath = Server.MapPath("~/aaa.pdf");
      
              Microsoft.Office.Interop.Excel.Application myExcelApp;
      
              Microsoft.Office.Interop.Excel.Workbooks myExcelWorkbooks = null;
      
              Microsoft.Office.Interop.Excel.Workbook myExcelWorkbook = null;
      
              try
              {
      
                  object misValue = System.Reflection.Missing.Value;
      
                  myExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
      
                  myExcelApp.Visible = true;
                  object varMissing = Type.Missing;
      
                  myExcelWorkbooks = myExcelApp.Workbooks;
      
      
                  //if file already exist then delete the file
                  if (System.IO.File.Exists(destinationFilePath))
                  {
                      System.IO.File.Delete(destinationFilePath);
                  }
      
                  myExcelWorkbook = myExcelWorkbooks.Open(sourceFilePath, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
      
                  myExcelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF,
                      destinationFilePath, Microsoft.Office.Interop.Excel.XlFixedFormatQuality.xlQualityStandard,
                      varMissing, false, varMissing, varMissing, false, varMissing);
      
                  myExcelWorkbooks.Close();
      
                  myExcelApp.Quit();
      
              }
              catch
              {
      
              }
              finally
              {
                  myExcelApp = null;
              }
      
      
          }
      

      Pankaj

      P Offline
      P Offline
      Pankaj Saha
      wrote on last edited by
      #2

      Pankaj Saha wrote:

      1. Whenever I do export, its open excel file. I do not want to open the excel file.

      I got the solution for this. Actually I did not see that little important thing. :omg:

      myExcelApp.Visible = false;

      now excel file is not opening. Now need the solution for the second problem. :confused:

      Pankaj

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups