We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Export html from hidden worksheet

Hi,

We are having an issue with exporting of HTML from hidden worksheet.

When export html is executed for a hidden worksheet the result are unexpected.
it may export the previous exported range, on other cases, it may exports the entire workbook.

Please see the code example below, the input Excel file is in the attached zip file, the zip file also includes the output HTMLs (out1, out2) you can see that the second output is the same as the first one.

Please advice,
Thanks,
Ori

Hidden-WS.zip (16.0 KB)

        public static void ExportHiddenHtml()
    {
        Workbook wbc = null;
        using (FileStream ms = new FileStream("export_hidden.xlsx", FileMode.Open))
        {
            wbc = new Workbook(ms, new LoadOptions(LoadFormat.Xlsx));
        }

        var range = wbc.Worksheets.Names["PUBLISH_1"].GetRange();
        var worksheetIndex = range.Worksheet.Index;
        wbc.Worksheets[worksheetIndex].PageSetup.PrintArea = range.Address;
        wbc.Worksheets.ActiveSheetIndex = worksheetIndex;
        using var html = new MemoryStream();
        wbc.Save(html, HtmlSaveOptions);

        File.WriteAllBytes(@"c:\temp\out1.html", html.ToArray());


        var range2 = wbc.Worksheets.Names["PUBLISH_2"].GetRange();
        worksheetIndex = range2.Worksheet.Index;
        //if (!wbc.Worksheets[worksheetIndex].IsVisible)
        //{
        //    wbc.Worksheets[worksheetIndex].IsVisible = true;
        //}
        wbc.Worksheets[worksheetIndex].PageSetup.PrintArea = range2.Address;
        wbc.Worksheets.ActiveSheetIndex = worksheetIndex;
        using var html2 = new MemoryStream();
        wbc.Save(html2, HtmlSaveOptions);

        File.WriteAllBytes(@"c:\temp\out2.html", html2.ToArray());
        //.

    }

@orik,

Thanks for the template file.

Please try the following sample code to accomplish your task:
e.g.
Sample code:

            Workbook wbc = null;
            HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions(SaveFormat.Html);
            htmlSaveOptions.ExportActiveWorksheetOnly = true;
            htmlSaveOptions.ExportPrintAreaOnly = true;

            using (FileStream ms = new FileStream("e:\\test2\\export_hidden.xlsx", FileMode.Open))
            {
                wbc = new Workbook(ms, new LoadOptions(LoadFormat.Xlsx));
            }

            var range = wbc.Worksheets.Names["PUBLISH_1"].GetRange();
            var worksheetIndex = range.Worksheet.Index;
            wbc.Worksheets[worksheetIndex].PageSetup.PrintArea = range.Address;
            wbc.Worksheets.ActiveSheetIndex = worksheetIndex;
            var html = new MemoryStream();
            wbc.Save(html, htmlSaveOptions);

            File.WriteAllBytes("e:\\test2\\out1.html", html.ToArray());


            var range2 = wbc.Worksheets.Names["PUBLISH_2"].GetRange();
            worksheetIndex = range2.Worksheet.Index;
            if (!wbc.Worksheets[worksheetIndex].IsVisible)
            {
                wbc.Worksheets[worksheetIndex].IsVisible = true;
            }
            wbc.Worksheets[worksheetIndex].PageSetup.PrintArea = range2.Address;
            wbc.Worksheets.ActiveSheetIndex = worksheetIndex;
            var html2 = new MemoryStream();
            wbc.Save(html2, htmlSaveOptions);

            File.WriteAllBytes("e:\\test2\\out2.html", html2.ToArray());
            //. 

Hope, this helps a bit.