Issue with converting date formats from Excel to PDF.

I'm using C# code to convert an Excel file to a PDF. Everything works fine except the date formats, which are incorrect in the PDF, even though they are correct in the Excel file.


My date on excel file 
example 
17h00

In pdf format : 17h0 or some time i have #name


Thank you for you replay 


 



Attachment: pdf_2c34857b.zip

6 Replies

YV Yaavann Vignesh Sethuraman Syncfusion Team November 28, 2024 06:18 AM UTC

Hi  Aitbouhou Adam,

We have attempted to convert the Excel file to PDF, and the issue appears to be resolved in the latest NuGet version. We kindly request you to update the NuGet packages to the latest version and check if the issue is resolved. If the issue still persists, we recommend sharing a sample or code snippet that reproduces the problem. This will help us to validate the problem and provide a solution at the earliest.

Regards,
Yaavann.



AA Aitbouhou Adam November 28, 2024 09:58 AM UTC

Hello 


I used version 27.2.4 but I still have the same problem


Many thanks



YV Yaavann Vignesh Sethuraman Syncfusion Team November 29, 2024 06:05 AM UTC

Hi Aitbouhou Adam,


We were unable to reproduce the issue on our end. We have shared the sample we tested, along with the converted PDF document for your reference. Kindly review it. We recommend modifying the sample or providing a sample that reproduces the issue, This will help us to validate the issue and provide you a solution at the earliest.

Regards,
Yaavann.


Attachment: Sample_e150fb25.zip


AA Aitbouhou Adam November 29, 2024 01:47 PM UTC

Hello 


Thank you for your replay.


Your code works fine for me,


here is the code I use, and when I commented out this line // CalculateFormulaForPdf(wb);

that works

            System.Globalization.CultureInfo.CurrentCulture =

                                       System.Globalization.CultureInfo.GetCultureInfo("fr-FR");


            XlsIORendererSettings settings = new XlsIORendererSettings

            {

                EmbedFonts = true

            };


            using (ExcelEngine excelE = new ExcelEngine())

            {

                IApplication application = excelE.Excel;

                application.DefaultVersion = ExcelVersion.Xlsx;


                //Initialize XlsIO renderer.

                XlsIORenderer renderer = new XlsIORenderer();


                using (FileStream excelStream = new FileStream("C:\\Data\\input.xlsx", FileMode.Open, FileAccess.Read))

                {

                    IWorkbook wb = application.Workbooks.Open(excelStream);

                   CalculateFormulaForPdf(wb);


                    //Convert Excel document with charts into PDF document

                    using (PdfDocument pdfDocument = renderer.ConvertToPDF(wb, settings))

                    {

                        using (Stream s = new FileStream("C:\\Data\\output.pdf", FileMode.Create, FileAccess.ReadWrite))

                        {


                            pdfDocument.Save(s);

                        }

                    }

                }

            }


/****/

        public static void CalculateFormulaForPdf(IWorkbook workbook)

        {

            foreach (var sheet in workbook.Worksheets)

            {

                List<IRange> lstCells = sheet.UsedCells.Where(x => x.HasFormula).ToList();

                sheet.EnableSheetCalculations();

                foreach (var cell in lstCells)

                {

                    try

                    {

                        cell.Value = cell.CalculatedValue;

                    }

                    catch

                    {

                        // wrong formula inside

                    }

                }

                sheet.DisableSheetCalculations();

            }

        }



AS Atchaya Sekar Syncfusion Team December 2, 2024 01:28 PM UTC

Hi Aitbouhou Adam,

 

We were able to reproduce the reported issue and are currently validating it. We will share the validation details on December 4, 2024.

 

Regards,

Atchaya S.



YV Yaavann Vignesh Sethuraman Syncfusion Team December 4, 2024 09:56 AM UTC

Hi Aitbouhou Adam,

We have reviewed the reported issue, and it can be resolved by setting the GetValueFromArgPreserveLeadingZeros property to true. This will address the issue at the sample level. Kindly please refer to the attached code snippet and UG link shared below.
 

For more information, please refer to the following resources in our User Guide documentation:

public static void CalculateFormulaForPdf(IWorkbook workbook)
        {
            foreach (var sheet in workbook.Worksheets)
            {
                List<IRange> lstCells = sheet.UsedCells.Where(x => x.HasFormula).ToList();
                sheet.EnableSheetCalculations();
               
sheet.CalcEngine.GetValueFromArgPreserveLeadingZeros = true;;

                foreach (var cell in lstCells)
                {
                    try
                    {
                        cell.Value = cell.CalculatedValue;
                    }
                    catch
                    {
                        // wrong formula inside
                    }
                }
                sheet.DisableSheetCalculations();
            }
        }


Regards,
Yaavann.


Loader.
Up arrow icon