Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SqlCommand SqlCommand = new SqlCommand(sqlQuery, SqlConnection);
- SqlCommand.CommandType = CommandType.Text;
- SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
- DataSet ds = new DataSet();
- da.Fill(ds);
- //string fileNames = ddlCompany.SelectedItem.Text.ToString().Replace(" ", "_");
- DateTime dtime = DateTime.Now.ToLocalTime();
- string fileNames = "MIBIS_ONLINE_REPORT_" + dtime.Year + dtime.Month + dtime.Day + dtime.Hour + dtime.Minute + dtime.Second;
- string attachment = "attachment; filename = " + fileNames + ".xls";
- Context.Response.ClearContent();
- //Context.Response.ContentType = "Application/x-msexcel";
- Context.Response.AddHeader("content-disposition", attachment);
- Context.Response.Charset = "utf-8";
- //Context.Response.ContentType = "Application/x-msexcel";
- Context.Response.ContentType = "Application/vnd.ms-excel";
- // Context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
- Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
- //Context.Response.ContentEncoding = System.Text.Encoding.Unicode;
- Context.Response.Write(ExportToCSVFile(ds.Tables[0]));
- Context.Response.End();
- }
- }
- catch (Exception e1)
- {
- SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("Problem With Export ", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, e1.Message + e1.StackTrace, null);
- }
- }
- private string ExportToExcelFile(DataTable dtTable)
- {
- string tab = "";
- StringBuilder sbldr = new StringBuilder();
- if (dtTable.Columns.Count != 0)
- {
- foreach (DataColumn col in dtTable.Columns)
- {
- sbldr.Append(tab + col.ColumnName);
- tab = "t";
- }
- sbldr.Append("n");
- foreach (DataRow row in dtTable.Rows)
- {
- tab = "";
- foreach (DataColumn column in dtTable.Columns)
- {
- string temp = row[column].ToString();
- string txt = temp.ToString(CultureInfo.InvariantCulture);
- if (column.ToString() == "Currency Rate(English)")
- {
- NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
- // Displays a value with the default separator (".").
- // Displays the same value with a blank as the separator.
- nfi.NumberDecimalSeparator = ".";
- //var priceAsDecimal = Decimal.Parse(temp, NumberStyles.Currency);
- //string priceAsDecimal1=priceAsDecimal.ToString("C");
- // double number = Convert.ToDouble(temp);
- // string a=number.ToString("G", CultureInfo.InvariantCulture);
- //sbldr.Append(tab + a);
- sbldr.Append(tab + txt.ToString(nfi).Replace("n", " ").Replace("nr", ";").Replace("r", " ").Replace("0", "0"));
- //sbldr.Append(tab + a.Replace("n", " ").Replace("nr", ";").Replace("r", " ").Replace("0", "0"));
- }
- //else if (column.ToString() == "Currency Rate(German)")
- //{
- // string txt = temp.ToString(cultureDe);
- // sbldr.Append(tab + txt.ToString().Replace("n", " ").Replace("nr", ";").Replace("r", " ").Replace("0", "0"));
- //}
- else
- {
- sbldr.Append(tab + row[column].ToString().Replace("n", " ").Replace("nr", ";").Replace("r", " ").Replace("0", "0"));
- }
- tab = "t";
- }
- sbldr.Append("n");
- }
- }
- return sbldr.ToString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement