private bool Refresh()
        {
            RemoveOldPdf();
            try
            {
                XlsFile xls = new XlsFile(XlsPath);

                PdfPath = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.InternetCache),
                    Path.ChangeExtension(Path.GetFileName(XlsUrl.Path), ".pdf"));

                using (FlexCelPdfExport pdf = new FlexCelPdfExport(xls, true))
                {
                    using (FileStream fs = new FileStream(PdfPath, FileMode.Create))
                    {
                        pdf.Export(fs);
                    }
                }
                Viewer.LoadRequest(new NSUrlRequest(NSUrl.FromFilename(PdfPath)));
            }
            catch (Exception ex)
            {
                Viewer.LoadHtmlString("<html>Error opening " + System.Security.SecurityElement.Escape(Path.GetFileName(XlsUrl.Path))
                                      + "<br><br>" + System.Security.SecurityElement.Escape(ex.Message) + "</html>", null);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
0
        private void Export(SaveFileDialog SaveDialog, bool ToPdf)
        {
            using (FlexCelReport reportStart = new FlexCelReport(true))
            {
                if (cbOffline.Checked && edCity.Text != "london")
                {
                    MessageBox.Show("Offline mode is selected, so we will show the data of london. The actual city you wrote will not be used unless you select online mode.", "Warning");
                }
                try
                {
                    string DataPath        = Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), ".."), "..") + Path.DirectorySeparatorChar;
                    string OfflineDataPath = Path.Combine(DataPath, "OfflineData") + Path.DirectorySeparatorChar;

                    //We will use a thread to connect, to avoid "freezing" the GUI
                    WebConnectThread MyWebConnect = new WebConnectThread(reportStart, edCity.Text, OfflineDataPath, cbOffline.Checked);
                    Thread           WebConnect   = new Thread(new ThreadStart(MyWebConnect.LoadData));
                    WebConnect.Start();
                    using (ProgressDialog Pg = new ProgressDialog())
                    {
                        Pg.ShowProgress(WebConnect);
                        if (MyWebConnect != null && MyWebConnect.MainException != null)
                        {
                            throw MyWebConnect.MainException;
                        }
                    }


                    if (SaveDialog.ShowDialog() == DialogResult.OK)
                    {
                        if (ToPdf)
                        {
                            XlsFile xls = new XlsFile();
                            xls.Open(DataPath + "HTML.template.xls");
                            reportStart.Run(xls);
                            using (FlexCelPdfExport PdfExport = new FlexCelPdfExport(xls, true))
                            {
                                PdfExport.Export(SaveDialog.FileName);
                            }
                        }
                        else
                        {
                            reportStart.Run(DataPath + "HTML.template.xls", SaveDialog.FileName);
                        }

                        if (MessageBox.Show("Do you want to open the generated file?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            Process.Start(SaveDialog.FileName);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Ejemplo n.º 3
0
        private void Export(SaveFileDialog SaveDialog, bool ToPdf)
        {
            try
            {
                string DataPath = Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), ".."), "..") + Path.DirectorySeparatorChar;

                //We will use a thread to connect, to avoid "freezing" the GUI
                WebConnectThread MyWebConnect = new WebConnectThread(reportStart, edcity.Text, DataPath, cbOffline.Checked, Cities);
                Thread           WebConnect   = new Thread(new ThreadStart(MyWebConnect.SetupNOAA));
                WebConnect.Start();
                using (ProgressDialog Pg = new ProgressDialog())
                {
                    Pg.ShowProgress(WebConnect);
                    if (MyWebConnect != null && MyWebConnect.MainException != null)
                    {
                        throw MyWebConnect.MainException;
                    }
                }


                if (SaveDialog.ShowDialog() == DialogResult.OK)
                {
                    if (ToPdf)
                    {
                        XlsFile xls = new XlsFile();
                        xls.Open(DataPath + "Exporting Web Services.template.xls");
                        reportStart.Run(xls);
                        using (FlexCelPdfExport PdfExport = new FlexCelPdfExport(xls, true))
                        {
                            PdfExport.Export(SaveDialog.FileName);
                        }
                    }
                    else
                    {
                        reportStart.Run(DataPath + "Exporting Web Services.template.xls", SaveDialog.FileName);
                    }

                    if (MessageBox.Show("Do you want to open the generated file?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        Process.Start(SaveDialog.FileName);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var xls = new XlsFile(1, TExcelFileFormat.v2019, true);

            xls.SetCellValue(1, 1, "Hello");
            xls.SetCellValue(2, 1, "World");
            xls.SetCellValue(3, 1, new TFormula("=A1 & \" \" & A2"));

            xls.Save("helloworld.xlsx");
            using (FlexCelPdfExport pdf = new FlexCelPdfExport(xls, true))
            {
                pdf.Export("helloworld.pdf");
            }

            Console.WriteLine("All done!");
        }
Ejemplo n.º 5
0
        private void AddData(ExcelFile Xls)
        {
            //Fill the file with the contents of this c# file, many times so we can see many page breaks.
            int Row = 3;

            DumpFile(Xls, ref Row);

            Xls.AutofitRowsOnWorkbook(false, true, 1);
            Xls.AutoPageBreaks(50, 100); // we will use a 100% of page scale since we are printing to pdf.
                                         //If this was to create an Excel file, pagescale should be lower to
                                         //compensate the differences between page sizes in diiferent printers in Excel

            //Export the file to PDF so we can see the page breaks.
            using (FlexCelPdfExport pdf = new FlexCelPdfExport(Xls, true))
            {
                pdf.Export(saveFileDialog1.FileName);
            }
        }
        public MemoryStream Export(XlsFileExBase xls)
        {
            xls.ActiveSheet = 1;

            var destStream = new MemoryStream();

            if (AdapterType == TExportExcelAdapterType.toXLS || AdapterType == TExportExcelAdapterType.toXLSx)
            {
                xls.Save(destStream, AdapterType == TExportExcelAdapterType.toXLS ? TFileFormats.Xls : TFileFormats.Xlsx);
            }

            if (AdapterType == TExportExcelAdapterType.toHTML)
            {
                var temps      = new MemoryStream();
                var htmlExport = new FlexCelHtmlExport(xls, true);

                var strHtmlColor = ColorTranslator.ToHtml(_htmlDocBkColor);
                var s            = new string[1];
                s[0] = "<body bgcolor=" + strHtmlColor + ">";
                htmlExport.ExtraInfo.BodyStart = s;
                using (var sw = new StreamWriter(temps))
                {
                    htmlExport.Export(sw, "", null);
                    sw.Flush();
                    var b = temps.ToArray();
                    destStream.Write(b, 0, b.Length);
                }
            }

            //----- PDF ---
            if (AdapterType == TExportExcelAdapterType.toPDF)
            {
                xls.PrintLandscape = false;
                using (var pdfExport = new FlexCelPdfExport(xls, true))
                {
                    pdfExport.Export(destStream);
                }
            }

            destStream.Position = 0;

            return(destStream);
        }
Ejemplo n.º 7
0
        ReportRunOutcome IExecuteEmployerReportsCommand.RunReport(EmployerReport report, bool includeCredits, IOrganisation organisation, IAdministrator accountManager, DateRange dateRange, Stream xlsOutput, Stream pdfOutput, StringBuilder stringOutput)
        {
            if (organisation == null)
            {
                throw new ArgumentNullException("organisation");
            }
            if (report.ReportAsFile && xlsOutput == null && pdfOutput == null)
            {
                throw new ArgumentException("At least one output stream must be supplied.");
            }

            using (var templateStream = GetTemplateStream(report, includeCredits))
            {
                XlsFile excelFile = null;
                if (templateStream != null)
                {
                    excelFile = new XlsFile(true);
                    excelFile.Open(templateStream);
                    FixHyperlink(excelFile, 1, "mailto:%3c%23AccountManagerEmail%3e", "mailto:" + HttpUtility.UrlEncode(accountManager.EmailAddress.Address));
                }

                var outcome = Run(report, includeCredits, organisation, accountManager, dateRange, excelFile, stringOutput);

                if (excelFile != null && (outcome == ReportRunOutcome.FileResult || report.ReportFileEvenIfNoResults))
                {
                    if (xlsOutput != null)
                    {
                        excelFile.Save(xlsOutput);
                    }

                    if (pdfOutput != null)
                    {
                        using (var export = new FlexCelPdfExport(excelFile, false))
                        {
                            export.Properties.Author = "LinkMe.com.au";
                            export.Export(pdfOutput);
                        }
                    }
                }

                return(outcome);
            }
        }
Ejemplo n.º 8
0
        private void AddData(ExcelFile Xls)
        {
            LoadUdfs(Xls);                                 //Register our custom functions. As we are using a local scope, we need to register them each time.

            Xls.Open(Path.Combine(PathToExe, "udfs.xls")); //Open the file we want to manipulate.

            //Fill the cell range with other values so we can see how the sheet is recalculated by FlexCel.
            TXlsCellRange Data = Xls.GetNamedRange("Data", -1);

            for (int r = Data.Top; r < Data.Bottom; r++)
            {
                Xls.SetCellValue(r, Data.Left, r - Data.Top);
            }

            //Add an UDF to the sheet. We can enter the fucntion "BoolChoose" here because it was registered into FlexCel in LoadUDF()
            //If it hadn't been registered, this line would raise an Exception of an unknown function.
            string FmlaText = "=BoolChoose(TRUE,\"This formula was entered with FlexCel!\",\"It shouldn't display this\")";

            Xls.SetCellValue(11, 1, new TFormula(FmlaText));

            //Verify the UDF entered is correct. We can read any udf from Excel, even if it is not registered with AddUserDefinedFunction.
            object   o  = Xls.GetCellValue(11, 1);
            TFormula fm = o as TFormula;

            Debug.Assert(fm != null, "The cell must contain a formula");
            if (fm != null)
            {
                Debug.Assert(fm.Text == FmlaText, "Error in Formula: It should be \"" + FmlaText + "\" and it is \"" + fm.Text + "\"");
            }

            //Recalc the sheet. As we are not saving it yet, we ned to make a manual recalc.
            Xls.Recalc();

            //Export the file to PDF so we can see the values calculated by FlexCel without Excel recalculating them.
            using (FlexCelPdfExport pdf = new FlexCelPdfExport(Xls, true))
            {
                pdf.Export(saveFileDialog1.FileName);
            }

            //Save the file as xls too so we can compare.
            Xls.Save(Path.ChangeExtension(saveFileDialog1.FileName, "xls"));
        }
Ejemplo n.º 9
0
 internal void ExportToPdf()
 {
     try
     {
         if (AllVisibleSheets)
         {
             try
             {
                 using (FileStream f = new FileStream(FileName, FileMode.Create, FileAccess.Write))
                 {
                     PdfExport.BeginExport(f);
                     PdfExport.PageLayout = TPageLayout.Outlines;
                     PdfExport.ExportAllVisibleSheets(false, System.IO.Path.GetFileNameWithoutExtension(FileName));
                     PdfExport.EndExport();
                 }
             }
             catch
             {
                 try
                 {
                     File.Delete(FileName);
                 }
                 catch
                 {
                     //Not here.
                 }
                 throw;
             }
         }
         else
         {
             PdfExport.PageLayout = TPageLayout.None;
             PdfExport.Export(FileName);
         }
     }
     catch (Exception ex)
     {
         FMainException = ex;
     }
 }
Ejemplo n.º 10
0
        private void DoThings()
        {
            ExcelFile xls = new XlsFile(true);

            xls.NewFile(1, TExcelFileFormat.v2019);

            for (int r = 1; r < 2000; r++)
            {
                xls.InsertHPageBreak(r); //This won't throw an exception here, since FlexCel allows to have more than 1025 page breaks, but at the moment of saving. (since an xls file can't have more than that)
            }

            xls.SetCellValue(1, 1, "We have a page break on each row, so this will print/export as one row per page");
            xls.SetCellValue(2, 1, "??? ? ? ? ???? ????"); //Since we leave the font at arial, this won't show when exporting to pdf.

            TFlxFormat fmt = xls.GetDefaultFormat;

            fmt.Font.Name = "Arial Unicode MS";
            xls.SetCellValue(3, 1, "??? ? ? ? ???? ????", xls.AddFormat(fmt)); //this will display fine in the pdf.

            fmt.Font.Name = "ThisFontDoesntExists";
            xls.SetCellValue(4, 1, "This font doesn't exists", xls.AddFormat(fmt));

            //Tahoma doesn't have italic variant. See http://help.lockergnome.com/office/Tahoma-italic-ftopict705661.html
            //You shouldn't normally use Tahoma italics in a document. If we embedded the fonts in this pdf, the fake italics wouldn't work.
            fmt.Font.Name  = "Tahoma";
            fmt.Font.Style = TFlxFontStyles.Italic;
            xls.SetCellValue(5, 1, "This is fake italics", xls.AddFormat(fmt));

            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            using (FlexCelPdfExport pdf = new FlexCelPdfExport(xls, true))
            {
                pdf.Export(Path.ChangeExtension(saveFileDialog1.FileName, ".pdf"));
            }

            xls.Save(saveFileDialog1.FileName + ".xls");
        }
Ejemplo n.º 11
0
        private void CreateFile(string FileName, bool EmbedSource, TPdfType PdfType, TTagMode TagMode)
        {
            ExcelFile xls = CreateSourceFile();

            using (FlexCelPdfExport pdf = new FlexCelPdfExport(xls, true))
            {
                pdf.PdfType = PdfType;
                pdf.TagMode = TagMode;
                if (EmbedSource)
                {
                    pdf.AttachFile("Report.xlsx", StandardMimeType.Xlsx, "This is the source file used to create the PDF", DateTime.Now, TPdfAttachmentKind.Source,
                                   delegate(TPdfAttachmentWriter attachWriter)
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            xls.Save(ms, TFileFormats.Xlsx);
                            ms.Position = 0;
                            attachWriter.Write(ms);
                        }
                    });
                }
                pdf.Export(FileName);
            }
        }
        //
        // GET: /Tally_Sheet_Export/Details/T0001
        public ActionResult Download_CM(int id)
        {
            //temp file name
            string copy_file_name = DateTime.Now.Ticks + ".xlsx";
            //template file name
            string Orginal_file_path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Report_Templates"), "Cargo_Manifest_Export.xlsx");
            //temp file path
            string copy_file_path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/App_Data"), copy_file_name);

            XlsFile cargo = new XlsFile(Orginal_file_path);

            cargo.ActiveSheet = 1;

            cargo.SetCellValue(1, 2, "Cargo Manifest - " + id);

            var cargo_details = from t in db.E_Tally_Sheet
                                join sd in db.Ship_Departure on t.ship_departure_id equals sd.ship_departure_id
                                join s in db.Ships on sd.shipp_id equals s.ship_id
                                join e in db.Employees on t.employee_id equals e.employee_id
                                where t.tally_sheet_id == id
                                select new { t.tally_sheet_id, s.ship_name, sd.starting_date, sd.ship_departure_code, e.first_name, e.middle_name, e.last_name };

            foreach (var item in cargo_details)
            {
                cargo.SetCellValue(2, 2, item.ship_name);
                cargo.SetCellValue(3, 2, item.starting_date);
                cargo.SetCellValue(4, 2, item.ship_departure_code);
                cargo.SetCellValue(5, 2, item.first_name + " " + item.middle_name + " " + item.last_name);
            }
            var cargo_goods_details = db.Cargo_Manifest_Export(id).OrderBy(e => e.way_bill_id);

            var iRowCnt = 8;

            foreach (var item in cargo_goods_details)
            {

                cargo.SetCellValue(iRowCnt, 1, item.way_bill_code);
                cargo.SetCellValue(iRowCnt, 2, item.exporter);
                cargo.SetCellValue(iRowCnt, 3, item.goods);
                cargo.SetCellValue(iRowCnt, 4, item.quantity);
                cargo.SetCellValue(iRowCnt, 5, item.unit_of_measure);
                cargo.SetCellValue(iRowCnt, 6, item.is_damaged);
                iRowCnt = iRowCnt + 1;
            }

            FlexCelPdfExport flexpdf = new FlexCelPdfExport();
            flexpdf.Workbook = cargo;
            flexpdf.AllowOverwritingFiles = true;
            flexpdf.Export(copy_file_path.Replace(".xlsx",".pdf"));

            var save_name = "Cargo_Manifest_" + id+".pdf";

            this.DownLoadFile(save_name, copy_file_name);
            this.DeleteFile(copy_file_name);

            return RedirectToAction("Index");
        }
Ejemplo n.º 13
0
        public void GeneratePDF(XlsFile xls, string Rep_name)
        {
            //create temp directory
            string merge_directory = DateTime.Now.Ticks.ToString();
            string merge_directory_path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/MergePDF"), merge_directory);
            System.IO.Directory.CreateDirectory(merge_directory_path);

            //ExportPdf(xls, filepath, Rep_name);
            //XlsFile xls = new XlsFile(path);

            xls.ActiveSheet = 1;

            string copy_file_name_import = DateTime.Now.Ticks + ".pdf";

            //temp file path
            string copy_file_path_import = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/MergePDF/" + merge_directory), copy_file_name_import);

            FlexCelPdfExport flexpdf = new FlexCelPdfExport();
            flexpdf.Workbook = xls;
            flexpdf.AllowOverwritingFiles = true;
            flexpdf.Export(copy_file_path_import);

            if (!Rep_name.ToLower().Contains("report_1_") && !Rep_name.ToLower().Contains("report_3_"))
            {
                xls.ActiveSheet = 2;

                string copy_file_name_export = DateTime.Now.Ticks + ".pdf";

                //temp file path
                string copy_file_path_export = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/MergePDF/" + merge_directory), copy_file_name_export);

                flexpdf = new FlexCelPdfExport();
                flexpdf.Workbook = xls;
                flexpdf.AllowOverwritingFiles = true;
                flexpdf.Export(copy_file_path_export);

            }
            string copy_file_name_target = DateTime.Now.Ticks + ".pdf";

            string copy_file_path_target = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Images"), copy_file_name_target);

            CreateMergedPDF(copy_file_path_target, merge_directory_path);
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + Rep_name + ".pdf");
            Response.TransmitFile(copy_file_path_target);
            Response.End();
        }