[ad_1]
csv 文件给出此代码的格式错误
我尝试过的:
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; }
解决方案1
首先查看您生成的实际 CSV 数据 – 我们无法这样做,因为我们没有您的输入可供使用。 因此,将 CSV 数据保存到文件中,然后在编辑器中打开它并亲自仔细查看。
CSV 不是一种复杂的格式,因此它可能就像缺少双引号一样简单,或者您的数据包含弄乱输出的字符。
但是,如果不查看导致问题的实际 CSV 数据,就无法知道从哪里开始查找。
[ad_2]
コメント