Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
csv file giving format error with this code

What I have tried:

C#
<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;
		}
Posted
Updated 8-Dec-21 21:24pm
v2

1 solution

Start by looking at the actual CSV Data you generate - we can't do that as we don't have your input to work from. So save the CSV data to a file, and open it in an editor and have a good look at it yourself.
CSV isn't a complicated format, so it may be as simple as a missing double quotes, or your data containing characters that mess up the output.
But without looking at the actual CSV data that give teh problem, there is no way to tell where to start looking.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900