Problema con la exportación de datos a un archivo CSV en .NET core

programación


archivo csv que da error de formato con este código

Lo que he probado:

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;
		}

Solución 1

Comience mirando los datos CSV reales que genera; no podemos hacerlo porque no tenemos su información para trabajar. Así que guarde los datos CSV en un archivo, ábralo en un editor y mírelo usted mismo.
CSV no es un formato complicado, por lo que puede ser tan simple como que falten comillas dobles o que sus datos contengan caracteres que estropeen el resultado.
Pero sin mirar los datos CSV reales que causan el problema, no hay forma de saber por dónde empezar a buscar.

コメント

タイトルとURLをコピーしました