I want to get styles, formatting of table in excel file and then return that table in html string

@John.He
And i dont want to return html i want to return html string means(excel table in table tags and added rowspan or colspan if merged cell is there)

@Amrinder_Singh
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-53504

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@Amrinder_Singh,

Please try the following code for your reference:
e.g.
Sample code:

 string ToHtmlTable(Cells cells, CellArea area)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<table>");
            for (int row = area.StartRow; row <= area.EndRow; row++)
            {
                sb.Append("<tr>");
                for (int col = area.StartColumn; col <= area.EndColumn; col++)
                {
                    Cell cell = cells.GetCell(row, col);

                    if (cell.IsMerged)
                    {
                        Range range = cell.GetMergedRange();
                        if (row == range.FirstRow || col == range.FirstColumn)
                        {
                            sb.Append("<td ");
                            if (range.ColumnCount > 1)
                            {
                                sb.Append("colspan ='");
                                sb.Append(range.CellCount);
                                sb.Append("'");
                                col += range.CellCount;
                            }
                            if (range.RowCount > 1)
                            {
                                if (row == range.FirstRow)
                                {
                                    sb.Append(" rowspan ='");
                                    sb.Append(range.RowCount);
                                    sb.Append("' ");
                                }
                            }
                            sb.Append(cell.HtmlString.Replace("<Font ", "").Replace("</Font>", ""));
                            sb.Append("</td>");
                        }
                    }
                    else
                    {
                        sb.Append("<td ");
                        sb.Append(cell.HtmlString.Replace("<Font ", "").Replace("</Font>", ""));
                        sb.Append("</td>");
                    }
                }
                sb.Append("</tr>");
            }
            sb.Append("</table>");
            return sb.ToString();
        }

and call it as following code:

 Workbook workbook = new Workbook("g:\\test2\\Bk_merged1.xlsx");
 Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];
 string table= ToHtmlTable(worksheet .Cells, CellArea.CreateCellArea("A1", "C7"));

Hope, this helps a bit.

@Eric.wang
I got the header row, while calling createCellArea, How can i get the firstcell and last cell based on the Excel sheet?Table.docx (14.5 KB)

And there is one problem with this code. when the merge is rowwise and we use e.g rowspan = ‘3’
Then the next row’s first cell is also being added as separate cell
and giving this output (see the file attached(table.docx))

@Amrinder_Singh,

We will be looking into it and get back to you soon.

@Amrinder_Singh,

You may use Cells.MinRow and Cells.MinColumn to get the first cell and use Cells.MaxRow and Cells.MaxColumn to get the last cell.
e.g.

CellArea.CreateCellArea(ws.Cells.MinRow, ws.Cells.MinColumn, ws.Cells.MaxRow, ws.Cells.MaxColumn);

I have modified the code for the problem that next row’s first cell is also being added, see the following code sample for your reference:
Sample code:

 static string ToHtmlTable(Cells cells, CellArea area)
        {
             StringBuilder sb = new StringBuilder();
        sb.Append("<table>");
        for (int row = area.StartRow; row <= area.EndRow; row++)
        {
            sb.Append("<tr>");
            for (int col = area.StartColumn; col <= area.EndColumn; col++)
            {
                Cell cell = cells[row, col];
                if(cell == null)
                {
                    continue;
                }
                if (cell.IsMerged)
                {
                    Range range = cell.GetMergedRange();
                    if (row == range.FirstRow || col == range.FirstColumn)
                    {
                        if (range.ColumnCount > 1)
                        {
                            sb.Append("<td ");
                            sb.Append("colspan ='");
                            sb.Append(range.CellCount);
                            sb.Append("'");
                            col += range.CellCount;
                        }
                        if (range.RowCount > 1)
                        {
                            if (row == range.FirstRow)
                            {
                                sb.Append("<td ");
                                sb.Append(" rowspan ='");
                                sb.Append(range.RowCount);
                                sb.Append("' ");
                            }
                            else
                            {
                                continue;
                            }
                        }
                        sb.Append(cell.HtmlString.Replace("<Font ", "").Replace("</Font>", ""));
                        sb.Append("</td>");
                    }
                }
                else
                {
                    sb.Append("<td ");
                    sb.Append(cell.HtmlString.Replace("<Font ", "").Replace("</Font>", ""));
                    sb.Append("</td>");
                }
            }
            sb.Append("</tr>");
        }
        sb.Append("</table>");
        return sb.ToString();
        }

Thank you this is working for now.

@Amrinder_Singh,

Thanks for your feedback and good to know the suggested code snippet works for your needs. Feel free to write us back if you have further queries or comments.