简体   繁体   English

在将HTML字符串插入到Excel单元格之前,从其中删除换行符

[英]Remove Line breaks from HTML string before inserting it into Excel Cell

Im using Microsoft Visual Studio to extract data from a database into excel. 我正在使用Microsoft Visual Studio将数据从数据库提取到excel。 The data in the database field looks as follows: 数据库字段中的数据如下所示:

RISK TITLE &amp; DESC.:this is a test to see if the data disappears. <br /> <ol> <li>mvkf</li> <li>fmjdk </li> </ol> kfhjl<br /> <ul> <li>jfidj </li> <li>fjdilfjwkl </li> </ul> fnhdjkfhkd<br /> <a href=http://islhnweb/ target=_blank>fhsdklfjsd<br /> </a>jdfkls

When i extract the data into excel each line of text is wrritten into a seperate row as follows: 当我将数据提取到excel中时,每一行文本都被写入单独的行,如下所示:

Row 6 : RISK TITLE & DESC.:this is a test to see if the data disappears. 第6行:风险标题和DESC .:这是一项测试,以查看数据是否消失。
Row 7 : 第7行:
Row 8 : 1. mvkf 第8行:1. mvkf
Row 9 : 2. fmjdk 第9行:2。fmjdk
Row 10 : 第10行:
Row 11 : kfhji 第11行:kfhji
Row 12 : 第12行:
Row 13 : jfidj 第13行:jfidj
Row 14 : fjdilfjwkl 第14行:fjdilfjwkl
Row 15 : 第15行:
Row 16 : fnhdjkfhkd 第16行:fnhdjkfhkd
Row 17 : (link of)fhsdkfjsd 第17行:(fhsdkfjsd的链接)
Row 18 : (link of) jdfkls 第18行:(jdfkls的链接)

I need to have the whole lot of the data in one cell in 1 row, not over multiple rows. 我需要在一行中的一个单元格中存储全部数据,而不是在多行中。

My code is: 我的代码是:

case "longtext":
    tCell.Text = RLB(datarow[defrow["ColumnName"].ToString()].ToString());
    tCell.Width = 500;
    break;

Next part is as follows: 下一部分如下:

    protected string RLB(string strHTML)
    {
       StringWriter myWriter = new StringWriter();
       HttpUtility.HtmlDecode(strHTML, myWriter);
       string strResult = myWriter.ToString();
       return strResult;
    }

All help is appreciated. 感谢所有帮助。

You can try the following: 您可以尝试以下方法:

tCell.Text = RLB(datarow[defrow["ColumnName"].ToString()].ToString()).Replace(System.Environment.NewLine, String.Empty);

If it does not solve your problem, try to remove (by adding more .Replace() methods) other escape sequences, like: \\f (Formfeed); 如果它不能解决您的问题,请尝试删除(通过添加更多.Replace()方法)其他转义序列,例如:\\ f(Formfeed); \\r (Carriage return); \\ r(回车); \\v (Vertical tab). \\ v(“垂直”标签)。

Hope this will help. 希望这会有所帮助。 Best regards, 最好的祝福,

Removing the <br /> elements would probably go a fair ways toward fixing this. 删除<br />元素可能会以合理的方式解决此问题。 The <ol> elements seem to be causing the remaining problems, so those might need to be sacrificed, although since Excel does have in-cell formatting they can probably be edited back in with a bit of work after the insertion. <ol>元素似乎是造成其余问题的原因,因此,尽管Excel确实具有单元格格式,但可能需要牺牲掉这些问题,但是插入后可能需要做一些工作才能对其进行编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM