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
Related
I am having an issue with importing a CSV file. The problem arises when an address field has multiple comma seperated values e.g. home no, street no, town etc.
I tried to use http://forums.asp.net/t/1705264.aspx/1 this article but, the problem did not solved because of a single field containing multiple comma separated values.
Any idea or solution? because I didnt found any help
Thanks
Don't split the string yourself. Parsing CSV files is not trivial, and using str.Split(',') will give you a lot of headaches. Try using a more robust library like CsvHelper
- https://github.com/JoshClose/CsvHelper
If that doesn't work then the format of the file is probably incorrect. Check to make sure the text fields are properly quoted.
Do you control the format of the CSV file? You could see about changing it to qualify the values by surrounding them with double quotes (""). Another option is to switch to a different delimiter like tabs.
Barring that, if the address is always in the same format, you could read in the file, and then inspect each record and manually concatenate columns 2, 3, and 4.
Are the fields surrounded by quotation marks? If so, split on "," rather than just ,.
Is the address field at the beginning or end of a record? If so, you can ignore the first x commas (if at the beginning) or split only the correct number of fields (if at the end).
Ideally, if you have control of the source file's creation, you would change the delimiter of either the address sub-fields, or the record fields.
I have a .NET Windows Forms applications and I need to copy a list of 8-digit numeric codes into the clipboard to be pasted to Excel sheet.
string tabbedText = string.Join("\n", codesArray);
Clipboard.SetText(tabbedText);
The problem is that when a code begins with one or more zeros (ex. "00001234") it's pasted as number with the zeros trimmed.
Is there a way how to set clipboard text so that Excel accepts it as text?
I would treat this problem inside of Excel (and not in your application programaticaly). Format your cells to be treated as text, and then paste from clipboard. This way leading zeros are always pasted.
EDIT: This doesn't work in Excel, in that the apostrophe gets pasted in and shows up too. I'm leaving the answer here as an explicit statement that this approach won't help for Excel.
It does work for OpenOffice Calc though.
The standard way to 'tell' Excel to treat a string as a string is to prefix it with an apostrophe. Have you tried something like:
string tabbedText = "'" + string.Join("\n'", codesArray);
(note the extra apostrophe in there... it's a bit hard to see).
Of course, this may cause you issues if you're planning to use this value thereafter in Excel calculations but there are ways to handle that too.
I use a OleDb data reader to read a number of records, and then write them to a CSV. I then read from this CSV using File.ReadAllLines, then split on commas to get my data. The problem is some parts of the CSV include a character I can't display (shows up as a square), which appears to act as a line break - this line break corrupts the CSV, so I need to get rid of it.
I've tried replacing Environment.NewLine with something else (a blank space) when writing the CSV, and ditto with /r and /n but to no avail - the character isn't replaced. What other ways are there to remove these?
I then read from this CSV using File.ReadAllLines, then split on commas to get my data.
Stop rolling your own CSV parser.
Dont write CSV file's this way... as it won't work in every scenario.
Use OLD DB to do it for you.
http://devlicio.us/blogs/sergio_pereira/archive/2008/09/17/tip-export-to-csv-using-ado-net.aspx
Hope that helps.
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.
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.