0

I am working on an asp.net page with a gridview. I need to export the gridview to excel but need to modify the colmnns data before exporting. I am using below code to export:

 Response.Clear();

 Response.Buffer = true;         

   Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
   Response.ContentType = "application/vnd.xls";

   StringWriter sw = new StringWriter();
   HtmlTextWriter hw = new HtmlTextWriter(sw);
   gvLogs.RenderControl(hw);
   gvLogs.AllowPaging = false;
   gvLogs.DataBind(); // bind data 
   Response.Output.Write(sw.ToString());
   //need to call Flush and End methods 
   Response.Flush();
   Response.End();

It works but it exports the grid to excel with same columns data as grid like this:

[email protected] my company1aa viewed profile [email protected] my name  Company views profile   7/24/2013 11:18
[email protected] my company1aa viewed profile [email protected] cv 3    Company views profile   7/24/2013 11:18
[email protected] my company1aa viewed profile [email protected] cv 2    Company views profile   7/24/2013 11:17
[email protected] my company1aa viewed profile [email protected] cv 4    Company views profile   7/24/2013 11:17
[email protected] my company1aa viewed profile [email protected] cv 1    Company views profile   7/24/2013 11:16

I want to break first column to 4 columns such that ( in case of first row)

Employer email = [email protected]
company name = my company1aa
Registrant email = [email protected]
Full name = my name

and then remaining 2 columns as it is

Action = Compnay view Profile
Date = 7/24/2013 11:18

Please suggest how can I do this. I am using object data source for binding but I can get data table:

HRActionLog actionLog = new HRActionLog();
           DataTable dt = actionLog.GetList(Action,DateFrom,DateTo,CompanyId,RegistrantId,VacancyId,CurrentLanguage);

Do I need to create a new datatable with additional columns and populate it from dt ?

Please suggest

2
  • 1
    I would create a new datatable. IMHO this is the quickest and cleanest way.
    – Koen
    Jul 25, 2013 at 6:29
  • @Koen How to break first column ? Jul 25, 2013 at 6:36

2 Answers 2

0

Supposing you can't get the data from somewhere, the only option I see is to split the string into different parts using string.Split I'm not fond of it, but I don't see any other solution.

Taking into account that the string will always be in the format as you've given you can use the following snippet:

    var lst = new List<string>
                  {
                      "[email protected] my company1aa1 viewed profile [email protected] my name",
                      "[email protected] my company1aa2 viewed profile [email protected] cv 3",
                      "[email protected] my company1aa3 viewed profile [email protected] cv 2",
                      "[email protected] my company1aa4 viewed profile [email protected] cv 4",
                      "[email protected] my company1aa5 viewed profile [email protected] cv 1"
                  };
    foreach (var str in lst)
    {
        var i = str.IndexOf("viewed", StringComparison.OrdinalIgnoreCase);
        var part1 = str.Substring(0, i - 1); // -1 ommits the space before "viewed"
        var employerEmail = part1.Substring(0, part1.IndexOf(" ", StringComparison.OrdinalIgnoreCase));
        Console.WriteLine("EmployerEmail :" + employerEmail);
        var companyName = part1.Substring(part1.IndexOf(" ", StringComparison.OrdinalIgnoreCase) + 1); // +1 ommits the space
        Console.WriteLine("CompanyName :" + companyName);

        var part2 = str.Substring(i + 16); // +16 to start right after "viewed profile "
        var registrantEmail = part2.Substring(0, part2.IndexOf(" ", StringComparison.OrdinalIgnoreCase));
        Console.WriteLine("RegistrantEmail :" + registrantEmail);
        var fullName = part2.Substring(part2.IndexOf(" ", StringComparison.OrdinalIgnoreCase) + 1); // +1 ommits the space
        Console.WriteLine("FullName :" + fullName);
    }

This code will only work when the strings are in format

<email> <companyname> viewed profile <email> <fullname>

If this changes, you'll have to adapt the snippet. But as mentioned on top: if you can find this data somewhere in the database, it's a lot better.

0

As @Koen said creating a new datatable is the best option.

As for breaking the first column into smaller strings / variables you would require a complex regexp and would still be prone to errors due to the fact that the string in your first column will, most probably, vary greatly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.