Issue in exporting data to CSV - c#

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.

Related

Change csv delimiter for Clipobard.GetData()

I try to save a datagrid as csv via copy datagrid to clipboard acording to this post:
Copy text from WPF DataGrid to Clipboard to Excel
The export works, but the column delimiter is "," but I use a german region setting with ";" delimiter character. The hole text is in one column:(
Can I change the DataFormats.CommaSeparatedValue delimiter?
Thank you and have a nice day
Chris
Not sure if you can change the delimiter. If you don't find a way, you can always do this:
string result1 = ((string)Clipboard.GetData(DataFormats.CommaSeparatedValue)).Replace(";",",");
From what I can tell, you can't change it.
I would agree with what someone already said in your link - you shouldn't be saving csv data to an xls file. When you open a .csv file with excel, it will ask you what delimiter you want to use before you import it.
If you want to change your delimiter before saving, take a look at this post.
Make sure to also take a look at the link in that answer - you'd need to use the Microsoft.VisualBasic.FileIO namespace.
However, if you still decide to save as .xls, be careful - you might change your delimiter to ';' before saving, but people whose excel expects ',' as a delimiter will have the same problem you have now.
i solved to problem by save the DataTable instead the DataGrid. The DataTableExtensions class in the link below works very well.
c# datatable to csv
Thanks

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

change of format when transferring string from C# to Excel

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)

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 do you prevent leading zeros from being stripped when importing an excel doc using c#

I'm able to connect to and read an excel file no problem. But when importing data such as zipcodes that have leading zeros, how do you prevent excel from guessing the datatype and in the process stripping out leading zeros?
I believe you have to set the option in your connect string to force textual import rather than auto-detecting it.
Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=c:\path\to\myfile.xlsx;
Extended Properties=\"Excel 12.0 Xml;IMEX=1\";
Your milage may vary depending on the version you have installed. The IMEX=1 extended property tells Excel to treat intermixed data as text.
Prefix with '
Prefixing the contents of the cell with ' forces Excel to see it as text instead of a number. The ' won't be displayed in Excel.
There is a registry hack that can force Excel to read more than the first 8 rows when reading a column to determine the type:
Change
HKLM\Software\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows
To be 0 to read all rows, or another number to set it to that number of rows.
Not that this will have a slighht performance hit.
I think the way to do this would be to format the source excel file such that the column is formatted as Text instead of General. Select the entire column and right click and select format cells, select text from the list of options.
I think that would explicitly define that the column content is text and should be treated as such.
Let me know if that works.
Saving the file as a tab delimited text file has also worked well.
---old
Unfortunately, we can't rely on the columns of the excel doc to stay in a particular format as the users will be pasting data into it regularly. I don't want the app to crash if we're relying on a certain datatype for a column.
prefixing with ' would work, is there a reasonable way to do that programatically once the data already exists in the excel doc?
Sending value 00022556 as '=" 00022556"' from Sql server is excellent way to handle leading zero problem
Add "\t" before your string. It'll make the string seem in a new tab.

Categories