HTML Parser to paste HTML content in Excel Cell

ONLY FORMAT font-forecolor, strong, Italics, Bold, Underline

Call this method by giving reference of Cell and HTML content.Also, attach TextFormatter.cs along with this function to your project.

private void PopulateDocumentCell(ref Aspose.Cells.Cell objCell, string HTMLTEXT)

{

try

{

//Defining Default Properties as ISBOLD^ISITALICS^ISUNDERLINE^FORECOLOR^BGCOLOR

DataTable dtStringInfo = TextFormatter.PopulateDataTableOfTagInfo(HTMLTEXT);

//Read the DataTable and populate the cell.

if (dtStringInfo.Rows.Count > 0)

{

//Populate String.

string strCellContent = string.Empty;

foreach (DataRow dr in dtStringInfo.Rows)

{

strCellContent += dr["Text"];

}

objCell.PutValue((string) strCellContent);

//Now format the text.

int intStartPosition = 0;

foreach (DataRow dr in dtStringInfo.Rows)

{

if ((dr["Bold"].ToString() == "1"))

{

objCell.Characters(intStartPosition, dr["Text"].ToString().Length).Font.IsBold = true;

}

if ((dr["Italics"].ToString() == "1"))

{

objCell.Characters(intStartPosition, dr["Text"].ToString().Length).Font.IsItalic = true;

}

if (dr["UnderLine"].ToString() == "1")

{

objCell.Characters(intStartPosition, dr["Text"].ToString().Length).Font.Underline = FontUnderlineType.Single;

}

if(dr["ForeColor"].ToString()!="#000000")

{

objCell.Characters(intStartPosition, dr["Text"].ToString().Length).Font.Color =

TextFormatter.HexStringToColor(dr["ForeColor"].ToString());

}

intStartPosition += dr["Text"].ToString().Length;

}

}

objCell.Style.VerticalAlignment = TextAlignmentType.Top;

objCell.Style.IsTextWrapped = true;

}

catch (Exception)

{

string strValue = TextFormatter.GetPlainText(HTMLTEXT);

while (strValue.Length > 2)

{

if (strValue.Substring(0, 1) == "\n")

{

strValue = strValue.Substring(1, strValue.Length - 1);

}

else

{

break;

}

}

objCell.PutValue((string) strValue);

objCell.Style.VerticalAlignment = TextAlignmentType.Top;

objCell.Style.IsTextWrapped = true;

}

}

Thank you for your code. However our forums don’t allow to download .cs file. Could you please zip your .cs file and post the zip file here? Thank you very much.

Please find the attached zip file.

Could you please let me kow how to access your attachment. Looks like it is not accessible.


Thanks,
Cpunji

Hi Cpunji,


I guess the attachments attached by the user won’t help you much as this is an older thread.
We included lots of changes and embedded many enhancements in Aspose.Cells component over the years. We recommend you to kindly try our latest version/fix: Aspose.Cells for .NET v7.5.2:
http://www.aspose.com/community/files/51/.net-components/aspose.cells-for-.net/entry488800.aspx

Moreover, if you find any issue, kindly elaborate your issue and requirements, so we could help you accordingly. You may also start a new thread, it would be better for technical assistance.

Thank you.