Nice! Very slick.
I was disappointed that Excel doesn’t let us paste to a merged cell and also pastes results containing a break into successive rows below the «target» cell though, as that meant it simply doesn’t work for me. I tried a few tweaks (unmerge/remerge, etc.) but then Excel dropped anything below a break, so that was a dead end.
Ultimately, I came up with a routine that’ll handle simple tags and not use the «native» Unicode converter that is causing the issue with merged fields. Hope others find this useful:
Public Sub AddHTMLFormattedText(rngA As Range, strHTML As String, Optional blnShowBadHTMLWarning As Boolean = False)
' Adds converts text formatted with basic HTML tags to formatted text in an Excel cell
' NOTE: Font Sizes not handled perfectly per HTML standard, but I find this method more useful!
Dim strActualText As String, intSrcPos As Integer, intDestPos As Integer, intDestSrcEquiv() As Integer
Dim varyTags As Variant, varTag As Variant, varEndTag As Variant, blnTagMatch As Boolean
Dim intCtr As Integer
Dim intStartPos As Integer, intEndPos As Integer, intActualStartPos As Integer, intActualEndPos As Integer
Dim intFontSizeStartPos As Integer, intFontSizeEndPos As Integer, intFontSize As Integer
varyTags = Array("<b>", "</b>", "<i>", "</i>", "<u>", "</u>", "<sub>", "</sub>", "<sup>", "</sup>")
' Remove unhandled/unneeded tags, convert <br> and <p> tags to line feeds
strHTML = Trim(strHTML)
strHTML = Replace(strHTML, "<html>", "")
strHTML = Replace(strHTML, "</html>", "")
strHTML = Replace(strHTML, "<p>", "")
While LCase(Right$(strHTML, 4)) = "</p>" Or LCase(Right$(strHTML, 4)) = "<br>"
strHTML = Left$(strHTML, Len(strHTML) - 4)
strHTML = Trim(strHTML)
Wend
strHTML = Replace(strHTML, "<br>", vbLf)
strHTML = Replace(strHTML, "</p>", vbLf)
strHTML = Trim(strHTML)
ReDim intDestSrcEquiv(1 To Len(strHTML))
strActualText = ""
intSrcPos = 1
intDestPos = 1
Do While intSrcPos <= Len(strHTML)
blnTagMatch = False
For Each varTag In varyTags
If LCase(Mid$(strHTML, intSrcPos, Len(varTag))) = varTag Then
blnTagMatch = True
intSrcPos = intSrcPos + Len(varTag)
If intSrcPos > Len(strHTML) Then Exit Do
Exit For
End If
Next
If blnTagMatch = False Then
varTag = "<font size"
If LCase(Mid$(strHTML, intSrcPos, Len(varTag))) = varTag Then
blnTagMatch = True
intEndPos = InStr(intSrcPos, strHTML, ">")
intSrcPos = intEndPos + 1
If intSrcPos > Len(strHTML) Then Exit Do
Else
varTag = "</font>"
If LCase(Mid$(strHTML, intSrcPos, Len(varTag))) = varTag Then
blnTagMatch = True
intSrcPos = intSrcPos + Len(varTag)
If intSrcPos > Len(strHTML) Then Exit Do
End If
End If
End If
If blnTagMatch = False Then
strActualText = strActualText & Mid$(strHTML, intSrcPos, 1)
intDestSrcEquiv(intSrcPos) = intDestPos
intDestPos = intDestPos + 1
intSrcPos = intSrcPos + 1
End If
Loop
' Clear any bold/underline/italic/superscript/subscript formatting from cell
rngA.Font.Bold = False
rngA.Font.Underline = False
rngA.Font.Italic = False
rngA.Font.Subscript = False
rngA.Font.Superscript = False
rngA.Value = strActualText
' Now start applying Formats!"
' Start with Font Size first
intSrcPos = 1
intDestPos = 1
Do While intSrcPos <= Len(strHTML)
varTag = "<font size"
If LCase(Mid$(strHTML, intSrcPos, Len(varTag))) = varTag Then
intFontSizeStartPos = InStr(intSrcPos, strHTML, """") + 1
intFontSizeEndPos = InStr(intFontSizeStartPos, strHTML, """") - 1
If intFontSizeEndPos - intFontSizeStartPos <= 3 And intFontSizeEndPos - intFontSizeStartPos > 0 Then
Debug.Print Mid$(strHTML, intFontSizeStartPos, intFontSizeEndPos - intFontSizeStartPos + 1)
If Mid$(strHTML, intFontSizeStartPos, 1) = "+" Then
intFontSizeStartPos = intFontSizeStartPos + 1
intFontSize = 11 + 2 * Mid$(strHTML, intFontSizeStartPos, intFontSizeEndPos - intFontSizeStartPos + 1)
ElseIf Mid$(strHTML, intFontSizeStartPos, 1) = "-" Then
intFontSizeStartPos = intFontSizeStartPos + 1
intFontSize = 11 - 2 * Mid$(strHTML, intFontSizeStartPos, intFontSizeEndPos - intFontSizeStartPos + 1)
Else
intFontSize = Mid$(strHTML, intFontSizeStartPos, intFontSizeEndPos - intFontSizeStartPos + 1)
End If
Else
' Error!
GoTo HTML_Err
End If
intEndPos = InStr(intSrcPos, strHTML, ">")
intSrcPos = intEndPos + 1
intStartPos = intSrcPos
If intSrcPos > Len(strHTML) Then Exit Do
While intDestSrcEquiv(intStartPos) = 0 And intStartPos < Len(strHTML)
intStartPos = intStartPos + 1
Wend
If intStartPos >= Len(strHTML) Then GoTo HTML_Err ' HTML is bad!
varEndTag = "</font>"
intEndPos = InStr(intSrcPos, LCase(strHTML), varEndTag)
If intEndPos = 0 Then GoTo HTML_Err ' HTML is bad!
While intDestSrcEquiv(intEndPos) = 0 And intEndPos > intSrcPos
intEndPos = intEndPos - 1
Wend
If intEndPos > intSrcPos Then
intActualStartPos = intDestSrcEquiv(intStartPos)
intActualEndPos = intDestSrcEquiv(intEndPos)
rngA.Characters(intActualStartPos, intActualEndPos - intActualStartPos + 1) _
.Font.Size = intFontSize
End If
End If
intSrcPos = intSrcPos + 1
Loop
'Now do remaining tags
intSrcPos = 1
intDestPos = 1
Do While intSrcPos <= Len(strHTML)
If intDestSrcEquiv(intSrcPos) = 0 Then
' This must be a Tag!
For intCtr = 0 To UBound(varyTags) Step 2
varTag = varyTags(intCtr)
intStartPos = intSrcPos + Len(varTag)
While intDestSrcEquiv(intStartPos) = 0 And intStartPos < Len(strHTML)
intStartPos = intStartPos + 1
Wend
If intStartPos >= Len(strHTML) Then GoTo HTML_Err ' HTML is bad!
If LCase(Mid$(strHTML, intSrcPos, Len(varTag))) = varTag Then
varEndTag = varyTags(intCtr + 1)
intEndPos = InStr(intSrcPos, LCase(strHTML), varEndTag)
If intEndPos = 0 Then GoTo HTML_Err ' HTML is bad!
While intDestSrcEquiv(intEndPos) = 0 And intEndPos > intSrcPos
intEndPos = intEndPos - 1
Wend
If intEndPos > intSrcPos Then
intActualStartPos = intDestSrcEquiv(intStartPos)
intActualEndPos = intDestSrcEquiv(intEndPos)
With rngA.Characters(intActualStartPos, intActualEndPos - intActualStartPos + 1).Font
If varTag = "<b>" Then
.Bold = True
ElseIf varTag = "<i>" Then
.Italic = True
ElseIf varTag = "<u>" Then
.Underline = True
ElseIf varTag = "<sup>" Then
.Superscript = True
ElseIf varTag = "<sub>" Then
.Subscript = True
End If
End With
End If
intSrcPos = intSrcPos + Len(varTag) - 1
Exit For
End If
Next
End If
intSrcPos = intSrcPos + 1
intDestPos = intDestPos + 1
Loop
Exit_Sub:
Exit Sub
HTML_Err:
' There was an error with the Tags. Show warning if requested.
If blnShowBadHTMLWarning Then
MsgBox "There was an error with the Tags in the HTML file. Could not apply formatting."
End If
End Sub
Note this doesn’t care about tag nesting, instead only requiring a close tag for every open tag, and assuming the close tag nearest the opening tag applies to the opening tag. Properly nested tags will work fine, while improperly nested tags will not be rejected and may or may not work.
Содержание
- Html formatted text in excel
- Answered by:
- Question
- HTML Text with tags to formatted text in an Excel cell
- 7 Answers 7
- Format HTML table cell so that Excel formats as text?
- 7 Answers 7
- Html formatted text in excel
- Answered by:
- Question
- Convert a Formatted Cell in Excel into HTML Tag using VBA
- Convert Formatted Cell in Excel to HTML
- Excel Worksheet Cell with Single format to HTML
- Convert Cell in Excel to HTML – Multiple Format Text
Html formatted text in excel
This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.
Answered by:
Question
My name is Raghav Sharma . I am working in the software company . From past day i am working on the generating excel pro grammatically using interop classes. Everything working for me but when i am coming to finish the excel work . I found a problem .The problem is this when we add the html code progrmmatically in the excel column it show the same as it. For example
If i add this html text (sample) then it show as it but i need to show the «sample» text bold. So my question is can we show the html code in the excel cell if yes can any one show my how this can be done?
one more thing when i copy the html code from some site or some html formatted content which is already html formatted then copy that text in the excel its working fine
I am using asp.net 3.5 framework c# and developing website which generates the report and show in the excel.
If any one familiar about this please reply me as soon as possible
Источник
HTML Text with tags to formatted text in an Excel cell
Is there a way to take HTML and import it to excel so that it is formatted as rich text (preferably by using VBA)? Basically, when I paste to an Excel cell, I’m looking to turn this:
This is a test. Will this text be bold or italic
7 Answers 7
Yes it is possible. In fact let Internet Explorer do the dirty work for you.
MY ASSUMPTIONS
- I am assuming that the html text is in Cell A1 of Sheet1. You can also use a variable instead.
- If you have a column full of html values, then simply put the below code in a loop
CODE
SNAPSHOT
I ran into the same error that BornToCode first identified in the comments of the original solution. Being unfamiliar with Excel and VBA it took me a second to figure out how to implement tiQU’s solution. So I’m posting it as a «For Dummies» solution below
- First enable developer mode in Excel: Link
- Select the Developer Tab > Visual Basic
- Click View > Code
- Paste the code below updating the lines that require cell references to be correct.
- Click the Green Run Arrow or press F5
You can copy the HTML code to the clipboard and paste special it back as Unicode text. Excel will render the HTML in the cell. Check out this post http://www.dailydoseofexcel.com/archives/2005/02/23/html-in-cells-ii/
The relevant macro code from the post:
If the IE example doesn’t work use this one. Anyway this should be faster than starting up an instance of IE.
Note, if your innerHTML is all numbers eg ‘12345’, HTML formatting dosen’t fully work in excel as it treats number differently? but add a character eg a trailing space at the end eg. 12345 + «& nbsp;» formats ok.
I know this thread is ancient, but after assigning the innerHTML, ExecWB worked for me:
And then just paste the contents into Excel. Since these methods are prone to runtime errors, but work fine after one or two tries in debug mode, you might have to tell Excel to try again if it runs into an error. I solved this by adding this error handler to the sub, and it works fine:
Nice! Very slick.
I was disappointed that Excel doesn’t let us paste to a merged cell and also pastes results containing a break into successive rows below the «target» cell though, as that meant it simply doesn’t work for me. I tried a few tweaks (unmerge/remerge, etc.) but then Excel dropped anything below a break, so that was a dead end.
Ultimately, I came up with a routine that’ll handle simple tags and not use the «native» Unicode converter that is causing the issue with merged fields. Hope others find this useful:
Note this doesn’t care about tag nesting, instead only requiring a close tag for every open tag, and assuming the close tag nearest the opening tag applies to the opening tag. Properly nested tags will work fine, while improperly nested tags will not be rejected and may or may not work.
Источник
Format HTML table cell so that Excel formats as text?
I am creating an HTML table that will be opened as a spreadsheet in Excel. What HTML tag or CSS style can I use to «tell» Excel to display the cell’s contents as text?
7 Answers 7
You can apply formatting to the cells for numbers, text, dates, etc.
(adjusted snippet)
If you add a CSS Class to your page:
And slap those classes on your TD’s, does it work?
For anyone with special number formats: you can save Excel file in .htm format and search for the mso-number-format in the generated files (or advanced way: copy the cell and get the HTML from the Clipboard). The CSS can also be inlined
2345
, but that can make the file bigger.
There is one problem using that solution (css style with number-format). The Excel gives the error «Number Stored as text» which can be inconvenient in some cases. To avoid this problem it’s possible to use the ZERO WIDTH SPACE character () in the begining of the field.
You can solve the problem too by adding non-breaking space: before the value of the
element.
Example: 0:12:12.185
Instead of:
0:12:12.185
I don’t have enough rep to comment or up-vote, but Raposo’s answer worked very well for me. Our system imports SSRS reports and runs them in local mode. It stores the DataSet query(ies) in the database and pulls them at runtime. However for Excel exports it just runs the query’s resulting data into a DataGrid object and writes that directly to the stream as HTML, setting the extension to .xls. Putting Raposo’s solution in the report’s DataSet query:
and removing it in the SSRS field’s expression:
is the only thing I’ve found that works. Thanks!
I have found five solutions for this issue:
Format the field as text as described by scunliffe. This has the problem of the green triangle as stated by Raposo.
Use as described by Raposo. This has the problem that the value is not really a number. This could be an issue if the data is pulled into some system for processing.
Add the TD as
=»067″
. This has the same problem as #2.
Add the TD as
=text(067,»000″)
. This also has the same problem as #2.
Use a CSS class of .text3 and add this class to the TD. This is my preferred solution but it has the problem of requiring multiple classes if you have numbers of different lengths. If you write your header and then iterate through your data, you have to add all possible classes before knowing which of them you will need. But this has the advantages that the text is really a number and there is no green triangle.
Источник
Html formatted text in excel
This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.
Answered by:
Question
My name is Raghav Sharma . I am working in the software company . From past day i am working on the generating excel pro grammatically using interop classes. Everything working for me but when i am coming to finish the excel work . I found a problem .The problem is this when we add the html code progrmmatically in the excel column it show the same as it. For example
If i add this html text (sample) then it show as it but i need to show the «sample» text bold. So my question is can we show the html code in the excel cell if yes can any one show my how this can be done?
one more thing when i copy the html code from some site or some html formatted content which is already html formatted then copy that text in the excel its working fine
I am using asp.net 3.5 framework c# and developing website which generates the report and show in the excel.
If any one familiar about this please reply me as soon as possible
Источник
Convert a Formatted Cell in Excel into HTML Tag using VBA
Convert Formatted Cell in Excel to HTML
Please enable JavaScript
To get the font and formatting style of a text in worksheet follow these methods.
- Excel Worksheet Cell format – Single Format
- Text with Different formats in same cell – Multiple Format
When we type any text in a Worksheet, we change the default format to give importance to some area of text. We can change the format of a text color, background color, font size, font family, underline, bold, make the text italics etc.,
Let’s see how to read these formatting styles of a Excel sheet cell and convert it into its equivalent HTML code.
Excel Worksheet Cell with Single format to HTML
To convert Excel to HTML, when a cell has only single format, then the following VBA code can be used. This VBA code reads the font format property of cell. Then converts each property into its equivalent HTML tag.
To use this code, first insert a new module in VB editor. Then copy paste this code. (Press alt F11 -> Menu -> Insert -> Module).
The function takes any cell as its parameter. To test this code, enter any text in cell A1 with bold,underlined and color it, then in cell Be enter this formula.
Note: Function ‘ColorIdx_to_RGB‘ from above code get the Color code and converts into RGB code in VBA.
Convert Cell in Excel to HTML – Multiple Format Text
Consider you have text with multiple formats in a cell like the one below.
“This Text has multiple formats and needs to converted to HTML“.
To convert this text with so many font formats, the previous methods wont work. To make it work, we have to read format of each character in the cell, get its format (whether bold, italics, underlined, superscript, subscript or strikethrough). Then convert those formats into HTML equivalent.
Download the below Excel app that can convert such complex text formats into HTML tag.
If you are satisfied with the functionality and working of this VBA code, then you can the Excel Macro VBA code from this link at a cheap and affordable price.
Источник
- Remove From My Forums
-
Question
-
Hello All,
My name is Raghav Sharma . I am working in the software company . From past day i am working on the generating excel pro grammatically using interop classes. Everything working for me but when i am coming
to finish the excel work . I found a problem .The problem is this when we add the html code progrmmatically in the excel column it show the same as it. For exampleIf i add this html text (<b>sample</b>) then it show as it but i need to show the «sample» text bold.
So my question is can we show the html code in the excel cell if yes can any one show my how this can be done?one more thing when i copy the html code from some site or some html formatted content which is already html formatted then copy that text in the excel its working fine
I am using asp.net 3.5 framework c# and developing website which generates the report and show in the excel.
If any one familiar about this please reply me as soon as possible
Thanks in Advance
Raghav Sharma
Answers
-
It may keep the HTML tag, but it probably is not actual HTML, in which case the ‘tag’ is simply text.
You could use code to find tags and apply the expected formatting to the string between the tags. For example, this macro will add a string with HTML tags to a cell, and the other macro will convert bolding tags to bold. You would need to run the macro
once for each bolding pair, and similar macros for other codes.HTH,
Bernie
Sub TestMacro()
ActiveCell.Value = «If i add this pseudo-html text (<b>sample</b>) then it will be converted to bold»
ConvertBold ActiveCell
End SubSub ConvertBold(myAB As Range)
Dim myS As String
Dim myF As Integer
Dim myEF As IntegermyS = myAB.Value
myF = InStr(1, myS, «<b>»)
If myF > 0 Then
myEF = InStr(1, myS, «</b>»)
myAB.Value = Replace(myAB.Value, «<b>», «», 1, 1)
myAB.Value = Replace(myAB.Value, «</b>», «», 1, 1)
myAB.Characters(Start:=myF, Length:=myEF — myF — 3).Font.FontStyle = «Bold»
End If
End Sub
HTH, Bernie
-
Marked as answer by
Friday, October 15, 2010 4:46 AM
-
Marked as answer by
This guide will discuss how to convert HTML to Text in Cells in Excel using two methods.
In other words, there are two ways we can use to convert html to text in cells in Excel, which are using the Find and Replace feature and VBA.
Table of Contents
- A Real Example of Converting HTML to Text in Cells in Excel
- How to Convert HTML to Text in Cells in Excel Using Find and Replace
- How to Convert HTML to Text in Excel Using VBA
Hypertext markup language, or html, is a standardized formatting system that is used to create web pages. So it is a computer language that is often used for web development, internet navigation, and web documentation.
An html code always contains tags, < and >, which can make it difficult to read, especially when placed in a cell in Excel. So there are two ways to convert html to text in Excel.
One is using the Find and Replace feature. The Find and Replace feature in Excel finds the character you specify and replaces it with any text, character, or number you input.
Another way is using VBA in Excel. VBA stands for Visual Basic for Applications. It is a programming language in Excel and other Office apps. And VBA automates repetitive tasks, data processing, and generating graphs and reports.
VBA is useful for converting html to text in the entire worksheet, all while using a VBA code. If we have certain repetitive tasks in Excel, we can utilize VBA and record a macro to automate those repetitive tasks.
But VBA is not available for the web-based version of Excel. We recommend using the VBA method when working with the Microsoft Excel application or the Office 360 version. So only the free version of Excel does not support working with VBA.
Let’s take an example.
Suppose you are a web designer and you want to share how you created a certain web page with your colleagues. But some of them find it difficult to read html. So you need to convert the html to text in the spreadsheet to make it easier to read and share with others.
Awesome! Let’s move on and check out how to convert html to text in Excel using the two methods.
A Real Example of Converting HTML to Text in Cells in Excel
First, let’s focus on an example of how to convert html to text in cells using the Find and Replace feature. For instance, you have an html code in a cell.
Since html codes always have tags, < and >, we simply need to find those tags in the cells. Then, we can replace it with nothing or an empty string. So this method will simply remove the tags in the html, and we will be left with just the text.
And this is what it will look like after removing the tags. So we have converted html to text.
Then, let’s see an example of converting html to text using VBA. Essentially, it does the same thing as the Find and Replace feature. But it utilizes a programming code instead.
VBA allows us to convert html to text in the entire worksheet. This is what the VBA window in Excel looks like.
We will convert html to text by copying a macro code in the module window. After all the tags, < and >, are removed from the entire worksheet, we will only be left with the text.
You can make your own copy of the spreadsheet above using the link attached below.
How to Convert HTML to Text in Cells in Excel Using Find and Replace
This section will explain the step-by-step process of how to convert HTML to Text in cells in Excel using the Find and Replace feature.
1. First, select the cell containing the html you want to convert to text. In this case, we will select A2. Then, press Ctrl + H to open the Find and Replace window.
2. Next, input ‘<*>’ in the Find what. Also, the * is a wildcard character that basically tells Excel to look for anything that starts and ends with tags.
3. Lastly, we will leave the Replace with empty or blank.
4. Since we only selected one cell, simply click Replace. Otherwise, it will apply the function to the whole worksheet.
Furthermore, you can click the Replace All option if you have more than one cell selected.
5. And that’s it! You have successfully converted html to text using Find and Replace.
6. Additionally, we may end up with a weird format after doing this. For example, the cell may go all the way down in the worksheet. To fix it, simply select the cell. Then, go to Home and select Wrap Text.
This section will focus on the steps in converting html to text in Excel using VBA.
1. First, we need to open the VBA window in Excel. To do this, press Alt + F11.
2. In the VBA window, select Insert.
3. Third, click Module. Then, input this code:
Sub HTML_Removal()
Dim Cell As Range
With CreateObject("vbscript.regexp")
.Pattern = "<.*?>"
.Global = True
For Each Cell In Selection
Cell.Value = .Replace(Cell.Value, "")
Next
End With
End Sub
4. Next, select the cells containing the html code you want to convert to text.
5. Finally, click Run or press the F5 key to run the macro code.
6. And tada! You have converted the html to text in Cells in Excel using a macro code in VBA.
That’s it! You have successfully learned how to convert html to text in cells in Excel using two methods: the Find and Replace feature and VBA. Now you can convert html to text whether you are using a free version of Excel or the application.
Are you interested in learning more about what Excel can do? You can now use the various other Microsoft Excel formulas available to create great worksheets that work for you. Make sure to subscribe to our newsletter to be the first to know about the latest guides and tutorials from us.
Get emails from us about Google Sheets.
Our goal this year is to create lots of rich, bite-sized tutorials for Google Sheets users like you. If you liked this one, you’ll love what we are working on! Readers receive ✨ early access ✨ to new content.
With GemBox.Spreadsheet you can apply font formatting for the whole cell’s text (see Styles and Formatting) or for the individual characters and words.
You can specify the size, color, type, underline, strikeout and more. For a full list check the ExcelFont
properties.
The following example shows how you can format individual characters and words within an Excel cell.
using GemBox.Spreadsheet;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("InlineTextFormatting");
worksheet.Columns[0].Width = 50 * 256;
var cell = worksheet.Cells["A1"];
cell.Value = "This is big and red text!";
// Apply the size to "big and red" part of the text.
cell.GetCharacters(8, 11).Font.Size = 400;
// Apply the color to "red" part of the text.
cell.GetCharacters(16, 3).Font.Color = SpreadsheetColor.FromName(ColorName.Red);
cell = worksheet.Cells["A3"];
cell.Value = "Formatting selected characters with GemBox.Spreadsheet component.";
// Apply formatting on the whole cell content.
cell.Style.Font.Color = SpreadsheetColor.FromName(ColorName.Blue);
cell.Style.Font.Italic = true;
cell.Style.WrapText = true;
// Get characters from index 36 to the end of string,
// e.g. the "GemBox.Spreadsheet component." part of the text.
var characters = cell.GetCharacters(36);
// Apply the color and underline to selected characters.
characters.Font.Color = SpreadsheetColor.FromName(ColorName.Orange);
characters.Font.UnderlineStyle = UnderlineStyle.Single;
// Write selected characters.
worksheet.Cells["A5"].Value = "Selected characters: " + characters.Text;
workbook.Save("Inline Text Formatting.%OutputFileType%");
}
}
Imports GemBox.Spreadsheet
Module Program
Sub Main()
' If using the Professional version, put your serial key below.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
Dim workbook As New ExcelFile()
Dim worksheet = workbook.Worksheets.Add("InlineTextFormatting")
worksheet.Columns(0).Width = 50 * 256
Dim cell = worksheet.Cells("A1")
cell.Value = "This is big and red text!"
' Apply the size to "big and red" part of the text.
cell.GetCharacters(8, 11).Font.Size = 400
' Apply the color to "red" part of the text.
cell.GetCharacters(16, 3).Font.Color = SpreadsheetColor.FromName(ColorName.Red)
cell = worksheet.Cells("A3")
cell.Value = "Formatting selected characters with GemBox.Spreadsheet component."
' Apply formatting on the whole cell content.
cell.Style.Font.Color = SpreadsheetColor.FromName(ColorName.Blue)
cell.Style.Font.Italic = True
cell.Style.WrapText = True
' Get characters from index 36 to the end of string,
' e.g. the "GemBox.Spreadsheet component." part of the text.
Dim characters = cell.GetCharacters(36)
' Apply the color and underline to selected characters.
characters.Font.Color = SpreadsheetColor.FromName(ColorName.Orange)
characters.Font.UnderlineStyle = UnderlineStyle.Single
' Write selected characters.
worksheet.Cells("A5").Value = "Selected characters: " & characters.Text
workbook.Save("Inline Text Formatting.%OutputFileType%")
End Sub
End Module
HTML text formatting
Instead of using GetCharacters
methods, another way to create a rich formatted text is to use an HTML text.
The following example shows how you can create an inline text formatting by setting the cell value with the ExcelCell.SetValue(String, HtmlLoadOptions)
method.
using GemBox.Spreadsheet;
class Program
{
static void Main()
{
// If using the Professional version, put your serial key below.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("HtmlTextFormatting");
worksheet.Columns[0].Width = 50 * 256;
var htmlOptions = new HtmlLoadOptions();
var html = "<h1 style='background:#DDEBF7'>HTML formatted text!</h1>";
worksheet.Cells["A1"].SetValue(html, htmlOptions);
html = @"<div style='font:11pt Calibri'>
<p>This is <span style='font-size:20pt'>big and <span style='color:red'>red</span></span> text!</p>
<p>This is <sub>subscript</sub>, <sup>superscript</sup>, <strike>strike</strike>, and <u>underline</u> text.</p>
</div>";
worksheet.Cells["A3"].SetValue(html, htmlOptions);
workbook.Save("Html Text Formatting.%OutputFileType%");
}
}
Imports GemBox.Spreadsheet
Module Program
Sub Main()
' If using the Professional version, put your serial key below.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")
Dim workbook As New ExcelFile()
Dim worksheet = workbook.Worksheets.Add("HtmlTextFormatting")
worksheet.Columns(0).Width = 50 * 256
Dim htmlOptions As New HtmlLoadOptions()
Dim html = "<h1 style='background:#DDEBF7'>HTML formatted text!</h1>"
worksheet.Cells("A1").SetValue(html, htmlOptions)
html = "<div style='font:11pt Calibri'>
<p>This is <span style='font-size:20pt'>big and <span style='color:red'>red</span></span> text!</p>
<p>This is <sub>subscript</sub>, <sup>superscript</sup>, <strike>strike</strike>, and <u>underline</u> text.</p>
</div>"
worksheet.Cells("A3").SetValue(html, htmlOptions)
workbook.Save("Html Text Formatting.%OutputFileType%")
End Sub
End Module
GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.
Download Buy
Published: December 13, 2018 | Modified: December 19, 2022 | Author: Mario Zorica