I'm working on a C# application (windows form) which has a DataGridView which is saved in a database (each cell considered as a string). What I want to do is to add a button that will color the text (or the cell BackColor) in such a way that when I print (on a paper) my database content, the color stays.
What I was thinking is to convert the cell string to RTF but I don't really know how to do this for the cells do not support RTF natively. I'd like to avoid as much as possible changing the DataGridView to a rich DataGridView so I was wondering if i can just do something like add this string when I save a colored cell:
"{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1036{\fonttbl{\f0\fswiss\fprq2\fcharset0 Microsoft Sans Serif;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}
{\colortbl ;\red255\green0\blue0;}
\viewkind4\uc1\pard\ltrpar\cf1\f0\fs17 " + cell.Value.ToString() + "\cf0\f1\par"
The tricky thing is that when I open my application, I want to read each cell's string, and apply the RTF style to the cell style. For exemple I save a red cell which contains "hello", when I open my application I want the cell to be red, but when I print (paper again) I want the text to be red (so parse the RTF on open to separate the text from the format).
Any ideas? Thanks in advance. :)
Forgot to say I found the correct RTF string, it is the following:
"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1036{\\fonttbl{\\f0\\fswiss\\fprq2\\fcharset0 Microsoft Sans Serif;}{\\f1\\fnil\\fcharset0 Microsoft Sans Serif;}}{\\colortbl ;\\red255\\green0\\blue0;}\\viewkind4\\uc1\\pard\\cf1\\f0\\fs17 " + value + "\\cf0\\f1}";
When starting the application I just check if the string cointains the RTF header, then I just split the text to keep the value and apply style to the cell.
Related
I am trying to inset formatted footnotes into an open word document using a WinForms application.
While I am able to use Interop.Word to set plain text footnotes and so long as I use plain text it works fine. However, I also want the user to be able to paste rich text formatted text from a rich text box into the footnote. This never works and always shows the rich text codes.
I know that footnotes can take formatting because if I put the rich text int a clipboard and paste it into a footnote the formatting is preserved.
I have even tried putting the rich text into the clipboard and then setting the string (s) to the clipboard contents using "s = Clipboard.GetText(TextDataFormat.Rtf);" It seems as if this should be exactly what I am pasting, but if I paste into the footnote it works. If the program sets it using the code below it does not work.
I appreciate any help.
application = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
application.ActiveDocument.Footnotes.Add(application.Selection.Range, "", s);
Cindy's answer was helpful. I fixed the problem by doing the following:
Saving the current selected position in the document and current clipboard contents.
Inserting a blank footnote.
Selecting the footnote.
Putting the string into the clipboard as rich text format.
Sending/pasting the information from the clipboard to Word.
Restoring the clipboard and original selected position in the document.
I have a requirment is to export data from DB table to Excel. I'm using MVC project where from View the user click a button to call a controller action to perform the export opertaion. The DB proc returns rows where couple of columns contains HTML data like
<ul><li>Name1</li><li>Name2</li></ul>
and this should be shown in Excel cell as below
Name1Name2
How can i do this ? any kind of help is deeply appreciated.
To recap the comments:
In general it's not possible to apply any styling via code (VBA or C#) that cannot be applied through the Excel interface.
Styling possibilities in Excel are quire limited, you can set font properties (size, weight, font, etc) by character, but 'paragraph' formatting can only be set per cell if at all.
Things that can be set per cell are alignment and indent (and text wrapping)
In order to simulate the bullet list, you could use VBA code like this:
Cells(1, 1).Value = Chr(149) & " foo" & vbCrLf & Chr(149) & " bar"
Cells(1, 1).IndentLevel = 2
C# code would be very similar.
But this is as good as it gets, doing anything more complicated in a single cell is unfortunately not possible. If you have to use Excel, I suggest you have just single paragraph per cell.
I follow the below pattern and I can easily copy text into clipboard in order to past it later on into excel, but now I like to have different background/font color for each cell.
When copying the data to clipboard, format it as Tab separated for columns, and Enter separated for rows. When pasting in Excel it will automatically put the values in rows and columns.
example of my code :
string clipboardText = "cell11" + "\t" + "cell12" + "\r\n" + "cell21" + "\t" + "cell22";
Clipboard.SetText(clipboardText);
any idea how to set a background/font color for each cell?
I'm afraid Clipboard is too simple tool for the job.
If I were you I would concider using Open XML library.
You can write your own copy function and use html to pass colour and font information. See this question.
I've a RichTextBox and I want color text in it. Is there any tag option? I'd like something like this [color:red]nick[/color] some message. Because I need to save it as text and I want on reload have also colored text.
Can I do something like this without writing own method?
You can set color for text in RichTextBox with SelectionColor
And if you want to save your rtf as plain text, then you will have to look at rtf format. Example:
{\rtf1\ansi\deff0 {\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
This line is the default color\line \cf2 This line is red\line \cf1
This line is the default color }
EDIT:
From this example - first of all you have to declare color table \colortbl in fololowing format:
{\colortbl; color1; color2; ... ; colorN;}
And then in the text you will have to enclose text with {\cfN YOUR_TEXT} where N is a number of color from table; you can not specify the boundaries of the block {}, then everything after \ cfN will be one color.
As the name says RichTextBox contains RichText
to change the Rtf text with 'rtf specific-tags' you can set/use the
RichTextBox.RtfProperty
also take a look at RichTextBox.SelectionColor to color text patterns in code
but when you don't want to use rtf, you said
need to save as text.
you could write your own 'markup' there is no built in expect rtf/html?
but rtf is text - at all
Example to use RichTextBix.SelectionColor to Color the text
richTextBox1.Text = "Hello";
richTextBox1.Select(0,2);
richTextBox1.SelectionColor = Color.Red;
colors the start of "Hello" red
and now you can access the 'taggeg' text in the RTFProperty of the RichTextBox
If you need examples of how things are encoded in RTF, you can create the document manually in Word or Wordpad, and save it as RTF. This will give you a hint about how to encode your formatting. Furthermore, if you're for instance creating help-documents, you can include them as an embedded resource and load them directly into the RichTextBox, with all the formatting included.
rtfMain.SaveFile(dlgSave.FileName);
From Reference Save text from rich text box with C#
Can you please help me to figure out how I can insert DataGridView values into a pre defined excel template (Into Specific Cells)?
I have a DataGridView on my windows Form which is getting the values from user input. Now I would like to enable users to export the DataGridView values into an excel file (A File like attached excel file).
As far as I know I have to create the headers and add them to the code programmatically but for the DataGridView part, honestly I have no idea how I can do that?
As you can see the Form (Box) is starting from B2 to K2 and end from B21 to K21 Now my question is how i can start importing values from B4 - k4 and so on?
Is there any way I can format the style of the cell (like Background color or font style and size) from C#? I mean generating a form like what is looking in attached Excel programmatically.
Thanks for your time in advance
Not sure if this is what you're looking for but ff you are using Office Interop, you can insert a 2-dimensional array into a range in Excel.
The following snippet might not be correct (no VS nearby and I haven't used excel automation for a long time) but you'll get the picture.
Excel.Range oRange = oSheet.Range("B2",Missing.Value);
oRange.Resize(myArray.GetLength(0),myArray.GetLength(1));
oRange.Value = myArray;