コード例 #1
0
        private void btnConvert_Click(object sender, EventArgs e)
        {
            // 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.
            ExcelEngine excelEngine = new ExcelEngine();

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

            // Workbook is created.
            IWorkbook workbook = application.Workbooks.Create(1);

            workbook.Version = ExcelVersion.Excel2016;

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

            sheet.ImportHtmlTable(GetFullTemplatePath("HTMLToExcel.html"), 1, 1);

            sheet.UsedRange.AutofitColumns();
            sheet.UsedRange.AutofitRows();

            string fileName = "Import-HTML-Table.xlsx";

            workbook.SaveAs(fileName);

            #region Workbook Close and Dispose
            //Close the excelEngine
            excelEngine.Dispose();
            #endregion

            #region View the Workbook
            //Message box confirmation to view the created spreadsheet.
            if (MessageBox.Show("Do you want to view the Workbook?", "Conversion successful",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Information)
                == DialogResult.Yes)
            {
                //Launching the Excels file using the default Application.[MS Excel Or Free ExcelViewer]
#if NETCORE
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo = new System.Diagnostics.ProcessStartInfo(fileName)
                {
                    UseShellExecute = true
                };
                process.Start();
#else
                Process.Start(fileName);
#endif
                //Exit
                this.Close();
            }
            else
            {
                // Exit
                this.Close();
            }
            #endregion
        }
コード例 #2
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.ImportHTMLTable.html");

            //Create Workbook
            IWorkbook workbook = application.Workbooks.Create(1);

            workbook.Version = ExcelVersion.Excel2016;

            IWorksheet sheet = workbook.Worksheets[0];

            sheet.ImportHtmlTable(fileStream, 1, 1);

            sheet.UsedRange.AutofitColumns();
            sheet.UsedRange.AutofitRows();

            MemoryStream stream = new MemoryStream();

            workbook.SaveAs(stream);

            stream.Position = 0;

            workbook.Close();
            excelEngine.Dispose();
            SaveAndView(stream, "application/msexcel");
        }
コード例 #3
0
        /// <summary>
        /// Imports the HTML table to Worksheet.
        /// </summary>
        /// <returns>Return the created excel document as stream</returns>
        public MemoryStream ImportHTMLTableXlsIO(string button)
        {
            if (button == "Input Template")
            {
                FileStream inputStream = new FileStream(ResolveApplicationPath("html-to-excel.html"), FileMode.Open, FileAccess.Read);
                //Save the document as a stream and retrun the stream
                using (MemoryStream stream = new MemoryStream())
                {
                    inputStream.CopyTo(stream);
                    stream.Position = 0;
                    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;

                    IWorkbook workbook = application.Workbooks.Create(1);

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

                    //An existing html is opened
                    FileStream inputStream = new FileStream(ResolveApplicationPath("html-to-excel.html"), FileMode.Open, FileAccess.Read);

                    worksheet.ImportHtmlTable(inputStream, 1, 1);

                    worksheet.UsedRange.AutofitColumns();
                    worksheet.UsedRange.AutofitRows();

                    //Save the document as a stream and retrun the stream
                    using (MemoryStream stream = new MemoryStream())
                    {
                        workbook.SaveAs(stream);
                        return(stream);
                    }
                }
            }
        }
コード例 #4
0
        public ActionResult ImportHtmlTable(string button)
        {
            string basePath = _hostingEnvironment.WebRootPath;

            if (button == null)
            {
                return(View());
            }
            else if (button == "Input Template")
            {
                Stream ms = new FileStream(basePath + @"/Import-HTML-Table.html", FileMode.Open, FileAccess.Read);
                return(File(ms, "text/html", "Import-HTML-Table.html"));
            }
            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;

                // A workbook is created.
                IWorkbook workbook = application.Workbooks.Create(1);

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

                Stream file = new FileStream(basePath + @"/Import-HTML-Table.html", FileMode.Open, FileAccess.Read);

                //Imports HTML table into the worksheet from first row and first column
                worksheet.ImportHtmlTable(file, 1, 1);

                worksheet.UsedRange.AutofitColumns();
                worksheet.UsedRange.AutofitRows();

                MemoryStream ms = new MemoryStream();
                workbook.SaveAs(ms);
                ms.Position = 0;

                excelEngine.Dispose();

                return(File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Import-HTML-Table.xlsx"));
            }
        }
        public ActionResult ImportHtmlTable(string button, string tableHTML)
        {
            if (button == null)
            {
                return(View());
            }

            MemoryStream ms = new MemoryStream();

            // 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;

                // A workbook is created.
                IWorkbook workbook = application.Workbooks.Create(1);

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

                byte[] byteArray = Encoding.UTF8.GetBytes(tableHTML);

                MemoryStream file = new MemoryStream(byteArray);

                // Imports HTML table into the worksheet from first row and first column
                worksheet.ImportHtmlTable(file, 1, 1);

                worksheet.UsedRange.AutofitColumns();
                worksheet.UsedRange.AutofitRows();

                workbook.SaveAs(ms);
                ms.Position = 0;
            }

            return(File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Export-HTML-Table.xlsx"));
        }
コード例 #6
0
        private void BtnGenerate_Clicked(object sender, System.EventArgs e)
        {
            ExcelEngine  excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;

#if COMMONSB
            Stream stream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Samples.XlsIO.Samples.Template.ImportHTMLTable.html");
#else
            Stream stream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.XlsIO.Samples.Template.ImportHTMLTable.html");
#endif
            IWorkbook workbook = application.Workbooks.Create(1);
            workbook.Version = ExcelVersion.Excel2016;

            IWorksheet worksheet = workbook.Worksheets[0];

            MemoryStream outputStream = new MemoryStream();
            string       fileName     = "ImportHTMLTable.xlsx";
            string       contentType  = "application/msexcel";

            worksheet.ImportHtmlTable(stream, 1, 1);

            worksheet.UsedRange.AutofitColumns();
            worksheet.UsedRange.AutofitRows();

            workbook.SaveAs(outputStream);

            outputStream.Position = 0;

            if (Device.RuntimePlatform == Device.UWP)
            {
                Xamarin.Forms.DependencyService.Get <ISaveWindowsPhone>().Save(fileName, contentType, outputStream);
            }
            else
            {
                Xamarin.Forms.DependencyService.Get <ISave>().Save(fileName, contentType, outputStream);
            }
        }
コード例 #7
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // 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.
            ExcelEngine excelEngine = new ExcelEngine();

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

            // A workbook is created.
            IWorkbook workbook = application.Workbooks.Create(1);

            workbook.Version = ExcelVersion.Excel2016;

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

#if NETCORE
            sheet.ImportHtmlTable(@"..\..\..\..\..\..\..\Common\Data\XlsIO\HTMLToExcel.html", 1, 1);
#else
            sheet.ImportHtmlTable(@"..\..\..\..\..\..\Common\Data\XlsIO\HTMLToExcel.html", 1, 1);
#endif
            sheet.UsedRange.AutofitColumns();

            sheet.UsedRange.AutofitRows();

            string fileName = "Import-HTML-Table.xlsx";

            workbook.SaveAs(fileName);

            //Close the excel engine.
            excelEngine.Dispose();

            //Message box confirmation to view the created spreadsheet.
            if (MessageBox.Show("Do you want to view the Workbook?", "Conversion successful",
                                MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
            {
                try
                {
                    //Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
#if NETCORE
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo = new System.Diagnostics.ProcessStartInfo(fileName)
                    {
                        UseShellExecute = true
                    };
                    process.Start();
#else
                    Process.Start(fileName);
#endif

                    //Exit
                    this.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            else
            {
                // Exit
                this.Close();
            }
        }