change of format when transferring string from C# to Excel - c#

I have a problem when I want transfer data from dataGridView in C# to Excel.
My data was "84853435455002" and when I saved in Excel I see this format "8.675675E2".
I want to save my data completely and not have "E" in my data ..
what must I do to solve this .

You need to format your cells in Excel to Number and set any decimal places to 0 if you do not want decimal too. By default the Excel cell format is general which converts your input this way.

u have to tell excel to read the cell as text. Or add some special character to numbers, so excel will read it as text. eg. " '84853435455002 " (put single quote in front of number)

Related

copy data from Excel cells without formatting c#

I have winform project in c# that I want to copy data from Excel cells to my datagirdview. But the excell cells is formatted with 2 digits after the comma.I want to get all value.
What i mean :
The excell cell value = 884,79258
Formatted value = 884,79
I am getting the data with Clipboard.GetText() method. Bu it gives me formatted value(884,79).
But i want the get all value which is 884,79258.
These are my codes
String[] lines = Clipboard.GetText().Split('\n');
string result = lines[0];
Thanx.
Edit:
Thanx to dr.nulll. The link that he shared is worked.
I am sharing the link below for those who have similar request.
Copying decimal values from Excel to C# causes to copy only displayed values
Don’t go via the clipboard. Either use Excel via interop or a 3rd party library like NPOI. If you must use the clipboard then the value copied reflects the current formatting of the cell, so you need it set to show 4 digits after the decimal point.

How to convert date and number format to text in excel so that when we query it using ODBC it returns the datafield as string

Problem--> Recently I completed an import program for which the input is excel.
To make the matter worse or good I assumed that every column of excel sheet will be of type string.
In excel we have some field that has MPN and partnumber. Internally the partnumber is stored as xx/xx/xxxx, which looks same as UK Date format.
The excel has made these column as number and data, which I never asked it to do.
If I query this sheet using odbc then I get .0 appended with MPN and part number I get as datetime.
To solve this problem I tried to paste the sheet in another blank sheet, I also tried to paste special (Values), but I am not able to paste it in text format. I even tried to paste it in note pad, but doing so is converting numbers in scientific notation.
My question: How can I change the type to text, without converting it to values.
Select the column, right click format cells and choose Text. But there would be a caveat there, since the internal format of dates is a number then you would get the underlying number as text I guess.
Saving as CSV would be a simple workaround.
Change the import-program in such a way that it casts all imported values to string? In Excel VBA it would look something like this:
Function importDateAsString(rng As Range)
importDateAsString = CStr(rng.Value)
End Function

How to convert the column number format of a excel sheet in c# dynamically?

I am exporting some fields to an excel sheet in c#. All but one column are coming correctly, but in that column I need to export the value 0 to 0.00 format.
When my code behind code is running I am appending the value to a stringBuilder object as 0.00 format, but when I opened that exported document it shows the value as 0. Then I must change the column number format in the excel sheet and change the number format by selecting the type 12.3323.
I don't want to do it like this. I want to define the column number format dynamically in c# code behind file.
How can I do this? Please suggest me if you know.
I think C# does right what it does. The issue is that you need to specify the "column format" in excel in order for it to show that way. Just add the format mask.
You can use Format function to convert your value when writing into cell.
Like :
Format(Convert.ToDouble(data.ToString()), "0.00")
You can use Range.NumberFormat = "0.00" (English version) or "0,00" (for the German version of Office);
e.g. ws.get_Range("A1", "C2").NumberFormat = "0.00";

Issue in exporting data to CSV

I am doing export of data from database to Excel. After exporting when opening the csv file, the excel application is autoformating the values in excel.
One my cell contained the value -A1177, but it got converted to zero.
Can any one let me know is there any solution to avoid this or switch off this autoformat in excel.
thanks.
The easiest way to do it, is to simply wrap the value in quotes, e.g. asdasd,"-A1177",11/03/1984 in your CSV file. You can also change the format of the column to `Text'.
Otherwise, check out this excellent Stack Overflow Question and Answer
Change column Data Format when importing the text change it to Text
Insert a ' at the start of any formula in Excel and it will be considered a string.
So if possible, check when generating the CSV file if the first char in a given field can be understood as an Excel formula "opener" (off my mind {=, -, +}) and prepend a '.
You can do it for every field, but its only strictly necessary if the text can be misunderstood as a formula.

Query excel spreadsheet for cell format.... (percentage)

I am using the .Net 4.0 and excel 2003
How can i use an oledb connection to retrieve the cell format of an excel spreadsheet... I specifically want to find out if a cell column (or cell itself) is in a numeric percentage format.
I cannot seem to find this information in the GetOleDbSchemaTable method.
EX: My web app reads numbers from an excel spreadsheet. This works fine; However, if the numbers are in a percentage format, excel displays it as (fraction*100) but the actual value is a fractional decimal (1/3 = .3333..) - Excel displays as 33.33% - (Notice the decimal point).
Therefore, i need a way of distinguishing between what is a percentage & what is not to allow my webapp to work properly...
Any ideas?
Thanks in advance.....
You might be able to get out this information with OleDbConnection.GetSchema, but I'm not sure what information you'll get for an Excel sheet with that. Documentation here.
Search the NumberFormat property of the Cell (Range) in question for a % sign.
Also, if you can get the format type, then you're looking for a format that starts with 'P', like 'P1'.
EDIT: The only way I can see is either using XML or Automation. For automation you need to use the Interop Assembly. Namespace: Microsoft.Office.Interop.Excel, Interface: DisplayFormat, Property: NumberFormat.
Can you just read the first row of data as a string and parse it looking for '%' in the string.

Categories