Numeric fields lose leading zero while writing CSV in c# - c#
I'm using an ASP.NET application which exports my clients data to CSV, I need my clients Phone number to be with the leading Zero.
I need the phone numbers to be without "-" and without quotations, and due to the nature of my application I cannot use 3rd party products such as EPPLUS.
I've tried to put a space and let the CSV "understand" that I need the phone number as text , but that doesn't seem right.
I would like to know how to make the excel include the leading zero , without using 3rd party products.
Thanks
Change the data that is saved in the csv with the following format:
="00023423"
CSV example:
David,Sooo,="00023423",World
This will show 00023423 in excel and not 23423.
public void CreatingCsvFiles(Client client)
{
string filePath = "Your path of the location" + "filename.csv";
if (!File.Exists(filePath))
{
File.Create(filePath).Close();
}
string delimiter = ",";
string[][] output = new string[][]{
new string[]{ "=\"" + client.phone + "\"", client.name }
};
int length = output.GetLength(0);
StringBuilder sb = new StringBuilder();
for (int index = 0; index < length; index++)
sb.AppendLine(string.Join(delimiter, output[index]));
File.AppendAllText(filePath, sb.ToString());
}
Inspired from http://softwaretipz.com/c-sharp-code-to-create-a-csv-file-and-write-data-into-it/
The important part :
"=\"" + client.phone + "\"", client.name
If the phone number is an int, of course you add .toString().
Print phone number to CSV with prepended ' (single quote), so it looks like:
"Some Name","'0000121212"
Excel should treat this 0000121212 as string then.
I believe converting the number into a formula like the accepted answer might not be a helpful solution for all.
The alternate solution I went with is to just add a tab space before the integer value.
Example:
Taking phoneNumber as a string variable which contains our int value
Solution:
"\t" + phoneNumber
If you know already how much numbers has to be inside phone you can do like this
phoneNumber.ToString("000000000000")
In this example I consider that phoneNumber is an int and required length of numbers is 12.
Related
StreamReader from .csv - "foreign" chars and blank values showing up as '?'
I'm having two problems with reading my .csv file with streamreader. What I'm trying to do is get the values, put them into variables which I'll be using later on, inputting the values into a browser via Selenium. Here's my code (the Console.Writeline at the end is just for debugging): string[] read; char[] seperators = { ';' }; StreamReader sr = new StreamReader(#"C:\filename.csv", Encoding.Default, true); string data = sr.ReadLine(); while((data = sr.ReadLine()) != null) { read = data.Split(seperators); string cpr = read[0]; string ydelsesKode = read[1]; string startDato = read[3]; string stopDato = read[4]; string leverandoer = read[5]; string leverandoerAdd = read[6]; Console.WriteLine(cpr + " " + ydelsesKode + " " + startDato + " " + stopDato + " " + leverandoer + " " + leverandoerAdd); } The code in and of itself works just fine - but I have two problems: The file has values in Danish, which means I get åøæ, but they're showing up as '?' in console. In notepad those characters look fine. Blank values also show up as '?'. Is there any way I can turn them into a blank space so Selenium won't get "confused"? Sample output: 1372 1.1 01-10-2013 01-10-2013 Bakkev?nget - dagcenter ? Bakkev?nget should be Bakkevænget and the final '?' should be blank (or rather, a bank space).
"Fixed" it by going with tab delimited unicode .txt file instead of .csv. For some reason my version of excel doesn't have the option to save in unicode .csv... Don't quite understand the problem of "rolling my own" parser, but maybe someday someone will take the time to explain it to me better. Still new-ish at this c# stuff...
How can i analise millions of strings that merge into each other?
I have millions of strings, around 8GB worth of HEX; each string is 3.2kb in length. Each of these strings contains multiple parts of data I need to extract. This is an example of one such string: GPGGA,104644.091,,,,,0,0,,,M,,M,,*43$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32Header Test.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ$GPGGA,104645.091,,,,,0,0,,,M,,M,,*42$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32Header Test.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ ÿÿ!ÿÿ"ÿÿ#ÿÿ$ÿÿ%ÿÿ&ÿÿ'ÿÿ(ÿÿ)ÿÿ*ÿÿ+ÿÿ,ÿÿ-ÿÿ.ÿÿ/ÿÿ0ÿÿ1ÿÿ$GPGGA,104646.091,,,,,0,0,,,M,,M,,*41$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32Header Test2ÿÿ3ÿÿ4ÿÿ5ÿÿ6ÿÿ7ÿÿ8ÿÿ9ÿÿ:ÿÿ;ÿÿ<ÿÿ=ÿÿ>ÿÿ?ÿÿ#ÿÿAÿÿBÿÿCÿÿDÿÿEÿÿFÿÿGÿÿHÿÿIÿÿJÿÿ$GPGGA,104647.091,,,,,0,0,,,M,,M,,*40$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32Header TestKÿÿLÿÿMÿÿNÿÿOÿÿPÿÿQÿÿRÿÿSÿÿTÿÿUÿÿVÿÿWÿÿXÿÿYÿÿZÿÿ[ÿÿ\ÿÿ]ÿÿ^ÿÿ_ÿÿ`ÿÿaÿÿbÿÿcÿÿ$GPGGA,104648.091,,,,,0,0,,,M,,M,,*4F$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32Header Testdÿÿeÿÿfÿÿgÿÿhÿÿiÿÿjÿÿkÿÿlÿÿmÿÿnÿÿoÿÿpÿÿqÿÿrÿÿsÿÿtÿÿuÿÿvÿÿwÿÿxÿÿyÿÿzÿÿ{ÿÿ|ÿÿ$GPGGA,104649.091,,,,,0,0,,,M,,M,,*4E$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32Header Test}ÿÿ~ÿÿ.ÿÿ€ÿÿ.ÿÿ‚ÿÿƒÿÿ„ÿÿ…ÿÿ†ÿÿ‡ÿÿˆÿÿ‰ÿÿŠÿÿ‹ÿÿŒÿÿ.ÿÿŽÿÿ.ÿÿ.ÿÿ‘ÿÿ’ÿÿ“ÿÿ”ÿÿ•ÿÿ$GPGGA,104650.091,,,,,0,0,,,M,,M,,*46$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32Head as you can see it is pretty much this repeated: GPGGA,104644.091,,,,,0,0,,,M,,M,,*43$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32Header Test.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ$GPGGA,104645.091,,,,,0,0,,,M,,M,,*42$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32Header Test.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ ÿÿ!ÿÿ"ÿÿ#ÿÿ$ÿÿ%ÿÿ&ÿÿ'ÿÿ(ÿÿ)ÿÿ*ÿÿ+ÿÿ,ÿÿ-ÿÿ.ÿÿ/ÿÿ0ÿÿ1ÿÿ I want to separate this string into two lists like this: _GPSList $GPGGA,104644.091,,,,,0,0,,,M,,M,,*43 $GPVTG,0.00,T,,M,0.00,N,0.00,K,N* $GPVTG,0.00,T,,M,0.00,N,0.00,K,N _WavList 32HeaderTest.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ 32HeaderTest.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ.ÿÿ ÿÿ!ÿÿ"ÿÿ#ÿÿ$ÿÿ%ÿÿ&ÿÿ'ÿÿ(ÿÿ)ÿÿ*ÿÿ+ÿÿ,ÿÿ-ÿÿ.ÿÿ/ÿÿ0ÿÿ1ÿÿ Issue 1: This repetition isn't containing within a single string, it overflows into the next string. so if some data crosses the end and start of two strings how to I deal with that? Issue 2: How do I analyse the string and extract only the parts I need?
The solution I'm providing is not a complete answer but more like an idea which might help you get what you want. Everything else which I present is an assumption on my behalf. //Assuming your data is stored in a file "yourdatafile" //Splitting all the text on "$" assuming this will separate GPSData string[] splittedstring = File.ReadAllText("yourdatafile").Split('$'); //I found an extra string lingering in the sample you provided //because I splitted on "$", so you gotta take that into account var GPSList = new List<string>(); var WAVList = new List<string>(); foreach (var str in splittedstring) { //So if the string contains "Header" we would want to separate it from GPS data if (str.Contains("Header")) { string temp = str.Remove(str.IndexOf("Header")); int indexOfAsterisk = temp.LastIndexOf("*"); string stringBeforeAsterisk = str.Substring(0, indexOfAsterisk + 1); string stringAfterAsterisk = str.Replace(stringBeforeAsterisk, ""); WAVList.Add(stringAfterAsterisk); GPSList.Add("$" + stringBeforeAsterisk); } else GPSList.Add("$" + str); } This provides the exact output as you need, only exception is with that extra string. Also some non-standard characters might look like black blocks.
How to add strings and show as a full sentence
I've assigned some string values from a data set to the string b. for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { string b = ds.Tables[0].Rows[i].ItemArray[0] + " " + ds.Tables[0].Rows[i].ItemArray[1]; } What I want to do is add those values, and finally show as a sentence. Example: if "dog", "cat", and "cow" are the values read by the for loop, I want to display "dog cat cow" in a message box. How to do that?
Edit: Since it appears you are interested in the String.Join() method this could work perfectly for you. You have plenty of options here but if you want to go that route here's how. First create an array of the items you are returning then you can simple use the String.Join() method to concatenate the items in the array like so: string separator = whatever seperator you want "," or "|" string d = String.Join(separator, animalArray); MessageBox.Show(d); The first thing you'll need to do is make sure you have imported the System.Windows.Forms namespace to enable your ability to call the MessageBox function. Essentially you are already there with the concatenation of the strings. If you are looking for a cleaner option I would recommend using String.Format() or using the newer method of concatenation by applying a '$' character in front of a string which allows you to simple add your variables between curly braces. For example: $"Hello my name is {name}."
What's wrong with the above? You already have them in your string variable b. Enclose it in MessageBox.Show(); instead of adding the variable. string b = ""; for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { b += ds.Tables[0].Rows[i].ItemArray[0] + " " + ds.Tables[0].Rows[i].ItemArray[1] + "\n"; } MessageBox.Show(b);
How can I add a SPACE halfway through a string value?
I'm trying to create a STRING in JSON format. However, one of the fields (from my editing/removing ALL spaces) now leaves a line like "START":"13/08/1410:30:00". However, I want to add a space between the date and time? I have tried using the ToCharArray() method to split the string, but I am at a loss as to how to add a space between the DATE and TIME part of the string? For Example, i am trying to get: "START":"13/08/14 10:30:00" but instead am getting "START":"13/08/1410:30:00" Please note. The length of the string before the space requirement will always be 17 characters long. I am using VS 2010 for NETMF (Fez Panda II)
If the split position is always 17, then simply: string t = s.Substring(0, 17) + " " + s.Substring(17);
Obviously you will have to sort the numbers out, but thats the general idea. String.Format("{0} {1}", dateString.Substring(0, 17), dateString.Substring(17, dateString.Length - 17); Or you can use the StringBuilder class: var finalString = new StringBuilder(); for (var i = 0; i < dateString.Length; i++){ if (i == 17) finalString.Add(" "); else finalString.Add(dateString.ToCharArray()[i]); } return finalString.ToString();
If the date time format always the same you can use string.Insert method var output = #"""START"":""13/08/1410:30:00""".Insert(17, " ");
Strings in .Net are immutable: you can never change them. However, you can easily create a new string. var date_time = dateString + " " + timeString;
String Format with undefined number of characters c#
So I'm working on formatting a string and I need to line it up in a table, but this string has an undetermined number of characters. Is there anyway to have the string be in the same spot for each column? so far I have: ostring += "Notes\t\t" + " : " + employees[number].Notes + "\t\t" + employees[number].FirstNotes + "\t\t" + employees[number].SecondNotes; I use a similar fashion on the other rows, but they have a pre-determined number of digits, this however doesn't so I can't use the string modifiers like I would like. Any ideas on what I need to do?
You can use String.PadRight() to force the string to a specific size, rather than using tabs.
When you are using String.Format item format has following syntax: { index[,alignment][ :formatString] } Thus you can specify alignment which indicates the total length of the field into which the argument is inserted and whether it is right-aligned (a positive integer) or left-aligned (a negative integer). Also it's better to use StringBuilder to build strings: var builder = new StringBuilder(); var employee = employees[number]; builder.AppendFormat("Notes {0,20} {1,10} {2,15}", employee.Notes, employee.FirstNotes, employee.SecondNotes);
You would first have to loop over every entry to find the largest one so you know hoe wide to make the columns, something like: var notesWidth = employees.Max(Notes.Length); var firstNotesWidth = employees.Max(FirstNotes.Length); // etc... Then you can pad the columns to the correct width: var output = new StringBuilder(); foreach(var employee in employees) { output.Append(employee.Notes.PadRight(notesWidth+1)); output.Append(employee.FirstNotes.PadRight(firstNotesWidth+1)); // etc... } And please don't do a lot of string "adding" ("1" + "2" + "3" + ...) in a loop. Use a StringBuilder instead. It is much more efficient.