C# - Decimals not writing out to Excel - c#

I have an ETL that's saving data to an Excel file. The issue is that the decimals are not being written out for integers. Example:
14.00
is being written out as
14
My code for writing out that line is
loWorksheet.Cells[liRowNum, 5] = lcAmount.ToString("0.00");
When I step through the code, it shows as 14.00, but on the Excel file it is not retaining the decimal places. Is this something that can be fixed in my code or is this an Excel issue? Any suggestions?

I'm quite sure you have to set format for your cells. I can't check right now, but it will be something like
xlYourRange.NumberFormat = "0.00";
You can check this question Set data type like number, text and date in excel column using Microsoft.Office.Interop.Excel in c#

If you really want the data to be displayed literally the way it is in the source file, you have to deal with trade-offs. The simplest way is to format the data as text. You can do this a cell at a time or for entire columns:
loWorksheet.Columns["A:E"].NumberFormat = "#";
The trade-off is it's just text at this point. You can't add, sum, average, whatever.
On the other hands, if your data looks like this:
4.0
4.00
4.000
You can't really keep it as numbers and expect to retain the original format without doing some funny business.
If it's consistently two decimal places, and you know it's going to be, then I agree with #RenatZamaletdinov's solution.
And you might want to consider other strings and what Excel might to do them
0000123 becomes 123
10/23 will probably render as a date, depending on your localization
12345678901234567890 will render as scientific notation probably
These are all avoided if you make the numeric format text (#), but again without knowing what you plan to do with the data, it's hard to say if this is the correct approach.

Wrap lcAmount.ToString("0.00");in a pair of quotes and put an equal sign in front of it. This will prevent excel from overriding the format.
loWorksheet.Cells[liRowNum, 5] = "=" + '"' lcAmount.ToString("0.00") + '"';

Related

how to get correct format from xlsx - to interpret floating point number formated as text

i am aware of the floating point inaccurrancy, this is just how to get the correct text FORMAT!! by openxml from the xlsx and show the value like excel does. I am not able to edit the excel file and change the format or something like that, because of some reasons.
while working with numbers formated as numbers everything runs fine
formatted as text or general, the number 0.813 is saved as 0.812999.., excel shows it correct!, but via openxml i can't get the used format (0.000), anyone an idea to get the format?
maybe this is a standard format, not saved and choosen by other values?
finally I am using the ugly solution to get rid of this ..
if the cellFormat NumberFormatId is 2 I try to parse the value to double and then to string with two decimals
double.Parse(displayedValue).ToString("0.00");
not my desired solution, but for the moment this works .. of course also with all other unit tests

Excel Library possible integer overflow

I am using Excel Library - http://code.google.com/p/excellibrary/ - To generate an excel 2003 spreadsheet. Everything works fine except when some big values are used.
These are some reference numbers that are used by a client and I simply need to present them as integer values in the spreadsheet.
int val = 1420007117;
worksheet.Celss[row, col] = new Cell(val); // Displays - 352108063
This results in the value 352108063 being displayed in the spreadsheet. If the value is lower, then it displays fine.
Anyone know what the issue might be, or how to work around this problem. Outputting the value as string is not possible as it leaves a green Number stored as Text error.
I would say that excel doesn't support 64-bit integers and excellibrary doesn't care about it.
For such big numbers you better use floating point. This is how Excel handles big numbers.

Formatting cell values from OpenXML

I'm opening up xlsx files as a package and reading the contents of the xml files. I'm able to get the shared strings, borders, etc that I need and it's orders of magnitude faster than when I was using Interop. The only issue I have is when it comes to pulling out numbers and formatting them properly based on what the formatting is in the Excel file.
Is there a generic function somewhere that takes a value and a format and returns the formatted string? For example, if I have the value 31502008 and the custom format "$* #,##0_);$* (#,##0)" is there a simple way to get what Excel shows (which is $31,502,008). Obviously Excel knows how to handle it, but I have some sheets that have a crazy number of custom formats and I'm wondering how best to ensure that the string I get back in code matches what is seen in Excel.
Any ideas?
Thanks a lot for any help.

How to read a text file into a List in C#

I have a text file that has the following format:
1234
ABC123 1000 2000
The first integer value is a weight and the next line has three values, a product code, weight and cost, and this line can be repeated any number of times. There is a space in between each value.
I have been able to read in the text file, store the first value on the first line into a variable, and then the subsequent lines into an array and then into a list, using first readline.split('').
To me this seems an inefficient way of doing it, and I have been trying to find a way where I can read from the second line where the product codes, weights and costs are listed down into a list without the need of using an array. My list control contains an object where I am only storing the weight and cost, not the product code.
Does anyone know how to read in a text file, take in some values from the file straight into a list control?
Thanks
What you do is correct. There is no generalized way of doing it, since what you did is that you descirbed the algorithm for it, that has to be coded or parametrized somehow.
Since your text file isn't as structured as a CSV file, this kind of manual parsing is probably your best bet.
C# doesn't have a Scanner class like Java, so what you wan't doesn't exist in the BCL, though you could write your own.
The other answers are correct - there's no generalized solution for this.
If you've got a relatively small file, you can use File.ReadAllLines(), which will at least get rid of a lot cruft code, since it'll immediately convert it to a string array for you.
If you don't want to parse strings from the file and to reserve an additional memory for holding split strings you can use a binary format to store your information in the file. Then you can use the class BinaryReader with methods like ReadInt32(), ReadDouble() and others. It is more efficient than read by characters.
But one thing: binary format is bad readable by humans. It will be difficult to edit the file in the editor. But programmatically - without any problems.

storing large data in string

i am trying to store large data more than 255 characters in a string datatype but it truncates after 255. how can i achive this basically i need to pass this data to database
C# strings do not have any particular character limit. However the database column you are writing to may have a limit. If you are storing large amounts of data, you should use a BLOB column instead of an ordinary varchar type.
StringBuilder class
Like they said the string class is not limited, but you can do this for large strings. I feel it handles them better.
StringBuilder sb = new StringBuilder();
sb.append("Some text...");
sb.append("more text...");
sb.append("even more text!");
sb.toString();
Okay, it sounds like you have several different technologies involved - Excel, XML, databases etc. Try to tackle just one at a time. First read the data out of Excel, and make sure you can do that without any truncation.
Write a small console app which will read the value, then write it to the console - and its length. If that works, you know the problem isn't in Excel.
Next you can write a small console app with hardcoded input data (so you don't need to keep using interop with Excel) and write the XML from that, or whatever your next stage is.
Basically, take the one big problem ("when I read data from Excel and write it to the database it truncates long values") and split it into smaller and smaller ones until you've found what's wrong.
The string type does not limit strings to 255 characters. Your database column must be 255 characters.
I know that c# strings can hold much longer data than that. If the truncation occurs on commiting to DB, check the length constraint on ur Db field
The problem lies in the Excel part; .Character has a 255 characters limitation.
To read the complete text from a shape the following VBA syntax would do:
Worksheets("YourSheet").Shapes("Shape1").OLEFormat.Object.Text

Categories

Resources