Advertisement
Guest User

Untitled

a guest
May 26th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. SqlCommand SqlCommand = new SqlCommand(sqlQuery, SqlConnection);
  2. SqlCommand.CommandType = CommandType.Text;
  3. SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
  4. DataSet ds = new DataSet();
  5. da.Fill(ds);
  6. //string fileNames = ddlCompany.SelectedItem.Text.ToString().Replace(" ", "_");
  7. DateTime dtime = DateTime.Now.ToLocalTime();
  8. string fileNames = "MIBIS_ONLINE_REPORT_" + dtime.Year + dtime.Month + dtime.Day + dtime.Hour + dtime.Minute + dtime.Second;
  9. string attachment = "attachment; filename = " + fileNames + ".xls";
  10. Context.Response.ClearContent();
  11. //Context.Response.ContentType = "Application/x-msexcel";
  12. Context.Response.AddHeader("content-disposition", attachment);
  13. Context.Response.Charset = "utf-8";
  14. //Context.Response.ContentType = "Application/x-msexcel";
  15. Context.Response.ContentType = "Application/vnd.ms-excel";
  16. // Context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  17. Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
  18.  
  19. //Context.Response.ContentEncoding = System.Text.Encoding.Unicode;
  20. Context.Response.Write(ExportToCSVFile(ds.Tables[0]));
  21. Context.Response.End();
  22. }
  23. }
  24. catch (Exception e1)
  25. {
  26. SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("Problem With Export ", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, e1.Message + e1.StackTrace, null);
  27. }
  28. }
  29. private string ExportToExcelFile(DataTable dtTable)
  30. {
  31.  
  32. string tab = "";
  33. StringBuilder sbldr = new StringBuilder();
  34. if (dtTable.Columns.Count != 0)
  35. {
  36. foreach (DataColumn col in dtTable.Columns)
  37. {
  38. sbldr.Append(tab + col.ColumnName);
  39. tab = "t";
  40. }
  41. sbldr.Append("n");
  42. foreach (DataRow row in dtTable.Rows)
  43. {
  44. tab = "";
  45. foreach (DataColumn column in dtTable.Columns)
  46. {
  47. string temp = row[column].ToString();
  48. string txt = temp.ToString(CultureInfo.InvariantCulture);
  49.  
  50. if (column.ToString() == "Currency Rate(English)")
  51. {
  52. NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
  53.  
  54. // Displays a value with the default separator (".").
  55. // Displays the same value with a blank as the separator.
  56. nfi.NumberDecimalSeparator = ".";
  57. //var priceAsDecimal = Decimal.Parse(temp, NumberStyles.Currency);
  58. //string priceAsDecimal1=priceAsDecimal.ToString("C");
  59. // double number = Convert.ToDouble(temp);
  60. // string a=number.ToString("G", CultureInfo.InvariantCulture);
  61. //sbldr.Append(tab + a);
  62. sbldr.Append(tab + txt.ToString(nfi).Replace("n", " ").Replace("nr", ";").Replace("r", " ").Replace("0", "0"));
  63. //sbldr.Append(tab + a.Replace("n", " ").Replace("nr", ";").Replace("r", " ").Replace("0", "0"));
  64. }
  65. //else if (column.ToString() == "Currency Rate(German)")
  66. //{
  67. // string txt = temp.ToString(cultureDe);
  68. // sbldr.Append(tab + txt.ToString().Replace("n", " ").Replace("nr", ";").Replace("r", " ").Replace("0", "0"));
  69. //}
  70. else
  71. {
  72. sbldr.Append(tab + row[column].ToString().Replace("n", " ").Replace("nr", ";").Replace("r", " ").Replace("0", "0"));
  73. }
  74. tab = "t";
  75. }
  76. sbldr.Append("n");
  77. }
  78. }
  79. return sbldr.ToString();
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement