SearchResultCollection to CSV - c#

Is there any easy way to export the contents of my results (SearchResultCollection) to a CSV file?
or do I have to Iterate each result and Append it to a text file?

Is there any easy way to export the contents of my results
(SearchResultCollection) to a CSV file or do I have to Iterate each
result and Append it to a text file?
No, there is no better option than iterating the SearchResultCollection and writing it to a text-file.
using (StreamWriter w = File.AppendText(path))
{
foreach (SearchResult result in allSearchResults)
{
w.WriteLine(string.Format("Path={0} Properties={1}"
, result.Path
, string.Join(",", result.Properties.PropertyNames));
}
}

Related

How do I write CSV file with header using FileHelpers?

I am using FileHelpers to write DataTable content into CSV file.
Because of huge number of records in DataTable I chose to dump the result set as it is in DataTable into CSV file like below
CommonEngine.DataTableToCSV(dt, filename)
And the CSV has sucessfully written with 2 million records having the size of 150MB.
But I wanted to add the filed header at first line of this CSV file.
Is there a way FileHelper will allow to write the header using CommonEngine.DataTableToCSV?
You must use engine.HeaderText = engine.GetFileHeader() before you call WriteFile
Looking at the source code, it looks like there is no way of setting the header line. However, it is easy to copy the code and write your own replacement. Something like this:
public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options, string headerLine)
{
using (StreamWriter writer = new StreamWriter(filename, false, options.Encoding, 102400))
{
// output header
writer.Write(headerLine);
writer.Write(StringHelper.NewLine);
foreach (DataRow row in dt.Rows)
{
object[] itemArray = row.ItemArray;
for (int i = 0; i < itemArray.Length; i++)
{
if (i > 0)
{
writer.Write(options.Delimiter);
}
writer.Write(options.ValueToString(itemArray[i]));
}
writer.Write(StringHelper.NewLine);
}
writer.Close();
}
}

Determine if input file is usable by program

I have a C# program that looks through directories for .txt files and loads each into a DataTable.
static IEnumerable<string> ReadAsLines(string fileName)
{
using (StreamReader reader = new StreamReader(fileName))
while (!reader.EndOfStream)
yield return reader.ReadLine();
}
public DataTable GetTxtData()
{
IEnumerable<string> reader = ReadAsLines(this.File);
DataTable txtData = new DataTable();
string[] headers = reader.First().Split('\t');
foreach (string columnName in headers)
txtData.Columns.Add(columnName);
IEnumerable<string> records = reader.Skip(1);
foreach (string rec in records)
txtData.Rows.Add(rec.Split('\t'));
return txtData;
}
This works great for regular tab-delimited files. However, the catch is that not every .txt file in the folders I need to use contains tab-delimited data. Some .txt files are actually SQL queries, notes, etc. that have been saved as plain text files, and I have no way of determining that beforehand. Trying to use the above code on such files clearly won't lead to the expected result.
So my question is this: How can I tell whether a .txt file actually contains tab-delimited data before I try to read it into a DataTable using the above code?
Just searching the file for any tab character won't work because, for example, a SQL query saved as plain text might have tabs for code formatting.
Any guidance here at all would be much appreciated!
If each line contains the same number of elements, then simply read each line, and verify that you get the correct number of fields in each record. If not error out.
if (headers.Count() != CORRECTNUMBER)
{
// ERROR
}
foreach (string rec in records)
{
string[] recordData = rec.Split('\t');
if (recordData.Count() != headers.Count())
{
// ERROR
}
txtData.Rows.Add(recordData);
}
To do this you need a set of "signature" logic providers which can check a given sample of the file for "signature" content. This is similar to how virus scanners work.
Consider you would create a set of classes where the ISignature was implemented by set of classes;
class TSVFile : ISignature
{
enumFileType ISignature.Evaluate(IEnumerable<byte> inputStream);
}
class SQLFile : ISignature
{
enumFileType ISignature.Evaluate(IEnumerable<byte> inputStream);
}
each one would read an appropriate number of bytes in and return the known file type, if it can be evaluated. Each file parser would need its own logic to determine how many bytes to read and on what basis to make its evaluation.

Print an array/list to excel in c#

I am able to save a single value into excel but I need help to save a full list/array into an excel sheet.
Code I have so far:
var MovieNames = session.Query<Movie>()
.ToArray();
List<string> MovieList = new List<string>();
foreach (var movie in MovieNames)
{
MovieList.Add(movie.MovieName);
}
//If I want to print a single value or a string,
//I can use the following to print/save to excel
// How can I do this if I want to print that entire
//list thats generated in "MovieList"
return File(new System.Text.UTF8Encoding().GetBytes(MovieList), "text/csv", "demo.csv");
You could use FileHelpers to serialize some strongly typed object into CSV. Just promise me to never roll your own CSV parser.
If you mean you want to create a .csv file with all movie names in one column so you can open it in Excel then simply loop over it:
byte[] content;
using (var ms = new MemoryStream())
{
using (var writer = new StreamWriter(ms))
{
foreach (var movieName in MovieList)
writer.WriteLine(movieName);
}
content = ms.ToArray();
}
return File(content, "text/csv", "demo.csv");
Edit
You can add more columns and get fancier with your output but then you run into the problem that you have check for special characters which need escaping (like , and "). If you want to do more than just a simple output then I suggest you follow #Darins suggestion and use the FileHelpers utilities. If you can't or don't want to use them then this article has an implementation of a csv writer.

How can I utilize StreamWriter to write to a csv file?

So here's what I'm working with. I'm trying to take an XML file, pull the info from the attributes, append them together, and write it to a CSV file. I'm still relatively new to programming, and the other programmer is out of the office today, so I could really use some assistance.
My first question, regards the StringBuilder. Do I need to have an AppendLine at the end of my StringBuilder, so that each string output from the foreach loop is on a new line? And would I need to do that inside the foreach loop?
My second question regards actually writing my string to the CSV file. Would it look something like?
swOutputFile.WriteLine(strAppendedJobData)
And I think this would also go inside the foreach loop, but I'm not too sure.
Thanks for the help, I hope I've worded my question in a somewhat easy to understand manner.
//Create a stream writer to write the data from returned XML job ticket to a new CSV
StreamWriter swOutputFile;
string strComma = ",";
swOutputFile = new StreamWriter(new FileStream("C:\\Dev\\AppendedJobData.csv", FileMode.Create, FileAccess.Write, FileShare.Read));
//Get nodes from returned XML ticket
XmlNodeList xmlJobs = xdResults.SelectNodes("/Updates/Jobs/Job");
//Pull out data from XML attributes
foreach (XmlElement xeJobUpdate in xmlJobs)
{
//Break down the job data
string strProjectID = xeJobUpdate.GetAttribute("SharpOwlProjectID");
string strJobNumber = xeJobUpdate.GetAttribute("JobNumber");
string strClientCode = xeJobUpdate.GetAttribute("SharpOwlClientCode");
string strClient = xeJobUpdate.GetAttribute("Client");
string strVCAOffice = xeJobUpdate.GetAttribute("VCAOffice");
string strLoadStatus = xeJobUpdate.GetAttribute("LoadStatus");
//Build the string to be added to the new CSV file
StringBuilder sbConcatJob = new StringBuilder();
sbConcatJob.Append(strProjectID).Append(strComma).Append(strJobNumber)
.Append(strComma).Append(strClientCode).Append(strComma).Append(strClient).Append(strComma)
.Append(strVCAOffice).Append(strComma).Append(strLoadStatus).Append(strComma);
string strAppendedJobData = sbConcatJob.ToString();
if you want to do it a bit more elegant you could do something like that:
using(StreamWriter swOutputFile = new StreamWriter(new FileStream("C:\\Dev\\AppendedJobData.csv", FileMode.Create, FileAccess.Write, FileShare.Read)))
{
//Get nodes from returned XML ticket
XmlNodeList xmlJobs = xdResults.SelectNodes("/Updates/Jobs/Job");
//Pull out data from XML attributes
foreach (XmlElement xeJobUpdate in xmlJobs)
{
List<String> lineItems = new List<String>();
lineItems.add(xeJobUpdate.GetAttribute("SharpOwlProjectID"));
//add all the other items
swOutputFile.WriteLine(String.Join(',', myLine.ToArray()));
}
//after the loop you close the writer
}
//all the work is done much easier
My first question, regards the
StringBuilder. Do I need to have an
AppendLine at the end of my
StringBuilder, so that each string
output from the foreach loop is on a
new line? And would I need to do that
inside the foreach loop?
My only advice since it appears you have not attempted this would be to try it. It is the only way you will learn.
swOutputFile.WriteLine(strAppendedJobData)
This would write an entire line of text to a file.
You really have two options here:
If you call sbConcatJob.AppendLine() inside the foreach loop you can build the contents of the file in one string builder then call swOutputFile.Write(sbConcatJob.ToString()) outside of the foreach loop to write the file.
If you keep your code as it is now you can add sw.OutputFile.WriteLine(sbConcatJob.ToString()) inside the foreach loop and write the file one line at a time.

How can i find out the Last row in a datagridview and write it to text file

I would like to find the last row of the datagridview and i would like to write that particular row data to a text file can any one help me
I will give you a sample code, it may help.
List<string> lstContents = new List<string>();
foreach (DataGridViewCell cell in mydataGrid.Rows[mydataGrid.RowCount - 1].Cells)
{
lstContents .Add((string)cell.Value);
}
string myData= string.Join(",", lstContents.ToArray());
Now using streamwriter you can write this string to your file. Also you can use any separator. I have used "," comma here.
///This will append data to your text file...
using (StreamWriter sw = new StreamWriter(FilePath, true))
{
sw.WriteLine(myData);
}

Categories