CSV file doesn't upload - C# , Sqlbulkcopy - c#

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.

Related

Facing issue while creating .CSV using File Helper library with c#

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.

Disallow Excel to auto format columns while opening a .csv file

Im exporting some data to a .csv file.
.csv file:
Hotel Name;Street;Postal Code;City;Latitude;Longitude
Hotel X;Street 1;00000;City X;15.000000;15.000000
But if i open it in Excel, Excel will Format the Latitude and Longitude automaticly so it cannot be used for copy & paste.
Ignore the 0 at postal Code, i must get rid of the 1.000 at Latitude and Longitude
How can i prevent Excel from doing this?
It should be done in the .csv file and not in Excel.
The Export Code:
foreach(...)
{
StringBuilder_Export.Append(DataRow_Temp[i] + ";");
}
StreamWriter StreamWriter_Export = new StreamWriter(
SaveFileDialog_Geo_Export.FileName,
true,
Encoding.Default
);
StreamWriter_Export.WriteLine(StringBuilder_Export.ToString());
EDIT: Im searching primarly for a solution of my Latitude and Longitude Problem.
Possibly a duplicate of Stop Excel from automatically converting certain text values to dates but if you want to generate an Excel file that looks exactly as you want when opened, do not use csv. Excel will always attempt to guess datatypes of csv columns by looking at the first (15 i think?) rows..
I use EPPlus to generate xlsx files from my apps, but there are many libraries you could use
Change its extension to txt and use the text import wizard. Then use that to tell excel how the columns should be treated (text, currency, date, etc). The text import wizard will start automatically when you open a .txt file
ensure the file is in UTF-8 format when you save it as a .csv
Then open excel, browse to select the file you want and it will automatically run the prompter.
Any other format than utf-8 and excel will try and auto convert.

Format a column to text format in CSV file using C#

I have an application, on which i export data from a datagrid to a csv file. I do this with the following steps:
Create a file:
var myFile = File.Create("test.csv");
myfile.Close();
write the data to a string builder(data)
write the data to the created file.
File.WriteAllText(filepath, data);
This works fine. The resulting csv file is opened in excel. I have a column of numbers which may have preceeding zeros, when those data is exported to csv file the preceeding 0's are lost. Is it possible to format the column as text column so the zeros are not lost.
View your file in notepad. The leading zeros are there, intact :-)
You need to tell Excel which format to use when you open the file. Change the file to .txt and use File -> Open in Excel and you should be presented with an import wizard. There you can explicitly tell Excel to treat your column as "text" which will prevent it from stripping leading zeroes.
More info here: http://www.howtogeek.com/howto/microsoft-office/how-to-import-a-csv-file-containing-a-column-with-a-leading-0-into-excel/

Stop Office Interop Copy/Paste from ignoring region settings

Am working with Office Interop (can't use OOXML) and want to copy a table from an Excel file into an RTF file.
So first i copy the table in Excel
excelSheet = excelBook.Worksheets[1];
excelBook.CheckCompatibility = false;
excelRange = excelSheet.Range["B12:F21"];
excelRange.Copy();
Then in Word (with the RTF open) i paste it
wordApplication.Selection.Find.Execute(placeholder);
WordRange range = wordApplication.Selection.Range;
if (range.Text.Contains(placeholder))
range.Paste();
Placeholder contains the text i use as code to know where to paste it in
Now in that excel table i have cells formated as currency, and so they contain data in the form 3,56 € but after the paste, what i have in Word (RTF file) is 3.56 $- notice the change from , to . and from € to $
However if i do all this manually (open the Excel file in Excel, select all cells from the table, press ctrl+C, open the RTF in Word, position the cursor and press ctrl+V - i get the correct value (euros).
Any ideas how i do this work programatacly as it does manually?
So i found a little workaround - thanks to #JensKloster for the idea
After pasting the table (as in the question) i load up the rtf as text into a variable and apply the following:
cnt = Regex.Replace(cnt, #"(?<main>\d+)\.(?<decimals>\d{2,3})", "${main},${decimals}").Replace("$", "€");
This seams to do the job (at least for now i found no issues with the current rtf templates in use - although issues could show up easilly in the future
Pending someone smarter giving me a better option, this will become the answer

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.

Categories