I am creating .csv using file helper library (https://www.filehelpers.net/docs/html/R_Project_FileHelpers.htm) I am facing two issues as follows:
1) I am passing string to export which contain special characters like "advantage's, -" these character get replace with some garbage value like —. I dont know why this is happening? and how I can deal with it?
For Above issue I tried with http://www.becsv.com/csv-viewer.php site when I am using this to see my data it showing correct data. even I open my CSV file in notpad it also contain correct data only problem with excel.
2) I am passing filename which is of 50 Characters long and same name is appearing as sheet name, but sheetname can accept only 13 characters. So is there any way to pass filename and sheet name different?
If any one knows about these issues please help me.
Related
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
I have an issue where I am unable to upload a CSV file using C# sqlbulkcopy. C# code is correct, files are getting uploaded when in correct format. Tested it.
Reason: These csv files, may be, are not in valid format. Please see pic below, here you can see that the highlighted part is my column row and where the highlighting ends there starts a new data row which is not in new line. So, it is a comma separated value sheet but values are in a single line, which throws me error "The given ColumnName does not match up with any column in data source" while uploading .
Please click on image for larger view.
If I open it in excel and save it by pressing Ctrl+S, a popup opens and says that it is not valid CSV and when I click yes and close the sheet it asks to save and replace the file in destination. After saving it and then opening in notepad there I could see that the content is in now correct format which uploads successfully using C#. Like below image:
When I put cursor between Over40hrs and 200 (image 1) and press backspace once nothing happens, seems like there is some character. This is happening for all rows. Can we play with that character and save it in valid format ?
Is there any way by which I can save the contents in valid format when uploading in C# asp.net ? I couldn't find such question on internet. Please help.
Looks that notepad is not able display that character, you can replace it with String.Replace Read all file into variable, replace those characters with Environment.NewLine save with new name this file and upload a new file.
I received a requirement to save data in CSV file and send it to customers.
Customers use both Excel and Notepad to view this file.
Data look like:
975567EB, 973456CE, 971343C8
And my data have some number end by "E3" like:
98765E3
so when open in Excel, it will change to:
9.8765E+7
I write a program to change this format to text by adding ="98765E3" to this in C#
while(!sr.EndOfStream) {
var line = sr.ReadLine();
var values = line.Split(',');
values[0] = "=" + "\"" + values[0] + "\""; //Change number format to string
listA.Add(new string[] {values[0], values[1], values[2], values[3]});
}
But with customer, who use Notepad to open CSV file, it will show like:
="98765E3"
How could I save number as text in CSV to open in both Excel and Notepad with the same result? Greatly appreciate any suggestion!
Don't Shoot the messenger.
Your problem is not the way you are exporting (creating...?) data in C#. It is with the way that you are opening the CSV files in Excel.
Excel has numerous options for importing text files that allow for the use of a FieldInfo parameter that specifies the TextFileColumnDataTypes property for each field (aka column) of data being brought in.
If you chose to double-click a CSV file from an Explorer folder window then you will have to put up with what Excel 'best-guesses' are your intended field types for each column. It's not going to stop halfway through an import process to ask your opinion. Some common errors include:
An alphanumeric value with an E will often be interpreted as scientific notation.
Half of the DMY dates will be misinterpreted as the wrong MDY dates (or vise-versa). The other half will become text since Excel cannot process something like 14/08/2015 as MDY.
Any value that starts with a + will produce a #NAME! error because Excel thinks you are attempting to bring in a formula with a named quality.
That's a short list of common errors. There are others. Here are some common solutions.
Use Data ► Get External Data ► From Text. Explicitly specify any ambiguous column data type; e.g. 98765E3 as Text, dates as either DMY, MDY, YMD, etc as the case may be. There is even the option to discard a column of useless data.
Use File ► Open ► Text Files which brings you through the same import wizard as the option above. These actions can be recorded for repeated use using either command.
Use VBA's Workbooks.OpenText method and specify each column's FieldInfo position and data type (the latter with a XlColumnDataType constant).
Read the import file into memory and process it in a memory array before dumping it into the target worksheet.
There are less precise solutions that are still subject to some interpretation from Excel.
Use a Range.PrefixCharacter to force numbers with leading zeroes or alphnumeric values that could conceivably be misinterpreted as scientific notation into the worksheet as text.
Use a text qualifier character; typically ASCII character 034 (e.g. ") to wrap values you want to be interpreted as text.
Copy and paste the entire text file into the target worksheet's column A then use the Range.TextToColumns method (again with FieldInfo options available for each column).
These latter two methods are going to cause some odd values in Notepad but Notepad isn't Excel and cannot process a half-million calculations and other operations in several seconds. If you must mash-up the two programs there will be some compromises.
My suggestion is to leave the values as best as they can be in Notepad and use the facilities and processes readily available in Excel to import the data properly.
I am writing a utility that edits .docx files. I've made it so that when the user right clicks on the correct type of file, it automatically makes the changes and saves the document with a bit of text appended to the file name. All of this works great, except for the fact that I am receiving heavily truncated file names. If the file name contains more than one word, the string passed to the program is has most of its characters replaced by a single ~. Is there any way to either read the original file name, or have the parameter be the full string?
I found the solution to what I was trying to do. I ended up using the C# method Path.GetFullPath.
string path = Path.GetFullPath(originalpath);
This outputs the full file name as opposed to the truncated one.
http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
File.getCanonicalPath will give you what you want
http://msdn.microsoft.com/en-us/library/aa988183(v=vs.80).aspx
Put the whole path between two double quot; like:
var fn = "\"C:\\Path With Spaces And Special Characters\\#\\to\\My File.docx\"";
// send fn as an argument to the other process
I have an excel spreadsheet which contains addresses. I'm reading the data from the spreadsheet using OLEDB and storing it into a DataTable in C#.
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + #";Extended Properties=""Excel 8.0;HDR=1;IMEX=1""";
Here's the problem: When I use the DataSet Visualizer, I have an empty string in the zip code field.
12345-1234 --> ""
So I want to correct this behavior so that the zip code appears as it should. If i have to chop off the digits after the hyphen, that would be fine. How can I ensure the zip code gets read?
Excel often has its own ideas about how a column should be formatted. For example, if you have a column containing zip codes, some with the Plus 4, others without, it is pretty much a crap shoot as to how that column will be formatted. Maybe Excel will assume that its filled with Zip+4's, maybe it will assume 5-digit Zips, or maybe just numbers. I've worked with these files for years, and I'm convinced Microsoft uses a random number generator in making this decision.
As for your original question, according to this site, CONVERT is a valid SQL scalar function, so maybe something like
SELECT CONVERT(BadField, SQL_CHAR) AS FixedField FROM [Table$]
might work?
My first inclination was to suggest using COM (instead of OleDb) to read the data from the spreadsheet. I'm pretty sure you would be able to read each cell's format and deal with it accordingly, but I always found Excel via COM to be difficult and not terribly fast.