My application is extracting data from a CSV excel file and distribute each lines in an array of string, and then distributes the array of string to another array of string to form a cell, right? here's my problem, some of the lines that has been extracted and distribute to an array of string from the csv file is cut short, and does not collect the array of strings completely. and we noticed that that array of string when we looked it inside the excel has a cell that has a line break:
here is the C# code when extracting the csv file's lines to an array:
var lines = File.ReadAllLines(fileName);
Related
I am working on a password generator, that uses elements of an array to generate a word-based password.
I currently am working with four arrays, with a lot of elements, and I have to hardcode them individually. I want to automate that process, because writing in a .txt file is both easier and cleaner than writing it on the code itself, and as I plan on distributing this program to my friends, I want to be able to make libraries for the arrays.
Simply put, the .txt file will have four lines, each for one of the arrays.
All I need to know currently is how to import each the lines of the text as a single string, which will be individually formatted into the arrays.
So, for example, the .txt file would have this:
a,b,c,d,e,f,g
d,e,f,g,h,i,j
g,h,i,j,k,l,m
j,k,l,m,n,o,p
And after the "fetching", four different strings would contain each of the lines:
string a = "a,b,c,d,e,f,g"
string b = "d,e,f,g,h,i,j"
string c = "g,h,i,j,k,l,m"
string d = "j,k,l,m,n,o,p"
I will then process it by this, for each string, to break them down into elements.
String pattern = #"\-";
String[] elements = System.Text.RegularExpressions.Regex.Split(passKey, pattern);
You can use this:
System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("c:\\file.txt");
To put them in an array specifically, use:
string[] lines = File.ReadLines("c:\\file.txt").ToArray();
The following code line allows me to append the variable w1 in excel file. but appending process happens in the first column. how to make it for multi-variables {w1, w2, w3} in multi-columns?
File.AppendAllText(Path.Combine(docPath, "Groupbox1.CSV"), w1);
First, you're actually working with .CSV files, not Excel. The CSV stands for Comma Separated Values, so each value in a line of text on these files are typically separated by a comma (or other delimiters such as semicolon). The Excel application lets you open .CSV files, but they are different from Excel files which have either .xls or .xslx extensions. Excel will display values separated by commas in their own columns.
Then, say you have multiple variables. Then you can simply create one string with commas in between values, and write it to the file.
var w1 = "Data1";
var w2 = "Data2";
var w3 = "Data3";
// This creates a string, according to above data, that looks like
// "Data1,Data2,Data3"
var lineToWrite = string.Format("{0},{1},{2}", w1, w2, w3);
File.AppendAllText(Path.Combine(docPath, "Groupbox1.CSV"), lineToWrite);
Now we write that line to the file, and each data item is separated by a comma. Now when you open this with Excel, each data item will be displayed in its own column.
I have string data containing *.csv file content (read from using File.ReadAllText(filePath) method and want create new Data Table object from the string data.
I don't have an option to use file path because the file is immediately deleted from the drive once it is been read.
You could consider replacing your File.ReadAllText method with File.ReadAllLines. Then, you could do something like the below steps:
Create a datatable, define its structure etc.
Read all lines from file as an array of strings
In a loop, split the string by your separator character, then parse each portion of string as its corresponding datatype e.g. int.TryParse() for numerical values etc.
Add new row to the datatable using DataTable.Rows.Add and supplying an object array with your parsed values.
I have a program that combines three text files and put them all into one and sorts them all out alphabetically. I was wondering how I could possibly put this onto an excel spreadsheet without downloading and using the excellibrary (if that's possible).
Heres my code that combines all three files if that helps.
private void button1_Click(object sender, EventArgs e) // merge files button
{
System.IO.StreamWriter output = new System.IO.StreamWriter("OUTPUT.txt");
String[] parts = new String[1000];
String[] parts2 = new String[1000];
parts = File.ReadAllLines(textBox1.Text); //gets filepath from top textbox
parts2 = File.ReadAllLines(textBox2.Text); //gets filepath from middle textbox
String[] head = File.ReadAllLines(headingFileBox.Text); //header file array
//merging the two files onto one list, there is no need to merge the header file because no math is being
//computed on it
var list = new List<String>();
list.AddRange(parts);
list.AddRange(parts2);
//foreach loop to write the header file into the output file
foreach (string h in head)
{
output.WriteLine(h);
}
//prints 3 blank lines for spaces
output.WriteLine();
output.WriteLine();
output.WriteLine();
String[] partsComb = list.ToArray(); // string array that takes in the list
Array.Sort(partsComb);
//foreach loop to combine files and sort them by 1st letter
foreach (string s in partsComb)
{
partsComb.Equals(s);
output.WriteLine(s);
}
output.Close();
}
Any help would be much appreciated.
You could look at creating it in a CSV format (Comma-separated values). Excel naturally opens it up and loads the data into the rows and cells.
Basic CSV looks like this:
"Bob","Smith","12/1/2012"
"Jane","Doe","5/10/2004"
Some things are optional like wrapping everything in quotes, but needed if your data may contain the delimiter.
If you're okay with a comma separated values (CSV) file, that's easy enough to generate with string manipulation and will load in Excel. If you need an excel specific format and are okay with XLSX, you can populate one with some XML manipulation and a ZIP library.
Fair warning, you will have to be careful about escaping commas and new lines if you choose a traditional CSV file. There are libraries that handle that as well.
You might want to try Excel package plus: http://EPPlus.codeplex.com
It's free, lightweight, and can create xlsx files.
I have a method that reads a file. This file has roughly 30000 lines. However when I read it into an array I get a random length for my array. I have seen it as low 6000.
I used both
string[] lines = System.IO.File.ReadAllLines(#"C:\out\qqqqq.txt");
and
System.IO.StreamReader file = new System.IO.StreamReader(#"C:\out\qqqqq.txt");
(and use a counter.)
But I get the same result. I can see in Excel these are too small.
If the line endings in the file are inconsistent (sometimes \n, sometimes \r\n and sometimes \r) then you could try reading the entire file as a string and splitting it yourself:
string file = System.IO.File.ReadAllText(#"C:\out\qqqqq.txt");
var lines = file.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
For large files, this is inefficient, because it needs to read the entire file - using StreamReader you would be able to read the file line-by-line as you're processing it. If performance is an issue, then you could write simple tool that first corrects the line endings.