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

Free Support Forum - aspose.com

Converting only active sheets from Excel to Aspose - VB.net

Hi


Currently our code is successfully converting a whole Excel workbook to aspose. However this is not what our clients want. They only want the sheets that they have set as Active in the workbook converting to the new aspose version, creating a new smaller aspose cells workbook.
The total number of sheets in the Excel sheet will be unknown as will the number and location of the Active sheets. Please can you help me with the code for this as I cant currently work out a way to loop through and find the active sheets to copy to a new workbook to save.

The working code we currently have to convert and save the entire workbook is as follows:

Dim oExcel As Aspose.Cells.Workbook
oExcel = New Workbook(sSrcFilePath)
oExcel.Save(sDisplayFilePath, SaveFormat.Pdf)

Many thanks

Hi Jeremy,


Thank you for contacting Aspose support.

If you wish to export only active worksheet to the resultant PDF then you have to hide all other worksheets (leaving active worksheet visible). Please check the following piece of code for better elaboration.

VB.NET

Dim book = New Workbook(dir & “sample.xlsx”)
Dim sheets = book.Worksheets
Dim activeSheetIndex As Integer = sheets.ActiveSheetIndex
For i As Integer = 0 To sheets.Count - 1
If i = activeSheetIndex Then
Continue For
Else
sheets(i).IsVisible = False
End If
Next i
book.Save(dir & “output.pdf”, SaveFormat.Pdf)

Thanks for that code, it is heading in the right direction.

However in the excel book I am testing with Sheets 1 and 3 are active. The code is correctly seeing the first active sheet and setting and saving it but it is not seeing the 2nd active sheet. The new converted document only contains the Sheet 1 but I need Sheets 1 and 3.

My next document has a different number of sheets active and they are at different sheet locations to my first test document so I cannot hard code the number of active sheets or their locations.

Thanks

Hi,


In MS Excel only one worksheet can be made active in the Workbook. Do you mean grouped sheets (selected) in the workbook? In MS Excel, we can select multiple worksheets (in the workbook) to make them grouped sheets (still only one worksheet from the grouped sheets would be active at a time) . Well, you may easily accomplish the task, see the updated code segment below:
e.g
Sample code:

[VB]

Dim book = New Workbook(“e:\test2\Sample1.xlsx”)
Dim sheets = book.Worksheets
For i As Integer = 0 To sheets.Count - 1
If sheets(i).IsSelected Then
Continue For
Else
sheets(i).IsVisible = False
End If
Next i
book.Save(“e:\test2\out1.pdf”, SaveFormat.Pdf)

If you still have any issue or confusion or I could not understand you correctly, kindly provide more details and attach your sample Excel file here, we will check it soon.

Thank you.

Thank you, sorry I had the terminology wrong!


This is now working perfectly.

Hi Jeremy,


Thank you for the confirmation on provided solutions. Please feel free to contact us back in case you need our further assistance with Aspose APIs.