예제 #1
0
        void OnConvertClicked(object sender, EventArgs e)
        {
            //Instantiate excel engine
            ExcelEngine excelEngine = new ExcelEngine();
            //Excel application
            IApplication application = excelEngine.Excel;
            //Load the input template from assembly.
            string   resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExcelToPDF.xlsx";
            Assembly assembly     = Assembly.GetExecutingAssembly();
            Stream   fileStream   = assembly.GetManifestResourceStream(resourcePath);

            //Open Workbook
            IWorkbook workbook = application.Workbooks.Open(fileStream);

            XlsIORenderer         renderer = new XlsIORenderer();
            XlsIORendererSettings settings = new XlsIORendererSettings();

            //Initialize XlsIORenderer.
            application.XlsIORenderer = new XlsIORenderer();
            ExportImageOptions imageOptions = new ExportImageOptions();

            int index = layoutList.IndexOf(selectedLayout);

            if (index == 0)
            {
                settings.LayoutOptions = LayoutOptions.NoScaling;
            }
            else if (index == 1)
            {
                settings.LayoutOptions = LayoutOptions.FitAllRowsOnOnePage;
            }
            else if (index == 2)
            {
                settings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage;
            }
            else
            {
                settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage;
            }

            PdfDocument  pdfDocument  = renderer.ConvertToPDF(workbook, settings);
            MemoryStream memoryStream = new MemoryStream();

            pdfDocument.Save(memoryStream);
            pdfDocument.Close(true);

            workbook.Close();
            excelEngine.Dispose();

            //Save and view the generated file.
            SaveAndView(memoryStream, "application/pdf");
        }
예제 #2
0
        void OnConvertClicked(object sender, EventArgs e)
        {
            //Instantiate excel engine
            ExcelEngine excelEngine = new ExcelEngine();
            //Excel application
            IApplication application = excelEngine.Excel;

            #region Initializing Workbook
            //Load the input template from assembly.
            string   resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExpenseReport.xlsx";
            Assembly assembly     = Assembly.GetExecutingAssembly();
            Stream   fileStream   = assembly.GetManifestResourceStream(resourcePath);

            //Open workbook
            IWorkbook workbook = application.Workbooks.Open(fileStream);

            //The first worksheet object in the worksheets collection is accessed.
            IWorksheet sheet = workbook.Worksheets[0];
            #endregion

            //Create a new memory stream to store the generated image.
            MemoryStream stream = new MemoryStream();

            //Initialize XlsIORenderer.
            application.XlsIORenderer = new XlsIORenderer();
            ExportImageOptions imageOptions = new ExportImageOptions();
            string             fileName     = null;
            string             ContentType  = null;
            if (jpegButton != null && (bool)jpegButton.IsChecked)
            {
                imageOptions.ImageFormat = ExportImageFormat.Jpeg;
                fileName    = "Image.jpeg";
                ContentType = "image/jpeg";
            }
            else
            {
                imageOptions.ImageFormat = ExportImageFormat.Png;
                fileName    = "Image.png";
                ContentType = "image/png";
            }

            //Convert to image
            sheet.ConvertToImage(sheet.UsedRange, imageOptions, stream);

            stream.Position = 0;

            if (stream != null)
            {
                SaveiOS iOSSave = new SaveiOS();
                iOSSave.Save(fileName, ContentType, stream);
            }
        }
예제 #3
0
        private void OnConvertButtonClicked(object sender, EventArgs e)
        {
            //Instantiate excel engine
            ExcelEngine excelEngine = new ExcelEngine();
            //Excel application
            IApplication application = excelEngine.Excel;

            //Get assembly manifest resource
            Assembly assembly   = Assembly.GetExecutingAssembly();
            Stream   fileStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.XlsIO.Template.ExpenseReport.xlsx");

            //Open Workbook
            IWorkbook workbook = application.Workbooks.Open(fileStream);

            //Get Worksheet
            IWorksheet worksheet = workbook.Worksheets[0];

            //Initialize XlsIORenderer
            application.XlsIORenderer = new XlsIORenderer();

            //Set file content type
            string             contentType  = "image/png";
            ExportImageOptions imageOptions = new ExportImageOptions();

            imageOptions.ImageFormat = ExportImageFormat.Png;

            //Create a new memory stream
            MemoryStream stream = new MemoryStream();

            if (pngButton.Checked)
            {
                worksheet.ConvertToImage(worksheet.UsedRange, imageOptions, stream);
            }
            else
            {
                imageOptions.ImageFormat = ExportImageFormat.Jpeg;
                worksheet.ConvertToImage(worksheet.UsedRange, imageOptions, stream);
                contentType = "image/jpeg";
            }

            workbook.Close();
            excelEngine.Dispose();

            //Save and view the generated file.
            SaveAndView(stream, contentType, true);
        }
        internal void OnButtonClicked(object sender, EventArgs e)
        {
            //XlsIORenderer renderer = new XlsIORenderer();
            ExcelEngine  excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;

#if COMMONSB
            Stream stream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Samples.XlsIO.Samples.Template.ExpenseReport.xlsx");
#else
            Stream stream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.XlsIO.Samples.Template.ExpenseReport.xlsx");
#endif
            IWorkbook  workbook  = application.Workbooks.Open(stream);
            IWorksheet worksheet = workbook.Worksheets[0];
            application.XlsIORenderer = new XlsIORenderer();
            MemoryStream       image        = new MemoryStream();
            ExportImageOptions imageOptions = new ExportImageOptions();
            string             fileName     = null;
            string             contentType  = null;

            if (this.jpegButton.IsChecked != null && (bool)this.jpegButton.IsChecked)
            {
                imageOptions.ImageFormat = ExportImageFormat.Jpeg;
                fileName    = "Image.jpeg";
                contentType = "image/jpeg";
            }
            else
            {
                imageOptions.ImageFormat = ExportImageFormat.Png;
                fileName    = "Image.png";
                contentType = "image/png";
            }

            //Convert to image
            worksheet.ConvertToImage(worksheet.UsedRange, imageOptions, image);

            image.Position = 0;

            if (Device.RuntimePlatform == Device.UWP)
            {
                Xamarin.Forms.DependencyService.Get <ISaveWindowsPhone>().Save(fileName, contentType, image);
            }
            else
            {
                Xamarin.Forms.DependencyService.Get <ISave>().Save(fileName, contentType, image);
            }
        }
예제 #5
0
        //
        // GET: /WorksheetToImage/

        public ActionResult WorksheetToImage(string button, string saveOption)
        {
            string basePath = _hostingEnvironment.WebRootPath;

            if (button == null)
            {
                return(View());
            }
            else if (button == "Input Template")
            {
                Stream ms = new FileStream(basePath + @"/XlsIO/ExpenseReport.xlsx", FileMode.Open, FileAccess.Read);
                return(File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Template.xlsx"));
            }
            else
            {
                // The instantiation process consists of two steps.
                // Step 1 : Instantiate the spreadsheet creation engine.
                ExcelEngine excelEngine = new ExcelEngine();

                // Step 2 : Instantiate the excel application object.
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2016;

                //Read file to memory stream
                Stream file = new FileStream(basePath + @"/XlsIO/ExpenseReport.xlsx", FileMode.Open, FileAccess.Read);

                // An existing workbook is opened.
                IWorkbook workbook = application.Workbooks.Open(file);

                // The first worksheet object in the worksheets collection is accessed.
                IWorksheet worksheet = workbook.Worksheets[0];

                try
                {
                    //Create a memory stream to store the generated image.
                    Stream image = new MemoryStream();
                    application.XlsIORenderer = new XlsIORenderer();

                    ExportImageOptions imageOptions = new ExportImageOptions()
                    {
                        ImageFormat = ExportImageFormat.Jpeg
                    };

                    //Save as JPEG image
                    if (saveOption == "jpeg")
                    {
                        worksheet.ConvertToImage(worksheet.UsedRange, imageOptions, image);
                        image.Position = 0;
                        return(File(image, "image/jpeg", "Image.jpeg"));
                    }
                    //Save as PNG image
                    else
                    {
                        imageOptions.ImageFormat = ExportImageFormat.Png;
                        worksheet.ConvertToImage(worksheet.UsedRange, imageOptions, image);
                        image.Position = 0;
                        return(File(image, "image/png", "Image.png"));
                    }
                }
                catch (Exception)
                { }
                finally
                {
                    workbook.Close();
                    excelEngine.Dispose();
                }
            }
            return(View());
        }
        /// <summary>
        /// Convert the Excel document to image
        /// </summary>
        /// <returns>Return the created excel document as stream</returns>
        public MemoryStream WorksheetToImageXlsIO(string button, string imageFormat)
        {
            if (button == "Input Document")
            {
                //New instance of XlsIO is created.[Equivalent to launching MS Excel with no workbooks open].
                //The instantiation process consists of two steps.

                //Step 1 : Instantiate the spreadsheet creation engine
                using (ExcelEngine excelEngine = new ExcelEngine())
                {
                    //Step 2 : Instantiate the excel application object
                    IApplication application = excelEngine.Excel;
                    application.DefaultVersion = ExcelVersion.Excel2016;

                    //An existing workbook is opened
                    FileStream inputStream = new FileStream(ResolveApplicationPath("ExpenseReport.xlsx"), FileMode.Open, FileAccess.Read);
                    IWorkbook  workbook    = application.Workbooks.Open(inputStream);

                    //Save the document as a stream and retrun the stream
                    using (MemoryStream stream = new MemoryStream())
                    {
                        //Save the created Excel document to MemoryStream
                        workbook.SaveAs(stream);
                        return(stream);
                    }
                }
            }
            else
            {
                //New instance of XlsIO is created.[Equivalent to launching MS Excel with no workbooks open].
                //The instantiation process consists of two steps.

                //Step 1 : Instantiate the spreadsheet creation engine
                using (ExcelEngine excelEngine = new ExcelEngine())
                {
                    //Step 2 : Instantiate the excel application object
                    IApplication application = excelEngine.Excel;
                    application.DefaultVersion = ExcelVersion.Excel2016;

                    //An existing workbook is opened
                    FileStream inputStream = new FileStream(ResolveApplicationPath("ExpenseReport.xlsx"), FileMode.Open, FileAccess.Read);
                    IWorkbook  workbook    = application.Workbooks.Open(inputStream);

                    //The first worksheet object in the worksheets collection is accessed
                    IWorksheet worksheet = workbook.Worksheets[0];

                    application.XlsIORenderer = new XlsIORenderer();

                    ExportImageOptions imageOptions = new ExportImageOptions()
                    {
                        ImageFormat = ExportImageFormat.Jpeg
                    };

                    //Save the document as a stream and retrun the stream
                    using (MemoryStream stream = new MemoryStream())
                    {
                        if (imageFormat == "PNG")
                        {
                            imageOptions.ImageFormat = ExportImageFormat.Png;
                        }
                        //Save the converted image to MemoryStream
                        worksheet.ConvertToImage(worksheet.UsedRange, imageOptions, stream);
                        return(stream);
                    }
                }
            }
        }