csv file giving format error with this code
What I have tried:
<pre>private byte[] BuildCollectionReport(string fromDate, string toDate) {
var FileData = new byte[] { };
using (System.IO.MemoryStream stream = new MemoryStream()) {
using (ExcelPackage p = new ExcelPackage(stream)) {
var s = p.Workbook.Worksheets.Add("IN Collection Report");
try {
string[] columnHeaders = new string[]{
"TypeName"
,"CategoryName"
,"TSSubject"
,"TSStatus"
,"TSContactId"
,"TSAgreementId"
,"Agreement Number"
,"TSDate"
,"TSAmount"
};
foreach (var c in columnHeaders) {
SetValue(ref s, 1, Array.IndexOf(columnHeaders, c) + 1, c);
}
int rowIndex = 2;
var sp = (StoredProcedure)new StoredProcedure(UserConnection, "CollectionReport")
.WithParameter("FromDate", fromDate)
.WithParameter("ToDate", toDate);
try {
using (DBExecutor dbExecutor = UserConnection.EnsureDBConnection())
{
using (var reader = dbExecutor.ExecuteReader(sp.GetSqlText(), sp.Parameters)) {
while(reader.Read()) {
for (int n = 0; n < reader.FieldCount; n++)
{
SetValue(ref s, rowIndex, n+1, reader.GetValue(n).ToString());
}
rowIndex++;
}
}
}
} catch {}
} catch {}
FileData = p.GetAsByteArray();
}
}
return FileData;
}
private void SetValue(ref ExcelWorksheet Sheet, int Row, int Cell, string Value) {
if (!string.IsNullOrWhiteSpace(Value)) {
Sheet.Cells[Row, Cell].Value = Value;
}
}
public Stream DownloadCollectionReport(string fromDate, string toDate)
{
var excelData = BuildCollectionReport(fromDate, toDate);
var reportStream = new MemoryStream(excelData);
WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
WebOperationContext.Current.OutgoingResponse.ContentLength = reportStream.Length;
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition",
"attachment; filename=CollectionReport.csv");
return reportStream;
}