Excel to Html with multiple sheets

Hi we are using Aspose.Cells 8.3.2.1 .NET C#


We are looking to convert a spreadsheet file to hml but cannot seem to get this to work if we have multiple sheets.

the following message is output as html "This page uses frames, but your browser doesn’t support them."

Can you please give me guidance on how to output multiple sheets into html?


Hi Graeme,


Thank you for contacting Aspose support.

Please note, as per default behaviour, Aspose.Cells APIs convert the spreadsheets to HTML by generating the frame set where contents of each individual worksheet renders in separate frame, giving the feel of Excel UI where each individual worksheet has its tab. However, we haven’t faced such issue before as “your browser doesn’t support frames”. I believe recent revisions of all browsers support them. I have tested the case in Internet Explorer, Opera, Chrome & Firefox. Could it be a reason that you have disabled this feature in your browser settings?

Anyway, you can also avoid the generation of frames by converting one worksheet at a time. Please check the following piece of code for elaboration.

C#

var book = new Workbook(dir + “sample.xlsx”);
foreach (Worksheet sheet in book.Worksheets)
{
book.Worksheets.ActiveSheetIndex = sheet.Index;
book.Save(dir + sheet.Name + “.html”, new HtmlSaveOptions() { ExportActiveWorksheetOnly = true });
}

Hi Babar,


Thanks for this. I got this working. also for reference i managed to achieve the same as this using the below code, however i am now hitting an issue where the html is rendering half way off the page and dosent appear to be taking into account the spreadsheet print settings. Is there a way to say fit all columns on one page when rendering as html?

for(int i = 1; i < workSheetCount; i++)
{

var currentWorkSheet =
documentTemplateSpreadsheet.Worksheets[1];
int insertPoint =
documentTemplateSpreadsheet.Worksheets[0].Cells.Rows.Count;
Aspose.Cells.Range range = documentTemplateSpreadsheet.Worksheets[1].Cells.CreateRange(0, 0, documentTemplateSpreadsheet.Worksheets[1].Cells.Rows.Count, documentTemplateSpreadsheet.Worksheets[1].Cells.Columns.Count);
Aspose.Cells.Range range2 = documentTemplateSpreadsheet.Worksheets[0].Cells.CreateRange(insertPoint, 0, documentTemplateSpreadsheet.Worksheets[1].Cells.Rows.Count, documentTemplateSpreadsheet.Worksheets[1].Cells.Columns.Count);
range2.Copy(range);
documentTemplateSpreadsheet.Worksheets[0].Cells.CopyRows(documentTemplateSpreadsheet.Worksheets[1].Cells, 0, insertPoint, 1);
documentTemplateSpreadsheet.Worksheets.RemoveAt(1);
}


Hi again,


I am afraid, there is no such options as “Fit All Columns on One Page” while rendering spreadsheets to HTML, moreover, the Page Setup settings will not be considered for this type of conversion. If you are facing any issue, please provide us an executable sample application along with its dependencies and input/output files for our testing.

Ah ok thats not good for what i need. Ok i have anew approach which is to convert the Excel document into a pdf document however when i do this i get page breaks between all of the sheets when rendered to the PDF. Id there a way to stop the page breaks and have each sheet running together?

Hi Graeme,

GraemeBaillie:
Id there a way to stop the page breaks and have each sheet running together?

Do you mean to render all contents of worksheet to one page of PDF? If yes, you can use the PdfSaveOptions.OnePagePerSheet property. By setting it to true, the API will ignore the page breaks and render all contents of the worksheet on single page of PDF. However, if you have multiple worksheets in a spreadsheet then you will have one PDF page per worksheet.

C#

Workbook workbook = new Workbook(dir + "sample.xlsx");
workbook.Save( dir + "output.pdf", new PdfSaveOptions() { OnePagePerSheet = true});

I have just tried this as it still gives you a separate pdf page for each sheet. Is there no way to have multiple sheets on the same page but allow the contents to spill over multiple pages?

Hi Graeme,


You can copy all contents of different worksheet onto single worksheet before rendering the resultant spreadsheet to PDF. Please check following piece of code and related articles.


C#

Workbook workbook = new Workbook(dir + “book1.xlsx”);
Worksheet target = workbook.Worksheets[0];
for (int i = 1; i < workbook.Worksheets.Count; i++)
{
Worksheet source = workbook.Worksheets[i];
Range sourceRange = source.Cells.MaxDisplayRange;
target.Cells.CreateRange(target.Cells.MaxDataRow + 1, 0, sourceRange.RowCount, sourceRange.ColumnCount).Copy(sourceRange);
workbook.Worksheets.RemoveAt(i);
}
workbook.Save( dir + “output.pdf”, new PdfSaveOptions() { AllColumnsInOnePagePerSheet = true});