How can I change the format of a cell in Excel using the Microsoft Excel 12.0 Library in C#? More specifically, I want to change a given cell to text format.
I have read .net c# changing excel cell format, but the cell still says general (although it does display as text).
It depends on the format you're actually trying to implement. There's not a silver bullet for changing to any format you want. If it's formatting numbers that you need, you could use something like this. I ran into a lot of suggestions to create a macro, you can do some pretty specific tasks that you define yourself.
No need to do that just change the NumberFormat.
// Set all cells from A1 to B2 to be text
ws.get_Range("A1", "B2")).NumberFormat = "#";
different formats can be looked up here
http://office.microsoft.com/en-us/excel-help/number-format-codes-HP005198679.aspx
Related
Taking a look at this answer Getting Cells with Formulas in Excel file, there is an elegant solution to get all cells in an Excel worksheet which contain formulas. But what if I want all the cells that do NOT contain formulas? (I do not want blank cells either - I just want the plain cell with a value and that is not a formula).
Is there an elegant solution which does not include checking each and every cell in C# VSTO environment?
If I understand your question, you want the constants, which is sort of the opposite of the formulas. There is a special cell type for this as well:
Range nonFormulas = ws.Cells.SpecialCells(XlCellType.xlCellTypeConstants);
foreach (Range r in nonFormulas)
{
// Do some stuff
}
I think you know this already, but the formulas are just:
ws.Cells.SpecialCells(XlCellType.xlCellTypeFormulas);
I am facing a peculiar problem with Excel with Data Refresh.
A solution that I am working on generates an Excel on the Server using OpenXml.
In this Excel, I also add a Connection Part, which basically is a link to a webpage of my website.
This particular problem is not with English user, but if the User has Danish as the language.
When the Excel is generated, I specify a cell type as Date type (FormatId = 14)
I set the text for the cell as an OADate.
The first rendering works fine and the date is displayed correctly.
But in case of Danish, the decimal value of the OADate is stripped off and a large date is rendered making Excel show ######## (date to large)
But during Data Refresh, when Excel tries to refresh based on the data my webpage sends as Html table, Excel strips the decimal and a long number is placed in the date time cell.
I tried setting the style of the cell based on advice available here: Format HTML table cell so that Excel formats as text?
But it seems there is a difference in the way OADate works. Does anyone know what is the best way to construct the Html for Excel Data Refresh in case of OADate.
By changing the Html to have a cell value as Text work, but I want my Html to pass on the OADate to Excel.
regards,
~Mayur
The delimiter "," or "." should be set as per user and then value should be set.
I'm currently updating values in an Excel template and I keep getting my currency fields to have the green triangle in the top corner and the "Number Stored as Text" message. How do I get Excel to recognize the cell is a number and I want it to treat it that way since I already have the cell formatted as currency? (just like what happens if I'm on the cell in Excel, hit F2, and then hit enter)
Here's a simplified version of what I'm currently doing:
UInt32Value moneyFormat = report.createCellFormat(report.Stylesheet, fontIndex, backgroundIndex, borderIndex, 168);
report.UpdateValue("Workbook", "I29", "1234.56", moneyFormat, true, String.Empty);
And here's an image of what my cells look like
You might find the answer in this post:
Open XML SDK 2.0 - how to update a cell in a spreadsheet?
Probably the DataType of the cell is not set correct.
I want to read excel display value, not value that excel internally saves. Problem is that when this value is wider that the cell it makes cell to display ####### which is OK in excel. But I want to read this value from excel API. When I access property range. Text I also get value ########. Is there a way to get value which will be displayed when cell is wide enough?
i.e some third property, not Value2 nor Text.
My colleague said that there are three values in excel, one internal value how excel represents cell value, one what is currently displayed and one what will be displayed when there is enough space, is he right?
If this is not possible, I have another solution which might work. I used AutoFit() method to make cell wide enough and that I read value. But the problem is that I don't want to change width of columns. Is there a way to remember width, then change width so I can read cell, and that return it again to previous width?
Or maybe I should somehow apply formatting option to value of cell to get what will be displayed?
How to solve this?
In VBA I would do this by getting the Value and the number Format, and then useing the VBA Format Function to format it, but I am not sure how to do this using c#.
As a matter of curiosity, why would you want to get the formatted value anyway?
m trying to break one large Excel spreadsheet into several. I've made good progress, but I'm running into some problems. Specifically, the values that get copied over don't retain their format (for instance, 40322 instead of 5/24/2010 and -101 instead of (101.00) ). I've tried using the style (see below) but that doesn't even get me the font, let alone the number format. Any help or a poke in the right direction would be appreciated.
There are 2 loops, one for row, one for column.
destinationSheet.Cells[i, j].Style = sourceSheet.Cells[i, j].Style;
Instead of looping for each cell, you can copy/paste the entire range of cells using the pastespecial method.
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.namedrange.pastespecial(VS.80).aspx