Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel to pdf using interop

I am converting excel file to pdf using interop. and i have a working code.

but before saving it to pdf. it prompt a dialog box which ask the user to "save changes to the file or not" how can i avoid this prompt?

and how can i close the excel when saving is done? Thank you

public string ExceltoPdf(string excelLocation, string outputLocation)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                app.Visible = false;
                Microsoft.Office.Interop.Excel.Workbook wkb = app.Workbooks.Open(excelLocation);
                wkb.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputLocation);

                wkb.Close();
                app.Quit();

                return outputLocation;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                throw ex;
            }
        }
like image 966
outlook email Avatar asked May 09 '17 23:05

outlook email


2 Answers

Try adding

app.DisplayAlerts = False

after you set .Visible.

like image 70
NetMage Avatar answered Oct 26 '22 05:10

NetMage


Open your Excel as ReadOnly

Microsoft.Office.Interop.Excel.Workbook wkb = app.Workbooks.Open(excelLocation, ReadOnly: true);
like image 45
Lakshan Dhanasekara Avatar answered Oct 26 '22 07:10

Lakshan Dhanasekara