Posted By

krisdb on 06/18/08


Tagged

c-sharp


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

flurk118
ad5qa
umang_nine
LeeRJohnson
vali29
rodrigojuarez
mheinrich


outputCSV


 / Published in: C#
 

  1. protected void OutputCSV(DataView dvResults)
  2. {
  3. Response.AppendHeader("content-disposition", "attachment; filename=GiftCardRegistrations.csv");
  4. Response.ContentType = "application/vnd.ms-excel";
  5.  
  6. int columns = dvResults.Table.Columns.Count;
  7.  
  8. //column names
  9. int i = 0;
  10. while (i < columns)
  11. {
  12. if (i > 0)
  13. Response.Write(",");
  14.  
  15. Response.Write(dvResults.Table.Columns[i].ColumnName);
  16. i++;
  17. }
  18.  
  19. Response.Write("\n");
  20.  
  21.  
  22. // data
  23. foreach (DataRowView drResults in dvResults)
  24. {
  25. i = 0;
  26. while (i < columns)
  27. {
  28. if (i > 0)
  29. Response.Write(",");
  30.  
  31. Response.Write("\"" + drResults[i] + "\"");
  32. i++;
  33. }
  34.  
  35. Response.Write("\n");
  36. }
  37.  
  38. Response.End();
  39. }

Report this snippet  

You need to login to post a comment.