예제 #1
0
    public static void ExcelToPdf(string excelPath)
    {
        try
        {
            //Convert Excel file to PDF file
            SautinSoft.ExcelToPdf x = new SautinSoft.ExcelToPdf();
            x.PageStyle.PageSize.A4();
            //x.PageStyle.PageScale.Auto();
            x.Serial = "10003967308";

            //Set PDF format for output document
            x.OutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Pdf;

            string rootpath = System.Web.HttpContext.Current.Server.MapPath("~/");

            string pdfPath = excelPath.Replace(".xls", ".pdf");

            x.ConvertFile(excelPath, pdfPath);
        }
        catch (Exception ex)
        {
            WriteLogError(ex.Message + "\n" + excelPath);
            throw (ex);
        }
    }
예제 #2
0
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.FileName.Length == 0 || FileUpload1.FileBytes.Length == 0)
        {
            Result.Text = "Please select an Excel document for conversion!";
            return;
        }

        SautinSoft.ExcelToPdf x = new SautinSoft.ExcelToPdf();
        x.PageStyle.PageSize.Letter();


        byte[] pdfBytes = null;

        try
        {
            pdfBytes = x.ConvertBytes(FileUpload1.FileBytes);
        }
        catch { }

        //show PDF
        if (pdfBytes != null)
        {
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = "application/PDF";
            Response.AppendHeader("content-disposition", "attachment; filename=Result.pdf");
            Response.BinaryWrite(pdfBytes);
            Response.Flush();
            Response.End();
        }
        else
        {
            Result.Text = "Converting failed!";
        }
    }
예제 #3
0
        public async Task <byte[]> GenerateInvoice(InvoiceReportModel model)
        {
            using (var package = new ExcelPackage())
            {
                var workSheet = package.Workbook.Worksheets.Add($"Invoice: {model.InvoiceDate}");
                workSheet.Cells.Style.Font.Size = 14;

                ExcelRange rg = workSheet.Cells[1, 1, 6, 7];
                rg.Style.Fill.PatternType = ExcelFillStyle.Solid;
                rg.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.YellowGreen);

                workSheet.Cells["B5"].Value           = "Invoice";
                workSheet.Cells["B5"].Style.Font.Size = 28;
                workSheet.Cells["B5"].Style.Font.Color.SetColor(System.Drawing.Color.White);

                workSheet.Cells["F1"].Value = "Veterinary clinic";
                workSheet.Cells["F2"].Value = "7 Galycka street";
                workSheet.Cells["F3"].Value = "Ivano-Frankivsk";
                workSheet.Cells["F4"].Value = "Ukraine";
                workSheet.Cells["F5"].Value = "564-555-1234";
                workSheet.Cells["F1:F5"].Style.Font.Color.SetColor(System.Drawing.Color.White);
                workSheet.Cells["F1:F5"].Style.Font.Size = 14;

                workSheet.Cells["B7"].Value  = "Bill to:";
                workSheet.Cells["B8"].Value  = $"{model.FirstName}  {model.LastName}";
                workSheet.Cells["B9"].Value  = model.PhoneNumber;
                workSheet.Cells["B10"].Value = model.EmailAddress;

                workSheet.Cells["F7"].Value            = "DATE";
                workSheet.Row(7).Style.Font.Bold       = true;
                workSheet.Cells["F8"].Value            = model.InvoiceDate;
                workSheet.Cells["F9"].Value            = "INVOICE DUE DATE";
                workSheet.Cells["F10"].Value           = model.InvoiceDueDate;
                workSheet.Cells["F11"].Value           = "1234-5678-9012-3456";
                workSheet.Cells["F11"].Style.Font.Size = 14;
                workSheet.Cells["A11:G11"].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;

                workSheet.Cells["B12"].Value      = "Performed procedures:";
                workSheet.Cells["D12"].Value      = "Description";
                workSheet.Cells["F12"].Value      = "PRICE";
                workSheet.Row(12).Style.Font.Bold = true;

                int procedureCount = 0;
                if (model.Procedures != null && model.Procedures.Count > 0)
                {
                    for (int i = 1; i <= model.Procedures.Count; i++)
                    {
                        workSheet.Cells["B" + (12 + i).ToString()].Value = $"{model.Procedures[i - 1].Name}";
                        workSheet.Cells["D" + (12 + i).ToString()].Value = $"{model.Procedures[i - 1].Description}";
                        workSheet.Cells["F" + (12 + i).ToString()].Value = $"{model.Procedures[i - 1].Price}";
                        workSheet.Row(12 + i).Style.VerticalAlignment    = ExcelVerticalAlignment.Center;
                        workSheet.Row(12 + i).Style.Font.Size            = 12;
                        procedureCount++;
                    }
                }

                workSheet.Cells["A" + (12 + procedureCount).ToString() + ":G" + (12 + procedureCount).ToString()].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;

                workSheet.Cells["F" + (13 + procedureCount).ToString()].Value           = "Total:";
                workSheet.Cells["F" + (14 + procedureCount).ToString()].Value           = (model.Procedures != null && model.Procedures.Count > 0) ? model.Procedures.Sum(p => p.Price) : 0;
                workSheet.Cells["F" + (14 + procedureCount).ToString()].Style.Font.Bold = true;

                workSheet.Cells["F1:F" + (14 + procedureCount).ToString()].Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;


                ExcelRange excelRange = workSheet.Cells[1, 1, workSheet.Dimension.End.Row, workSheet.Dimension.End.Column];
                excelRange.Style.Border.BorderAround(ExcelBorderStyle.Thick);

                double minimumSize = 7;
                workSheet.Cells[workSheet.Dimension.Address].AutoFitColumns(minimumSize);

                workSheet.Column(4).AutoFit(20, 60);
                workSheet.Column(4).Style.WrapText = true;

                byte[] excelFile = await package.GetAsByteArrayAsync();

                SautinSoft.ExcelToPdf x = new SautinSoft.ExcelToPdf();
                return(x.ConvertBytes(excelFile));
            }
        }