Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm using the below code to export the data in queryResult:
C#
private void btnDownload_Click(object sender, EventArgs e)
{
    var queryResult = from s in context.Contacts
                        join c in context.Address on s.ContactID equals c.Contact.ContactID
                        select new
                        {
                            s.FirstName,
                            s.LastName,
                            s.ContactNumber,
                            //s.Addresses,
                            c.Address1,
                            c.Mortgage,
                            c.RemainingMortgage,
                            c.Rent,
                            c.Period,
                            c.RentArrears,
                            c.Rates,
                            c.RateArrears,
                            c.RatesUpToDate,
                            c.Repairs
                        };

    CsvFileDescription outputFileDescription = new CsvFileDescription
    {
        SeparatorChar = '\t', // tab delimited
        FirstLineHasColumnNames = true,
        FileCultureName = "en-GB" // use formats used in UK

    };
    CsvContext cc = new CsvContext();

    // Write contents of productsNetherlands to file
    cc.Write(
        queryResult,
        "Houses.csv",
        outputFileDescription);
}  


I've a couple of questions regarding the created CSV file:
1)my header col has the column names yet in the outputted CSV they are listed together in Cell A1 as 1 string

2)The address field on the form is a rich text box so when an address with return key presses is entered this splits the outputted string in the CSV file (one way to stop this is to have separate text boxes for each part of the address but is there a workaround for my question?)

3) Instead of writing the file to a file directory can I display it to the user once all data has been written successfully?
Posted
Updated 26-Mar-14 5:37am
v2
Comments
ZurdoDev 26-Mar-14 11:38am    
1. It must be missing the comma then.
2. I don't know of a way for CSV to support line breaks. I would suggest replacing them with a special code and when you load the CSV translate it back.
3. I don't follow. You are writing the code so you can do whatever you want. Not sure what the question really is.
pmcm 26-Mar-14 11:43am    
1. Missing a comma between each col header value?
3. So the app creates the file and then displays a message to the user asking do they want to open or save the file - similar to downloading a file online for example.
ZurdoDev 26-Mar-14 11:47am    
1. Yes, if they are all run together you just need to make sure there is a comma between them.
3. Again, that is your choice. You could have a text box show the results. You could save as a text file and allow them to open it. There are many options.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900