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

Free Support Forum - aspose.com

ASP.NET Web Form To Excel

Support,

Currently we use Aspose.Cells to export a ASP.NET page to Excel but the format is very plain with no styling. How can we keep the styling?

Here is what we do in the Load_Complete event:

'Get HTML of the page
Dim oStringWriter As New StringWriter()
Dim oHtmlTextWriter As New HtmlTextWriter(oStringWriter)

Render(oHtmlTextWriter)

Dim reader = New StringReader(oStringWriter.ToString())

Dim strContent As String = reader.ReadToEnd.ToString()

'Let strip off all the header info
strContent = Mid(strContent, InStr(strContent, “–>”) + 4)

strContent = StripTags(strContent)

'Get rid of tags
strContent = Replace(strContent, “&nbsp”, “”)
strContent = Replace(strContent, “”, “”)
strContent = Replace(strContent, “”, “”)
strContent = Replace(strContent, “”, “”)

strContent = Trim(strContent)

'Set the values for splitting the data into an array
Dim arg() As String = {vbCrLf, vbLf}

Dim objArrayList As New ArrayList(strContent.Split(arg, StringSplitOptions.RemoveEmptyEntries))

'Loop through removing items that are blank
For iLoop As Integer = objArrayList.Count - 1 To 0 Step -1
If Trim(objArrayList(iLoop).ToString()) = “” Then
objArrayList.RemoveAt(iLoop)
End If
Next

'Excel
Dim workbook As Workbook = New Workbook()
Dim sheet As Worksheet = workbook.Worksheets(0)

'Put the data into the spreadsheet
sheet.Cells.ImportArrayList(objArrayList, 1, 0, True)

'Get Worksheet Cells Collection
Dim cell As Cells = sheet.Cells

'Set Style
For i As Integer = 0 To cell.Count

<span style="color:green;">'Increase the width of First Column Width</span>
cell.SetColumnWidth(0, 150)

<span style="color:green;">'Increase the height of first row</span>
cell.SetRowHeight(0, 36)

<span style="color:green;">'Trim the extra spaces</span>
cell(i, 0).Value = Trim(cell(i, 0).Value)

<span style="color:green;">'Get Style</span>
<span style="color:blue;">Dim</span> style <span style="color:blue;">As</span> <span style="color:#2b91af;">Style</span> = cell(i, 0).GetStyle()

<span style="color:green;">'Make Cell's Text wrap</span>
style.IsTextWrapped = <span style="color:blue;">True</span>

<span style="color:green;">'Set Style</span>
cell(i, 0).SetStyle(style)

Next

'Save
workbook.Save(HttpContext.Current.Response, “Export.xls”, Aspose.Cells.ContentDisposition.Attachment, New XlsSaveOptions(Aspose.Cells.SaveFormat.Excel97To2003))

HttpContext.Current.Response.End()

Thanks,
Cean


Hi Cean,

Thanks for your posting and using Aspose.Cells.

Please provide us your actual output Excel file and the expected output Excel file to illustrate this issue. You can create your expected output Excel file manually using Microsoft Excel and post it here. It will help us look into your issue more closely and precisely and we will be able to help you asap.

Support,

Attached is the HTML page, Export spreadsheet and Expected Export spreadsheet that you asked for.

Cean

Hi Cean,

Thanks for your posting and using Aspose.Cells.

Aspose.Cells can work with Excel oriented HTML only.

You should first get the HTML of your web page and save it inside some file and then try to open it in MS-Excel. If it opens fine there, then Aspose.Cells should also be able to load it fine.

Once, you get the loadable HTML, then you should open it inside a workbook object using the following code and then save it in XLS or XLSX format which will preserve styles fine.

I have attached a sample HTML file which is loadable in Microsoft Excel. I then loaded it with Aspose.Cells and saved it in XLS format which gives correct output with styles. I have also attached the output XLS file for your reference.

Here is the code I used to convert the HTML into XLS format.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\Book1.htm”;


HTMLLoadOptions options = new HTMLLoadOptions();


//Load your html inside the workbook object

Workbook workbook = new Workbook(filePath, options);


//Save the workbook object in XLS format

workbook.Save(“output.xls”, SaveFormat.Excel97To2003);