Best way to handle long integers in a CSV file - c#

I have a csv file with a column where the integers are over 16 characters long. The issue with this is that it changes the integer to a short abbreviation example: 8.71688E+17, is there a way in c# you can convert it back to a long integer without having to modify the csv file itself?
I currently convert the CSV file to a datatable, could there be a way at the conversion stage?

You can use BitInteger class. Here's more info: http://www.codeproject.com/Articles/2728/C-BigInteger-Class

prefix value with '=' and enclose large-value inside double quote so final string to put into excel cell is ="<large-value>"

Related

Converting .csv file to xlsx adds <> to fields

I opened a csv file in Excel and then converted it to an xlsx file so that we can import it into our system via the OLEDB interface via C#.
It is a simple query we do select * from [anyworksheet] to populate a datatable object. However, after the conversion we receive an error. This happens for different column types:
DateTime: Cannot parse <15-01-2015> as a DateTime
Int (null entry): Cannot parse < > as an Int
I checked the csv file and it doesn't contain <> in the fields, and have tried to force datetime formatting on the cells in the excel file and it didn't work.
Can you at least tell me what is happening here, even if there isn't a solution to this?
Thanks
I'm fairly sure the < and > only appear as part of the error message to make it clear what is the actual value. Think of them as quotes.
In your examples neither is a space an integer and can be parsed as such, nor is your date format anything sensible. So it doesn't surprise me that you get errors. For dates try using ISO-8601, i.e. YYYY-MM-DD, which should work much better in an interchange format.
You can also try to use ClosedXML library.
It is simpler than OLEdb and doesn't limit string length to 255 characters.

When I attach details to Excel It is truncating 0 infront of the Zip Code

I am sending a email to prgrom manager and Attaching all the details filled by the applicant in a csv. when they receive the email they are missing a 0 in the Zipcode.
I am using C# and asp.net I placed a break point just before I write data into CSV and It is looking good with the 0. But when I receive the email and open the Excel When I look at Zipcode it is missing the 0.
Can anyone suggest me how to correct this issue?
Thanks
Append a ' symbol in front of your numbers (a # in the excel code itself):
C# : string zip = #"0066222";
Excel: `0066222
Excel will read it as text and preserve the format rather than a number (where it trims the leading 0's).
The data will need formatted like this:
="001",="002",="003"
Append a single quote character (') to the beginning of the zip code value before you write it to the Excel file. This will tell Excel to show the exact value in that field. Otherwise, Excel will interpret that field as being numeric, and trim leading zeroes.

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.

How to show long numbers in Excel?

I have to build a C# program that makes CSV files and puts long numbers (as string in my program). The problem is, when I open this CSV file in Excel the numbers appear like this:
1234E+ or 1234560000000 (the end of the number is 0)
How I retain the formatting of the numbers? If I open the file as a text file, the numbers are formatted correctly.
Thanks in advance.
As others have mentioned, you can force the data to be a string. The best way for that was ="1234567890123". The = makes the cell a formula, and the quotation marks make the enclosed value an Excel string literal. This will display all the digits, even beyond Excel's numeric precision limit, but the cell (generally) won't be able to be used directly in numeric calculations.
If you need the data to remain numeric, the best way is probably to create a native Excel file (.xls or .xlsx). Various approaches for that can be found in the solutions to this related Stack Overflow question.
If you don't mind having thousands separators, there is one other trick you can use, which is to make your C# program insert the thousands separators and surround the value in quotes: "1,234,567,890,123". Do not include a leading = (as that will force it to be a string). Note that in this case, the quotation marks are for protecting the commas in the CSV, not for specifying an Excel string literal.
Format those long numbers as strings by putting a ' (apostrophe) in front or making a formula out of it: ="1234567890123"
You can't. Excel stores numbers with fifteen digits of precision. If you don't mind not having the ability to perform calculations on the numbers from within Excel, you can store them as Text, and all of the digits will display.
When I generate data to imported into Excel, I do not generate a CSV file if I want control over how the data are displayed. Instead, I write out an Excel file where the properties of the cells are set appropriately. I do not know if there is a library out there that would do that for you in C# without requiring Excel to be installed on the machine generating the files, but it is something to look into.
My two cents:
I think it's important to realize there is a difference between "Data" and "Formatting". In this example you are kind of trying to store both in a data-only file. This will, as you can tell from other answers, change the nature of the data. (In other words cause it to be converted to a string. A CSV file is a data only file. You can do some tricks here and there to merge formatting in with data, but to my way of thinking this essentially corrupts the data by merging it with non-data values: ie: "Formatting".
If you really need to be able to store formatting information I suggest that, if you have time to develop it out, you switch to a file type capable of storing formatting info separately from the data. It sounds like this problem would be a good candidate for a XML Spreadsheet solution. In this way you can not only specify your data, but also it's type and any formatting you choose to use.

How to convert between Progress Database Character[] datatype and C# strings?

Progress databases allow for a Character[x] datatype. How can I write to a particular x using C# and ODBC?
Please do not answer unless you understand the what Character[x] means... it is not a string (array of chars), it is an array of strings (which are arrays of chars).
I figured it out. The documentation I have refers to a datatype of character[20], format x(24). character[x] (where x is a number), is like an array of strings. Format x(24) means each string in the array can be 24 characters long.
Essentially characters[20], format x(24) is a string that is 20 * 24 characters long with each "array element" separated with a semi-colon (;).
If column "options" is defined as character[20], x(24) then to populate it with strings from 1 to 20, one would merely write:
row.options = "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20";
To populate it with all empty strings write:
row.options = ";;;;;;;;;;;;;;;;;;;";
Format x(24) means each string in the array can be 24 characters long.
Not quite accurate, the format is a DISPLAY format, which is used by a lot of the Progress routines when displaying / printing / exporting this field. All character fields, whether they have an extent or not, are stored on the DB as variable length string. So you could easily have up to about 32K worth of data in each of your 20 extents.
The Progress ODBC Driver Guide doesn't seem to mention that type at all?

Categories