Esempio n. 1
0
        private static void ConvertCSVToPDF(string filePath)
        {
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
            string outputFilePath           = OutputFolder + fileNameWithoutExtension + ".pdf";

            Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
            application.Visible = false;
            Workbook  workbook  = null;
            Workbooks workbooks = null;

            try
            {
                workbooks = application.Workbooks;
                workbooks.OpenText(Filename: filePath, DataType: XlTextParsingType.xlDelimited, Semicolon: true);
                workbook = workbooks[1];
                workbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, outputFilePath, XlFixedFormatQuality.xlQualityStandard,
                                             true, true, Type.Missing, Type.Missing, false, Type.Missing);
            }
            catch (System.Exception e)
            {
                log.Error("Eccezione durante la conversione del file Excel " + filePath);
                log.Error(e.StackTrace);
                File.Move(filePath, ErrorFolder + Path.GetFileName(filePath));
            }
            finally
            {
                if (workbook != null)
                {
                    workbook.Close(XlSaveAction.xlDoNotSaveChanges);
                    while (Marshal.FinalReleaseComObject(workbook) != 0)
                    {
                        ;
                    }
                }
                if (workbooks != null)
                {
                    workbooks.Close();
                    while (Marshal.FinalReleaseComObject(workbooks) != 0)
                    {
                        ;
                    }
                }
                if (application != null)
                {
                    application.Quit();
                    application.Application.Quit();
                    while (Marshal.FinalReleaseComObject(application) != 0)
                    {
                        ;
                    }
                }
                CloseProcess("EXCEL");
                File.Delete(filePath);
            }
        }