How to Set column Formats In Excel File from C# Code - c#

I have a protected standard template excel sheet with formulas that I am loading SQL data to Excel through SSIS. While loading all int columns are convert to text format in an excel. Because of that formulas is not working until I double click each cell.
Or if I change that entire column format, right-click, select Format Cells and set the alignment or number format for the entire column, then it is calculating. I was wondering is there any way that I can change column format using C# code? or is there any better way to do this.

Right-click on the Excel Destination, and select "Show Advanced Editor". Change to the "Input and Output Properties" tab. Expand the "Excel Destination Input" and "External Columns". Click on the "Premium" column. The Data Type is probably "Unicode string [DT_WSTR]". Change it to an appropriate numeric type.

Related

Open XML from C# to Excel, cell datatype as date

I'm creating a workbook in Excel using Open XML, which works fine - except dates. Working in Excel you can change a cell's datatype like this:
Right click the cell, format cell, in the number tab choose "date" (from the category section). I'd like to accomplish the same thing using C# and Open XML to create the workbook, however my date cells are shown as "general" (text) in Excel.
The date format is OK, it's presented like this: 01.01.2015. What I need is the cell type to be set to Date, so that I can filter by "January", not the string "01.01.2015".
When saving my Excel sheet as an .xml, I find that the cell is stored like this:
<Cell><Data ss:Type="String">04.09.2015 22:00:41</Data></Cell>
What I need is this:
<Cell ss:StyleID="s62"><Data ss:Type="String">04.09.2015 22:00:41</Data></Cell>
The bottom row is from a cell where I manually manipulated the data type to a Date.
I've used the examples where you create your own styles, the cell type is still a string when opened in Excel.

Number stored as Text

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.

How to format numbers as string data-types while exporting to Excel in ASP.NET?

In my ASP.NET Web-form Project I have an Event which Export Data (List<Profit>) to Excel . I'm using the old fashion way , writing to HTML file with .XLS extenstionmode
Profit Entity has some string properties which has numeric data like "100000561234"
When I export to Excel , Those Columns appear like 1.00001E+11 in Excel columns, So the user has to Right-Click on Excel column and change format-cell.
Any help appreciated
Simply Add the following line before your HTML code, it will resolve the issue. This style sheet will be applied on every cell and no need to format the cell as string manually.
Response.Write("<style> TD { mso-number-format:\#; } </style>");

Assign RTF/HTML code to Excel cell

I am Working on Microsoft C#.net(windows)
I have a requirement ,
I have an excel file containing formatted text in a cell (ex: text will be bold,italic,color) . I need to save the cell content in to the sql database.
Then i need to show the saved formatted text from database to a new excel file.
How to assign RTF code and HTML code to Excel cell.
Is there any option available. Please help
Thanks
Sandeep
i think you can use openxml to split the data along with cell formart and save in different tables in your db by referencing some id.
Lets say sheet 1 has sheetid as 1 and against this sheet id save data in your table and against same sheetid save cell number and style associated like font border fill.
Below Link can be helpful.
http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.cellformat.aspx
I am writing code for the same will share soon

How can i change excel cell value

i m exporting data from gridview to excel.
This is my exported column:
POSTING KEY
1
50
50
but the column seems in the gridview like that:
POSTING KEY
01
50
50
Excel convert my string values to numeric when after export. But i must see "01". How can i change excel cell type or cell value in code when before save file.?
From Excel, highlight the column or the cell, right click and select "Format Cells", under the "Number" tab, click "Custom" under the Category:. In the Type: textbox, key in "00".
You can export text with a apostrophe suffix. for e.g. to display 01 instead of 1, you need to set cell's value as '01 and not just 01
Excel will take this value as text instead of number. But in this case you will see a Error SamrtTag in Excel saying "Number stored as Text" which can be turned off from preferences.
When you read or write a cell there are two values you can look at. The first one is value (e.g. cell.Value) which is the one you are probably setting. The second is NumberFormat (e.g. cell.NumberFormat). This is a string a will represent the format of the value when displayed in Excel, set this to an appropriate value (use the string representations in Excel if you're not sure which one you want).

Categories

Resources