public void ExportToFileCsv() { Response.Clear(); var query = eqService.GetQuery(); if (!query.IsEmpty) { var sql = eqService.BuildQuery(query); eqService.Paging.Enabled = false; DataSet dataset = eqService.GetDataSetBySql(sql); if (dataset != null) { Response.ContentType = "text/csv"; Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", HttpUtility.UrlEncode("report.csv"))); DbExport.ExportToCsv(dataset, Response.Output, CsvFormats.Default); } else { ErrorResponse("Empty dataset"); } } else { ErrorResponse("Empty query"); } }
protected void ExportResultToCSV(string fileName) { ResultDS.SelectCommand = SqlTextBox.Text; DataView view = (DataView)ResultDS.Select(DataSourceSelectArguments.Empty); if (view == null) { return; } Response.Clear(); Response.ContentType = "text/csv"; Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", HttpUtility.UrlEncodeUnicode(fileName))); DbExport.ExportToCsv(view.Table, Response.Output, CsvFormats.Default); Response.End(); }
/// <summary> /// Writes information to CSV file. /// </summary> /// <returns></returns> private void ExportToFileCsv(string sql) { HttpContext.Response.Clear(); if (!string.IsNullOrEmpty(sql)) { using (var connection = eqService.ConnectionResolver()){ connection.Open(); var command = connection.CreateCommand(); command.CommandText = sql; command.CommandType = CommandType.Text; var dataset = command.ExecuteReader(); if (dataset != null) { HttpContext.Response.ContentType = "text/csv";; HttpContext.Response.Headers.Add("Content-Disposition", string.Format("attachment; filename=\"{0}\"", UrlEncoder.Default.Encode("report.csv"))); StreamWriter dataStream = new StreamWriter(HttpContext.Response.Body); DbExport.ExportToCsv(dataset, dataStream, CsvFormats.Default); } else { ErrorResponse("Empty dataset"); } eqService.ConnectionResolver().Close(); } } else { ErrorResponse("Empty query"); } }