MVC excel to pdf

kadir 60 Reputation points
2024-07-18T06:35:35.71+00:00
public ActionResult Excel2(int? id)

    {

        List<TBL_Table1> FileData = db.TBL_Table1.Where(x => x.Tableid == id).ToList();

        string DosyaAdi = "";

        try

        {

            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

            var existingFilePath = Server.MapPath("~/App_Data/IsBasvuru2.xlsx");

            var existingFile = new FileInfo(existingFilePath);

            using (var memoryStream = new MemoryStream())

            {

                using (var excelPackage = new ExcelPackage(existingFile))

                {

                    var worksheet = excelPackage.Workbook.Worksheets[0]; // İlk çalışma sayfasını aç

                    foreach (var data in FileData)

                    {

                        //worksheet.Cells["D7"].Value = data.GenelBilgiid;

                        worksheet.Cells["P21"].Value = data.Ad;

                        worksheet.Cells["AN21"].Value = data.Soyad;

                        DosyaAdi = data.Ad + "-" + data.Soyad + "-" + data.GenelBilgiid + ".xlsx";

                        worksheet.Cells["AN25"].Value = data.DogumYeri;

                        worksheet.Cells["P25"].Value = Convert.ToDateTime(data.DogumTarihi).ToShortDateString();


                       

                    }


                   

                    excelPackage.SaveAs(memoryStream);

                }

                memoryStream.Position = 0;

                return File(memoryStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", DosyaAdi);

            }

        }

        catch (Exception ex)

        {

            return new ContentResult { Content = "Excel dosyası oluşturulurken bir hata oluştu: " + ex.Message };

        }

    }

    Hello everyone, friends, I have a question for you, I can create an excel file with this code, it works very healthy, but I need to convert this file to pdf, that is, it will save the data in the excel file and convert all the pages in the excel file to pdf, can you help me with this?
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,500 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 29,666 Reputation points Microsoft Vendor
    2024-07-18T09:21:00.27+00:00

    Hi @kadir,

    Perhaps you could try FreeSpire.XLS.

    In Package Manager Console:

    PM> NuGet\Install-Package FreeSpire.XLS -Version 14.2.0
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.