我正在使用 WinForms,并且正在使用 RichTextBox 来使用粗体和斜体。当我插入数据库时,我将其作为 HTML 导入,以便以后能够将它们导出为 HTML 并获得相同的字体。
在尝试打印报告时,我使用了 HTMLTextBox,但似乎该控件只承认简单的标记语言,如果使用我得到的 HTML,则会崩溃。
所以为了完成我的工作,我设置了一个不可调整的字体,只允许他使用普通字体,粗体或斜体,这样CSS类在他使用所需字体时将保持不变。
我试图设置一个简化 HTML 的函数,以便能够翻译 HTML 以便在 HTMLTextBox 中被接纳。
我使用了这个函数,但它不起作用,我已经研究了 2 天,而且我是 SQL 新手。
DECLARE @HTMLpure nvarchar(max) ='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled</title><style type="text/css"> .p_CC664AAA { margin: 0px 0px 12px 0px;text-align: left;text-indent: 0pt;padding: 0px 0px 0px 0px; } .s_E5E150E4 { font-family: ''Calibri'';font-weight: bold;font-size: 16px;color: #000000; } .s_6BF1D20F { font-family: ''Calibri'';font-style: normal;font-size: 16px;color: #000000; } .s_F152B3A2 { font-family: ''Calibri'';font-style: italic;font-size: 16px;color: #000000; } .s_40C97128 { font-family: ''Calibri'';font-style: normal;font-size: 16px;color: #000000;text-decoration: underline; } </style></head><body><p class="p_CC664AAA"><span class="s_E5E150E4">BOLD</span><span class="s_6BF1D20F"> </span><span class="s_F152B3A2">iTALIC </span><span class="s_40C97128">UNDERLINE </span><span class="s_6BF1D20F">NORMAL</span></p></body></html>'
DECLARE @FinalHTML nvarchar(max)= ''
DECLARE @newHTMLpure nvarchar(max)
SET @HTMLpure = REPLACE (@HTMLpure, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled</title><style type="text/css">' , '')
set @HTMLpure = replace (@HTMLpure, '.p_CC664AAA { margin: 0px 0px 12px 0px;text-align: left;text-indent: 0pt;padding: 0px 0px 0px 0px; } ','')
set @HTMLpure = replace (@HTMLpure, '.s_E5E150E4 { font-family: ''Calibri'';font-weight: bold;font-size: 16px;color: #000000; }','')
set @HTMLpure = replace (@HTMLpure , '.s_40C97128 { font-family: ''Calibri'';font-style: normal;font-size: 16px;color: #000000;text-decoration: underline; } ','')
set @HTMLpure = replace (@HTMLpure , '.s_6BF1D20F { font-family: ''Calibri'';font-style: normal;font-size: 16px;color: #000000; } ','')
set @HTMLpure = replace (@HTMLpure , '.s_F152B3A2 { font-family: ''Calibri'';font-style: italic;font-size: 16px;color: #000000; } ','')
set @HTMLpure = REPLACE (@HTMLpure , '.p_384D933F { margin: 0px 0px 0px 0px;text-align: left;text-indent: 0pt;padding: 0px 0px 0px 0px; }' , '')
set @HTMLpure = replace (@HTMLpure ,'</style></head><body><p class="p_CC664AAA">','')
set @HTMLpure = REPLACE (@HTMLpure ,'</style></head><body><p class="p_384D933F">' , '')
set @HTMLpure = replace (@HTMLpure ,'</p></body></html>','')
WHILE len(LTRIM(@HTMLpure)) > 0
BEGIN
IF LTRIM(@HTMLpure) like '<span class="s_E5E150E4">%'
BEGIN
SET @HTMLpure = replace (LTRIM(@HTMLpure) ,'<span class="s_E5E150E4">','<strong>')
SET @HTMLpure=Stuff(@HTMLpure, CharIndex('</span>', @HTMLpure), Len('</span>'), '</strong>')
SELECT @newHTMLpure = substring(LTRIM(@HTMLpure),charindex('<strong>',LTRIM(@HTMLpure))+len('<strong>'),charindex('</strong>',LTRIM(@HTMLpure))-charindex('<strong>',LTRIM(@HTMLpure))-len('<strong>'))
SET @HTMLpure = REPLACE(@HTMLpure,'<strong>' + @newHTMLpure + '</strong>','')
SET @newHTMLpure = '<strong>' + @newHTMLpure + '</strong>'
SET @FinalHTML = @newHTMLpure
SET @newHTMLpure = ''
END
ELSE IF LTRIM(@HTMLpure) like '<span class="s_F152B3A2">%'
BEGIN
SET @HTMLpure = replace (LTRIM(@HTMLpure) ,'<span class="s_F152B3A2">','<em>')
SET @HTMLpure=Stuff(LTRIM(@HTMLpure), CharIndex('</span>', LTRIM(@HTMLpure)), Len('</span>'), '</em>')
SELECT @newHTMLpure = substring(LTRIM(@HTMLpure),charindex('<em>',LTRIM(@HTMLpure))+len('<em>'),charindex('</em>',LTRIM(@HTMLpure))-charindex('<em>',LTRIM(@HTMLpure))-len('<em>'))
SET @HTMLpure = REPLACE(LTRIM(@HTMLpure),'<em>' + @newHTMLpure + '</em>','')
SET @newHTMLpure = '<em>' + @newHTMLpure + '</em>'
SET @FinalHTML = @FinalHTML + @newHTMLpure
SET @newHTMLpure = ''
END
ELSE IF LTRIM(@HTMLpure) like '<span class="s_40C97128">%'
BEGIN
SET @HTMLpure = replace (@HTMLpure ,'<span class="s_40C97128">','<u>')
SET @HTMLpure=Stuff(@HTMLpure, CharIndex('</span>', @HTMLpure), Len('</span>'), '</u>')
SELECT @newHTMLpure = substring(@HTMLpure,charindex('<u>',@HTMLpure)+len('<u>'),charindex('</u>',@HTMLpure)-charindex('<u>',@HTMLpure)-len('<u>'))
SET @HTMLpure = REPLACE(@HTMLpure,'<u>' + @newHTMLpure + '</u>','')
SET @newHTMLpure = '<u>' + @newHTMLpure + '</u>'
SET @FinalHTML = @FinalHTML + @newHTMLpure
SET @newHTMLpure = ''
END
ELSE IF LTRIM(@HTMLpure) like '<span class="s_6BF1D20F">%'
BEGIN
SET @HTMLpure=Stuff(LTRIM(@HTMLpure), CharIndex('<span class="s_6BF1D20F">', LTRIM(@HTMLpure)), Len('<span class="s_6BF1D20F">'), ' ')
SET @HTMLpure=Stuff(LTRIM(@HTMLpure), CharIndex('</span>', LTRIM(@HTMLpure)), Len('</span>'), ' ')
SET @HTMLpure = REPLACE(LTRIM(@HTMLpure),'' + @newHTMLpure + '','')
SELECT @newHTMLpure = substring(LTRIM(@HTMLpure),charindex(' ',LTRIM(@HTMLpure))+len(' '),charindex(' ',LTRIM(@HTMLpure))-charindex('% ',LTRIM(@HTMLpure))-len(' '))\
IF @newHTMLpure = ''
BEGIN
SET @newHTMLpure = '&NBSP;'
END
SET @FinalHTML = @FinalHTML + @newHTMLpure
SET @newHTMLpure = ''
END
SET @FinalHTML = @FinalHTML + ' ' + @newHTMLpure
END
SET @HTMLpure = @FinalHTML
END
对于这么长的代码,我真的很抱歉,我知道我应该使用 vb 编码而不是 SQL,但我将其作为我在 SQL 中的练习
请帮忙,
谢谢你。