binning at zero-filling data
-
Hello, I would like to bin and zero-fill excel or text data using C#. For example; Raw data: 1 34 2 3 43 4 3 5 converted to: 1 34 2 0 3 45 4 3 5 0 Does anyone have code for this? Thanks
-
Hello, I would like to bin and zero-fill excel or text data using C#. For example; Raw data: 1 34 2 3 43 4 3 5 converted to: 1 34 2 0 3 45 4 3 5 0 Does anyone have code for this? Thanks
Couldn't you just format the column to be a number that was an empty cell would display 0?
Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]
-
Hello, I would like to bin and zero-fill excel or text data using C#. For example; Raw data: 1 34 2 3 43 4 3 5 converted to: 1 34 2 0 3 45 4 3 5 0 Does anyone have code for this? Thanks
For text data, look for lines that don't contain an embedded space, then append " 0":
public List<string> CleanupText(List<string> inputLines) { List<string> outputLines = new List<string>(); foreach (string s in inputLines) { // Note: the Trim() and Replace() ensure that (a) there are no false hits and (b) no false misses, respectively if (s.Trim().Replace('\\t', ' ').Contains(" ")) outputLines.Add(s); else outputLines.Add(string.Concat(s, " 0")); } return outputLines; }
If you want to modify the collection in place, you'll have to replace the foreach() with a for() and an index into the list - since foreach() loops disallow modifying the collection they are iterating. To do this with Excel you'll have to iterate rows (the end condition being a cell in the first column that is empty), looking for cells in the 2nd column that are blank and inserting a '0' into those blank cells. I've not done enough MSOffice interop to be able to give you a code example.