2016-12-12 71 views
1

以下是我將數據導出到CSV的方法。如何從內存流中獲取所有數據到CSV?

public MemoryStream ExportToCsv(string jsonData, HttpResponseBase response, string fileName) 
{ 
    using (MemoryStream stream = new MemoryStream()) 
    { 
     StreamWriter writer = new StreamWriter(stream); 
     try 
     { 
      String s = JsonToCsv(jsonData, ","); 
      writer.Write(s); 
      stream.Position = 0; 
     } 
     catch (Exception ex) 
     { 
      clsErrorHandling.WriteLog("CSVExport", "GenerateStreamFromString", ex.Message); 
      throw; 
     } 
     finally 
     { 
      writer.Flush(); 
      writer.Close(); 
      response.Clear(); 
      response.Buffer = true; 
      response.ContentType = "application/csv"; 
      response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ""); 
      response.BinaryWrite(stream.ToArray()); 
     } 

     return stream; 
    } 
} 

#region Private CSV Block 

private String JsonToCsv(string jsonData, string delimiter) 
{ 
    try 
    { 
     using (StringWriter swObj = new StringWriter()) 
     { 
      using (var csv = new CsvWriter(swObj)) 
      { 
       csv.Configuration.SkipEmptyRecords = true; 
       csv.Configuration.WillThrowOnMissingField = false; 
       csv.Configuration.Delimiter = delimiter; 

       using (var dt = jsonStringToTable(jsonData)) 
       { 
        foreach (DataColumn col in dt.Columns) 
        { 
         csv.WriteField(col.ColumnName); 
        } 
        csv.NextRecord(); 

        foreach (DataRow row in dt.Rows) 
        { 
         for (var i = 0; i < dt.Columns.Count; i++) 
         { 
          csv.WriteField(row[i]); 
         } 
         csv.NextRecord(); 
        } 
       } 

      } 
      return swObj.ToString(); 
     } 

    } 
    catch (Exception ex) 
    { 
     clsErrorHandling.WriteLog("CSVExportHandler", "JsonToCsv", ex.Message); 
     return null; 
    } 
} 

private DataTable jsonStringToTable(string jsonContent) 
{ 
    DataTable dt = JsonConvert.DeserializeObject<DataTable>(jsonContent); 
    return dt; 
} 

#endregion 

如果記錄數小於100.那麼沒有問題。但是,當數據爲100或150+時,最後15-20個記錄不會寫入csv文件。

假設,如果記錄的數目是175,然後我在CSV得到一些圍繞163 如果記錄的數量是150,然後我在CSV得到arounf 131 &等。

這可能是什麼原因造成的?我該如何處理?

+0

你爲什麼註釋掉writer.Flush()和writer.Close()?這shloud將StreamWriter的內部緩衝區刷新到MemoryStream並解決您的問題。 –

+0

@Ňuf,我剛剛與取消註釋他們,但仍然是同一個問題。 –

+0

所以可能還有一個類似的問題,雖然我沒有看到它。你可以檢查swObj.ToString()是否返回所有記錄?這應該給我們提示是否問題在ExportToCsv()或JsonToCsv()中。 –

回答

1

我的public方法中的一些小更新修復了我的問題。

public MemoryStream ExportToCsv(string jsonData, HttpResponseBase response, string fileName) 
{ 
     using (MemoryStream stream = new MemoryStream()) 
     { 
      StreamWriter writer = new StreamWriter(stream); 
      try 
      { 
       response.Clear(); 
       response.Buffer = true; 
       response.ContentType = "application/csv"; 
       response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ""); 
       String s = JsonToCsv(jsonData, ","); 
       writer.Write(s); 
       writer.Flush(); 

       stream.Position = 0; 
       response.BinaryWrite(stream.ToArray()); 
      } 
      catch (Exception ex) 
      { 

       throw; 
      } 
      finally 
      { 
       writer.Close(); 
      } 
    return stream; 
} 

}

希望這有助於:)

相關問題