locked
Generating Excel File Issues RRS feed

  • Question

  • User1960919895 posted

    Hi Friends,
                    I have a current application in which the administrator logs in and generates some Purchase codes.

                In the webpage to do this we have provided a textbox to enter the number of purchase codes he intend to create. For eg if administrator needs 40 Purchase codes he will enter 40 in the textbox provided and clicks the Generate Purchase Code Button.

              On clicking it I will create 40 random purchase codes .

    for (int i = 0; i < Convert.ToInt32(argStrQuantity); i++)

    {

    Session ["MailBody"] =GenerateRandomPurchaseCode//function to generate random Pur.code

    }

    These codes are sent to him as a mail through the code(In the mail body the newly generated Purchase codes were listed) . Here comes the requirement .....

     He wants the newly generated purchase codes sent to him in excel format.Currently I was generating the random purchase codes and storing them in a session. This session variable was later included in the mail body while sending the mail.

    So how can this requirement be met... instead of saving into the session object I should be saving it into a excel file ... and sent this excel file as an attachment to him isnt it ?

    I have doubts regarding how such an excel file can be created...Any idea? and where will this excel file be created???. I hope once i am able to write it into an excel file and that excel file is ready , I have the option of attaching it through Attachments.Add() while sending mail isnt it...

    So how can i construct that excel file into which I can enter the Purchase codes that I create.. Kindly suggest

    Thursday, January 7, 2010 6:43 AM

All replies

  • User1696475319 posted

    Excels can read CSVs (comma separated values).  So it should be a simple matter of generating a textfile, with the contents like so


    purchasecode1
    purchasecode2
    purchasecode3
    purchasecode4


    Save the file with an extension of XLS or CSV and then attach the file.  You already know how to attach the file, so you're left with the task of generating the file.



                TextWriter tw = new StreamWriter("purchasecodes.xls");
                // A foreach loop - go through all of the codes you've generated
                tw.WriteLine(purchasecode[i]);
                tw.Close();

    Thursday, January 7, 2010 8:00 AM