I am exporting a CSV file from an ASP .NET page but somehow it contains HTML code and the data that are supposed to be exported (ie. 29 rows) are not complete (ie from supposedly 29 rows, only 17 shows up, and then HTML all the way down).
Here are my codes:
private void WriteToCSV()
{
var clientId = int.Parse(ddlClients.SelectedValue);
var taskTypeId = int.Parse(ddlTaskType.SelectedValue);
var mtComponent = new MainTableComponent();
var data = mtComponent.GetMainTableRecords(clientId, 0, taskTypeId);
if (data == null || data.Count == 0)
{
ShowAlertMessage("No available data to export.");
return;
}
var callerId = GetClientList().FirstOrDefault(x => x.ClientId == clientId).CallerId;
string attachment = string.Concat("attachment; filename=CallList_", ddlClients.SelectedItem.Text,
"_", ddlTaskType.SelectedItem.Text, "_", DateTime.Now.ToString("yyyyMMdd"), ".csv");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
HttpContext.Current.Response.Write("row,phone1,phone2,phone3,caller_id");
foreach (var d in data)
WriteUserInfo(d, callerId);
HttpContext.Current.Response.End();
}
private void WriteUserInfo(MainTableEntity person, string callerId)
{
HttpContext.Current.Response.Write(Environment.NewLine);
StringBuilder stringBuilder = new StringBuilder();
AddComma(person.RowNumber.ToString(), stringBuilder);
AddComma(FormatNumber(person.MobileNumber), stringBuilder);
AddComma("", stringBuilder);
AddComma("", stringBuilder);
AddComma(callerId, stringBuilder);
stringBuilder.Remove(stringBuilder.Length - 1, 1);
HttpContext.Current.Response.Write(stringBuilder.ToString());
}
private void AddComma(string value, StringBuilder stringBuilder)
{
value.Replace(",", "");
stringBuilder.Append(value).Append(",");
}
private string FormatNumber(string number)
{
if (string.IsNullOrWhiteSpace(number))
return null;
string n = number.Replace('-', ' ').Replace(" ", "").Trim();
return n;
}
The funny thing is, this does not happen on my local machine and on our test environment. It only happens in our production environment.
Any help is appreciated. Thank you very much!
Related
I've been reading through:
https://www.aspsnippets.com/Articles/Export-data-from-SQL-Server-to-CSV-file-in-ASPNet-using-C-and-VBNet.aspx
Rather than only have the option to download as csv as described there in:
//Download the CSV file.
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=SqlExport.csv");
Response.Charset = "";
Response.ContentType = "application/text";
Response.Output.Write(csv);
Response.Flush();
Response.End();
is there a way using native asp.net to first zip the csv output from the csv variable in Response.Output.Write(csv); so that the user downloads SqlExport.zip rather than SqlExport.csv?
Roughly based on this, you can create a zip file while streaming it to the client;
Response.ContentType = "application/octet-stream";
Response.Headers.Add("Content-Disposition", "attachment; filename=\"SqlExport.zip\"");
using var archive = new ZipArchive(Response.Body, ZipArchiveMode.Create);
var entry = archive.CreateEntry("SqlExport.csv");
using var entryStream = entry.Open();
entryStream.Write(csv); // write the actual content here
entryStream.Flush();
Though rather than appending to a single csv string, you should probably consider using a StreamWriter to write each snippet of text directly into the response stream. Substituting from your linked csv example;
using var sw = new StreamWriter(entryStream);
// TODO write header
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
//Add the Data rows.
await sw.WriteAsync(row[column.ColumnName].ToString().Replace(",", ";") + ',');
}
//Add new line.
await sw.WriteLineAsync();
}
Though that is a terrible example of a csv file. Rather than substituting ';' characters, the string should be quoted & all quotes escaped.
However Response.Body is only available in .net 5 / core. To write directly to a http response in .net 4.8 or earlier, you'll have to write your own HttpContent. Putting everything together, including a better csv formatter;
public class ZipContent : HttpContent
{
private DataTable dt;
private string name;
public ZipContent(DataTable dt, string name = null)
{
this.dt = dt;
this.name = name ?? dt.TableName;
Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = $"{name}.zip"
};
}
private string formatCsvValue(string value)
{
if (value == null)
return "";
if (value.Contains('"') || value.Contains(',') || value.Contains('\r') || value.Contains('\n'))
return $"\"{value.Replace("\"", "\"\"")}\"";
return value;
}
private IEnumerable<DataColumn> Columns()
{
// Why is this not already an IEnumerable<DataColumn>?
foreach (DataColumn col in dt.Columns)
yield return col;
}
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
using var archive = new ZipArchive(stream, ZipArchiveMode.Create);
var entry = archive.CreateEntry($"{name}.csv");
using var entryStream = entry.Open();
using var sw = new StreamWriter(entryStream);
await sw.WriteLineAsync(
string.Join(",",
Columns()
.Select(c => formatCsvValue(c.ColumnName))
));
foreach (DataRow row in dt.Rows)
{
await sw.WriteLineAsync(
string.Join(",",
row.ItemArray
.Select(o => formatCsvValue(o?.ToString()))
));
}
}
protected override bool TryComputeLength(out long length)
{
length = 0;
return false;
}
}
Have a look at the ZipArchive Class
you can use public System.IO.Compression.ZipArchiveEntry CreateEntry (string entryName); to create an ZipEntry nd add it to an archive
I'm trying to export data as a CSV file in C#, but the problems starts when i'm trying to import the csv file in excel.
In excel I'm using the function "import from text", and afterwards I set the delimiter to semicolon
My problem is that some of the columns have linebreaks and then the import in excel is wrong.
I have tried with single and doubles quotes with no luck.
I have searched for a solution, but has not found one yet.
Anyone knows if lumenworks has a export function, because i'm using this for the import function
The problem is the export function and the linebreaks are required.
if (list.Any())
{
result = list.Select(i => new
{
i.Product.ProductIdentifier,
i.Product.Header,
body = string.Format("\"" + "{0}" + "\"", i.Product.Body),
Active = i.Product.Active ? 1 : 0,
Approved = i.Product.Approved ? 1 : 0,
i.Product.Sort,
i.Product.MetaDescription,
i.Product.MetaKeywords,
i.CatalogMenuItemContentItemId
}).ToList().ToCsv(";");
}
string attachment = "attachment; filename=myfile.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
HttpContext.Current.Response.Write(result);
HttpContext.Current.Response.End();
public static string ToCsv<T>(this IEnumerable<T> items, string seperator = ",")
where T : class
{
var csvBuilder = new StringBuilder();
var properties = typeof(T).GetProperties();
foreach (T item in items)
{
string line = string.Join(seperator, properties.Select(p => p.GetValue(item, null).ToCsvValue()).ToArray());
csvBuilder.AppendLine(line);
}
return csvBuilder.ToString();
}
private static string ToCsvValue<T>(this T item)
{
return string.Format("{0}", HttpUtility.HtmlDecode(item.ToString()));
}
Any idea ?
What am I doing wrong here?
I am trying to export a list of customers to a downloadable csv file, that can be viewed in Excel
Currently I do not care about buttons and listeners, I just want to generate the actual csv file.
public class CSVExporter
{
public static void WriteToCSV(List<CustomerInformation> customerList)
{
string attachment = "attachment; filename=CustomerList.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
HttpContext.Current.Response.AddHeader("Pragma", "public");
WriteColumnName();
foreach (CustomerInformation customer in customerList)
{
WriteUserInfo(customer);
}
HttpContext.Current.Response.End();
}
private static void WriteUserInfo(CustomerInformation customer)
{
StringBuilder stringBuilder = new StringBuilder();
AddComma(customer.name, stringBuilder);
AddComma(customer.email, stringBuilder);
AddComma(customer.username, stringBuilder);
AddComma(customer.mobilenumber, stringBuilder);
AddComma(customer.dateCreated, stringBuilder);
AddComma(customer.birthday, stringBuilder);
HttpContext.Current.Response.Write(stringBuilder.ToString());
HttpContext.Current.Response.Write(Environment.NewLine);
}
private static void AddComma(string value, StringBuilder stringBuilder)
{
stringBuilder.Append(value.Replace(',', ' '));
stringBuilder.Append(", ");
}
private static void WriteColumnName()
{
string columnNames = "Name, Email, Username, Mobile number, Date created, Birthdate";
HttpContext.Current.Response.Write(columnNames);
HttpContext.Current.Response.Write(Environment.NewLine);
}
}
When I run the code, I get this:
I've simply used this
public HttpResponseMessage DownloadCsv()
{
const string csv = "mobile,type,amount\r\n123456789,0,200\r\n56987459,0,200";
var result = new HttpResponseMessage(HttpStatusCode.OK) {Content = new StringContent(csv)};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Sample.csv"
};
return result;
}
i m using the following code to send a csv file through http request.The code runs fine but the file is not downloaded on client machine , not save as menu ,nothing appears . The same code i have used for another report and it works fine and file is also downloaded . I m running the project on local host. but not for the button btnGenerateReport.
protected void btnGenerateReport_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
MHSProgressNotesBL oMHSProgressNote = new MHSProgressNotesBL();
int ClientId = Convert.ToInt32(ddlClients.SelectedValue);
int LocationId = Convert.ToInt32(ddlLocations.SelectedValue);
int PractitionerId = Convert.ToInt32(ddlPractitioner.SelectedValue);
dt = oMHSProgressNote.FetchBillingReport(LocationId, ClientId, PractitionerId, Convert.ToDateTime(txtNoteStartDate.Text), Convert.ToDateTime(txtNoteEndDate.Text));
WriteToCSV(dt);
}
private void WriteToCSV(DataTable dt)
{
try
{
string attachment = "attachment; filename=ClientRoster.csv";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "text/csv";
//HttpContext.Current.Response.AddHeader("Pragma", "public");
WriteColumnName(dt);
foreach (DataRow clientdata in dt.Rows)
{
WriteUserInfo(clientdata);
}
HttpContext.Current.Response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception e)
{ }
}
private void WriteColumnName(DataTable dt)
{
string columnNames = "";
// Write Header row
foreach (DataColumn item in dt.Columns)
{
if (item.ColumnName.Contains(','))
{
columnNames += "\"" + item.ColumnName + "\"";
}
else
{
columnNames += item.ColumnName + ",";
}
}
columnNames = columnNames.TrimEnd(',');
HttpContext.Current.Response.Write(columnNames);
HttpContext.Current.Response.Write(Environment.NewLine);
}
private void WriteUserInfo(DataRow clientDataRow)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (var item in clientDataRow.ItemArray)
{
AddComma((string)(item == System.DBNull.Value ? "" : item.ToString()), stringBuilder);
}
HttpContext.Current.Response.Write(stringBuilder.ToString().TrimEnd(','));
HttpContext.Current.Response.Write(Environment.NewLine);
}
private void AddComma(string value, StringBuilder stringBuilder)
{
if (value.Contains('"'))
{
value = value.Replace(#"""", #"""""");
value = "\"" + value + "\"";
}
else if (value.Contains(','))
{
value = "\"" + value + "\"";
}
stringBuilder.Append(value);
stringBuilder.Append(",");
}
The code is ok ......... the main problem was the button btnGenerateReport was inside of a update panel .Due to which an error was rising :The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near 'Client,Practitioner,'.
The code worked fine after placing the button outside of update panel.
whats the best way to export a Datagrid to excel? I have no experience whatsoever in exporting datagrid to excel, so i want to know how you guys export datagrid to excel.
i read that there are a lot of ways, but i am thinking to just make a simple export excel to datagrid function.i am using asp.net C#
cheers..
The simplest way is to simply write either csv, or html (in particular, a <table><tr><td>...</td></tr>...</table>) to the output, and simply pretend that it is in excel format via the content-type header. Excel will happily load either; csv is simpler...
Here's a similar example (it actually takes an IEnumerable, but it would be similar from any source (such as a DataTable, looping over the rows).
public static void WriteCsv(string[] headers, IEnumerable<string[]> data, string filename)
{
if (data == null) throw new ArgumentNullException("data");
if (string.IsNullOrEmpty(filename)) filename = "export.csv";
HttpResponse resp = System.Web.HttpContext.Current.Response;
resp.Clear();
// remove this line if you don't want to prompt the user to save the file
resp.AddHeader("Content-Disposition", "attachment;filename=" + filename);
// if not saving, try: "application/ms-excel"
resp.ContentType = "text/csv";
string csv = GetCsv(headers, data);
byte[] buffer = resp.ContentEncoding.GetBytes(csv);
resp.AddHeader("Content-Length", buffer.Length.ToString());
resp.BinaryWrite(buffer);
resp.End();
}
static void WriteRow(string[] row, StringBuilder destination)
{
if (row == null) return;
int fields = row.Length;
for (int i = 0; i < fields; i++)
{
string field = row[i];
if (i > 0)
{
destination.Append(',');
}
if (string.IsNullOrEmpty(field)) continue; // empty field
bool quote = false;
if (field.Contains("\""))
{
// if contains quotes, then needs quoting and escaping
quote = true;
field = field.Replace("\"", "\"\"");
}
else
{
// commas, line-breaks, and leading-trailing space also require quoting
if (field.Contains(",") || field.Contains("\n") || field.Contains("\r")
|| field.StartsWith(" ") || field.EndsWith(" "))
{
quote = true;
}
}
if (quote)
{
destination.Append('\"');
destination.Append(field);
destination.Append('\"');
}
else
{
destination.Append(field);
}
}
destination.AppendLine();
}
static string GetCsv(string[] headers, IEnumerable<string[]> data)
{
StringBuilder sb = new StringBuilder();
if (data == null) throw new ArgumentNullException("data");
WriteRow(headers, sb);
foreach (string[] row in data)
{
WriteRow(row, sb);
}
return sb.ToString();
}
You can do it in this way:
private void ExportButton_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(dataGrid);
dataGrid.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
Complete example here.
SpreadsheetGear for .NET will do it.
You can see live ASP.NET samples with C# and VB source code here. Several of these samples demonstrate converting a DataSet or DataTable to Excel - and you can easily get a DataSet or DataTable from a DataGrid. You can download the free trial here if you want to try it yourself.
Disclaimer: I own SpreadsheetGear LLC