最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to transform object list into CSV and zip it - Stack Overflow

programmeradmin1浏览0评论

I need to generate multiple .CSV files from an object and then zip all the files.

This is what I got, I can't make a way to split the .csv file in 2 if it's needed, and the .csv file is broken, I got a lot of text errors in it that wasn't on the list...

What is the best approach to do this? Am I close to the right thing?

Controller:

IActionResult GetDados()
{
    var list = _itensService.GetDadosXXX().ToList();
    var sourceFiles = _itensService.PrepareData(list);

    byte[] bytes = null;

    using (MemoryStream ms = new MemoryStream()) 
    {
        using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Create, true)) 
        {
            foreach(SourceFile sf in sourceFile) 
            { 
                ZipArchiveEntry zipItem = zip.CreateEntry(sf.Name + "." + sd.Extension)

                using (MemoryStream fileMemoryStream = new MemoryStream(sf.FileBytes))
                {
                    using (Stream entryStream = zipItem.Open())
                    { 
                        fileMemoryStream.CopyTo(entryStream);
                    }
                }
            }
        }
    }

    return File(FileBytes, "application/zip", ZipArchiveTest.zip");
}

Service:

public IEnumerable<SourceFile> PreparaData(List<XXXData> data)
{
    List<SourceFile> result = new List<SourceFile>();
    string archiveName = "Test_";
  
    var mStream = new MemoryStream();
    int aux = 1;
    foreach(object item in data) 
    {
        var binFormatter = new BinaryFormatter();
        binFormatter.Serialize(mStream, item);
        aux++;
    }

    result.Add(new SourceFile { Name = archiveName + aux, Extension = "csv", FileBytes = mStream.toArray() });

    return result;
}

SourceFile:

public class SourceFile 
{
    public string Name { get; set; } 
    public string Extension { get; set; }
    public Byte[] FileBytes { get; set; }
}

Let me know if something more is needed...

I need to generate multiple .CSV files from an object and then zip all the files.

This is what I got, I can't make a way to split the .csv file in 2 if it's needed, and the .csv file is broken, I got a lot of text errors in it that wasn't on the list...

What is the best approach to do this? Am I close to the right thing?

Controller:

IActionResult GetDados()
{
    var list = _itensService.GetDadosXXX().ToList();
    var sourceFiles = _itensService.PrepareData(list);

    byte[] bytes = null;

    using (MemoryStream ms = new MemoryStream()) 
    {
        using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Create, true)) 
        {
            foreach(SourceFile sf in sourceFile) 
            { 
                ZipArchiveEntry zipItem = zip.CreateEntry(sf.Name + "." + sd.Extension)

                using (MemoryStream fileMemoryStream = new MemoryStream(sf.FileBytes))
                {
                    using (Stream entryStream = zipItem.Open())
                    { 
                        fileMemoryStream.CopyTo(entryStream);
                    }
                }
            }
        }
    }

    return File(FileBytes, "application/zip", ZipArchiveTest.zip");
}

Service:

public IEnumerable<SourceFile> PreparaData(List<XXXData> data)
{
    List<SourceFile> result = new List<SourceFile>();
    string archiveName = "Test_";
  
    var mStream = new MemoryStream();
    int aux = 1;
    foreach(object item in data) 
    {
        var binFormatter = new BinaryFormatter();
        binFormatter.Serialize(mStream, item);
        aux++;
    }

    result.Add(new SourceFile { Name = archiveName + aux, Extension = "csv", FileBytes = mStream.toArray() });

    return result;
}

SourceFile:

public class SourceFile 
{
    public string Name { get; set; } 
    public string Extension { get; set; }
    public Byte[] FileBytes { get; set; }
}

Let me know if something more is needed...

Share Improve this question edited Jan 30 at 13:43 Pedro Martins asked Jan 30 at 4:48 Pedro MartinsPedro Martins 11 bronze badge 8
  • Why rework a well-defined data structure into sloppy CSV? Why? – Sergey A Kryukov Commented Jan 30 at 5:02
  • Is it a typo or you're not using aux in your Service? This way all objects' Name in Controller's sourceFiles have the same name. Furthermore, aux is not declared and your foreach definition is missing item declaration. – Mike-Kilo Commented Jan 30 at 7:54
  • What is BinFormatter? – Klaus Gütter Commented Jan 30 at 11:21
  • Aux is just to count how many archives it will have, but as I said, I can only do 1 archieve, so its not working as intended... I edited all the info that was missing! About why go with csv the answer is simple.... They asked for it haha, this method will bring close to 1KK data, so csv is the best way to handle it... – Pedro Martins Commented Jan 30 at 12:27
  • 1 That's BinaryFormatter, not BinFormatter. And it is definitely not meant to write text files. And it is also deprecated. – Klaus Gütter Commented Jan 30 at 13:14
 |  Show 3 more comments

1 Answer 1

Reset to default 0

BinaryFormatter is obsolete, and should not be used for anything except loading legacy data, and then only with great care due to the security issues that made it obsolete. BinaryFormatter will also use its own internal binary format for objects, hence the name, and this is format has nothing to do whatsoever with comma separated values.

If your object is a simple collection of plain properties you can use CSV helper to help create the csv file. Do not write to an intermediate file, just write to the entry stream directly:

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}
...
var records = new List<Foo>
{
    new Foo { Id = 1, Name = "one" },
};
...

using (var csv = new CsvWriter(entryStream , CultureInfo.InvariantCulture))
{
    csv.WriteRecords(records);
}

If your objects are more complex you should probably consider a serialization format that supports hierarchical objects, like json, xml, protobuf etc.

发布评论

评论列表(0)

  1. 暂无评论