I'm currently exporting to Excel a RadGrid and I have some cells that have the text "1 / 10" meaning that they had 1 hour used for 10 units. When this gets to Excel, it thinks the cell is a date, so it changes the cell to be January-10 which is not what I'm wanting.
I've gone the other direction before (changing a string to a number/date) but I've never had the issue where I needed to make Excel keep the cell as a string and not try to change the format.
How do I do this? I've tried adding a apostrophe to the beginning of the cell text:
e.Cell.Text = String.Format("'{0}", e.Cell.Text);
but that seems to just make Excel display two apostrophe's at the start of the cell:
Update:
I'm using Telerik RadGrid (v2012.2.929.40) and all it does is generate some HTML for Excel to open (you always get that horrible prompt from Excel when trying to open it). So before, if I wanted something to be formatted as a number like I had specific in the grid, I could have the following event that would set the mso-number-format. But, this doesn't quite work when I want Excel to display the cell value as strictly text.
protected void RadGridQuote_ExcelExportCellFormatting(object sender, ExcelExportCellFormattingEventArgs e)
{
if (e.Cell.Style["mso-number-format"] != null)
{
e.Cell.Style["mso-number-format"] = "/#";
e.Cell.HorizontalAlign = HorizontalAlign.Left;
}
}
If you're using Excel Automation in C#, I've found the best results would be to set the NumberFormat first, which you should set to "#". Then set the value in your .Text property. (If you do it in reverse, it'll convert the date into a number, which you don't want.)
See also MSDN regarding NumberFormat.
Related
In this assignment I have to create a spreadsheet like excel without importing a library. Now So far I created a grid of 26x26 textboxes (USING CODE), created the indexed rows and alphabet indexed columns. (All of this using a class named Cellfields as a template)
Gave the names (A1,A2,A3 etc) to each cell. and created a formula bar up top.
then I created an if statement that if what the user writes.SubString(0) == to "=" (Meaning the start of a formula),
it will then go to a switch to read the other input the user entered, meaning if user enters =Sum(A1:A3) it will calculate the sum of cells A1,A2,A3.
Now since the calculated cells required are unspecified, I am unable to find a solution to how to calculate a range of values online, as all you can find online are how to import libraries.
I Did try a few things but were immediately removed. I am trying to create a method for it however, have been staring at laptop for the past 3 days.
So you already have a matrix of 26x26 Textboxes, I assume they would be arranged in a Grid, say the name of your Grid is "gridCells".
Now, Users will click on a Cell (actually textbox) and then go to the Formula Bar and type a formula. So On GotFocus of the textboxes, let the textbox remain "Selected", in the sense keep the reference at form level also may be Highlight the Border to show that this is the currently selected cell. This will help you know which Textbox to apply the formula.
Now, when someone types "=Sum(A1:A3)" in the Formula Bar, you would parse the formula, identify it is a "Sum" operation and find out the names of the Textboxes A1, A2 & A3.
From your post looks like you are done this far. Now write a function as below (Not bothering about performance etc. right now).
private String GetValueOfCell(String cellName)
{
foreach (var control in gridCells.Children.OfType<TextBox>())
if (control.Name.Equals(cellName))
return control.Text;
return "";
}
Call this function for all 3 Textboxes, like this:
String value = GetValueOfCell("A1");
And if the value is not empty and can be parsed as int/double, keep doing the Sum and finally store the value in the Text property of our highlighted Textbox.
Hint: You might also want to Save the Formula on the highlighted textbox, because this value goes into the Text property you can use the Tag property to hold the formula.
=CONCATENATE( "Level3!$B", MATCH($B6,Level31!$A:$A,0),":$B", COUNTIF(Level31!$A:$A,$B6) + MATCH($B6,Level31!$A:$A,0)-1)
I have this formula in excel cell. Which I am copy pasting in all the rows below through C# code. It is fine till here.
The problem is that in this formula Level3 was a sheet name which is renamed to Level31. This change is reflecting in formula automatically in three places and not on first place..Why
After manual formula update in cell it works fine.
Second problem is in next cell
where another formula is implemented. When I am opening sheet it shows #N/A
but when I click in the cell and hit enter key. It starts working..
Can anyone please guide me?
If you are referring to first place as =CONCATENATE( "Level3!$B", , that value is stored as string and will not automatically adjust from 3 to 31. Try this formula =CONCATENATE(MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255)&"!$B", MATCH($B6,Level31!$A:$A,0),":$B", COUNTIF(Level31!$A:$A,$B6) + MATCH($B6,Level31!$A:$A,0)-1)
formula MID(CELL("filename",A1),FIND("]",CELL("filename",A1))+1,255) makes your first place also dynamic.
I have an interesting issue that I can't seem to figure out how to solve.
I need to export a date into excel in one format, but display it in another.
The user should see: ddMMMyyyy, but they enter it in MM/dd/yyyy. The problem I am having is that if I set the cell value to a MM/dd/yyyy format, it is not auto formatting to the ddMMMyyyy unless I manually edit the field in excel.
Is there any way to force the formatting?
worksheet.Cell("I" + rowIndex).SetValue<String>(LastUpdated.ToString("MM/dd/yyyy"));
worksheet.Cell("I" + rowIndex).Style.DateFormat.Format = "[$-409]dd-mmm-yyyy;#";
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;
I'm trying to do an Excel Add-In and I would like that, in a worksheet that I'm creating, I could select a cell (or a range of cell) that, once you start typing on it (or them), a dropdown-like with possible matches for the typed characters.
Is this possible to do it programmatically?
The closest that I got to achieve this is through the Validation object of the Cell, but a dropdown-like list of options is not displayed just, as you could imagine, I get to display a message indicating that the text is not valid.
As always, thanks in advance!