C# CsvExport

1.

Install-Package CsvExport

2.

using Csv;
using System.Runtime.InteropServices;

namespace ConsoleApp8
{
    internal class Program
    {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

        static void Main(string[] args)
        {
            Task.Run(() =>
            {
                MonitorMemory();
            });
            CSVExportDemo();            
        }

        private static void MonitorMemory()
        {
            while (true)
            {
                var proc = System.Diagnostics.Process.GetCurrentProcess();
                Console.WriteLine($"proc.PrivateMemorySize64:{proc.PrivateMemorySize64}");
                Thread.Sleep(1000);
            }
        }

        static void CSVExportDemo()
        {
            List<Book> booksList = new List<Book>();
            for (int i = 0; i < 10000000; i++)
            {
                booksList.Add(new Book()
                {
                    Id = i + 1,
                    ISBN = $"ISBN_{i + 1},{Guid.NewGuid().ToString("N")}",
                    Name = $"Name_{i + 1}",
                    Title = $"Title_{i + 1}",
                    Topic = $"Topic_{i + 1}"
                });
            }

            string csvFile = "TestCsvExport.csv";
            var csvExport = new CsvExport(columnSeparator: ",", includeColumnSeparatorDefinitionPreamble: true, includeHeaderRow: true);
            csvExport.AddRows(booksList);
            string csvStr = csvExport.Export();
            csvExport.ExportToFile(csvFile);
            MessageBox(new IntPtr(0), $"Export list to {csvFile}", "CSVExport", 0);
            Console.WriteLine($"Export list to {csvFile}");
        }
    }

    public class Book
    {
        public int Id { get; set; }
        public string ISBN { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2024-12-12 23:26  FredGrit  阅读(32)  评论(0)    收藏  举报