Beispiel #1
0
 public void WriteCsv(StreamWriter s)
 {
     foreach (var dt in DataTables)
     {
         s.WriteLine(string.Join(",", dt.Columns.Select(CsvUtil.EscapeCsv)));
         CsvUtil.WriteCsv(dt.Rows, s);
     }
 }
    private void WriteTable(SimpleDataTable sdt)
    {
        using var status = WaitStatus.StartRows(Path.GetFileName(_filePath));
        var fileMode = _truncateExistingFile ? FileMode.Create : FileMode.Append;

        using var stream = File.Open(_filePath, fileMode, FileAccess.Write, FileShare.None);
        using var writer = new StreamWriter(stream, _fileEncoding);
        if (_headerRow)
        {
            writer.WriteLine(string.Join(_separator, sdt.Columns.Select(c => CsvUtil.QuoteCsv(c, _separator))));
        }
        CsvUtil.WriteCsv(sdt.Rows, writer, status.IncrementRows, _separator, _cancel);
    }
Beispiel #3
0
        static void Main(string[] args)
        {
            var transactions = CsvUtil.ReadCsv("Transactions");
            var customers    = transactions.Select(t => t.Customer).Distinct();
            var offers       = transactions.Select(t => t.Offer).Distinct().ToList();

            offers.Sort();
            var results = new List <OutputData>();

            foreach (var customer in customers)
            {
                var result = new OutputData();
                result.Customer = customer;
                foreach (var offer in offers)
                {
                    int qtd = transactions.Where(t => t.Offer == offer && t.Customer.Equals(customer)).Count();
                    result.GetType().GetProperty("Offer" + offer).SetValue(result, qtd);
                }
                results.Add(result);
            }
            CsvUtil.WriteCsv(results, "DataPrepared");
        }
        public async Task LoadGrid()
        {
            await _gridLoadControl.DoLoad(async() => {
                var data = await Task.Run(() =>
                                          XlsUtil.ReadWorksheet(_filePath, _sheetMeta.Index, lastRowIndex: MAX_SHEET_ROWS - 1));

                // user might have closed the window while we were loading
                if (_grid.IsDisposed)
                {
                    return;
                }

                // reogrid doesn't seem to properly support newlines in quoted CSV strings but we're just using it for
                // preview purposes, so we'll just remove newlines.  it's fine here.  it's important that reogrid's
                // row numbers match what we think the row numbers are
                foreach (var row in data)
                {
                    for (var j = 0; j < row.Length; j++)
                    {
                        if (row[j] is string s && s.Contains('\n'))
                        {
                            row[j] = s.Replace("\r", "").Replace("\n", " ");
                        }
                    }
                }

                var columnCount = Math.Max(1, data.Max(x => x.Length));

                // the fastest way to get the data into the grid control seems to be via CSV
                using (var memoryStream = new MemoryStream()) {
                    await Task.Run(() => {
                        var streamWriter = new StreamWriter(
                            memoryStream, Encoding.Default, STREAM_BUFFER_SIZE, leaveOpen: true);
                        using (streamWriter) {
                            CsvUtil.WriteCsv(data, streamWriter);
                        }
                        memoryStream.Position = 0;
                    });

                    // user might have closed the window while we were loading
                    if (_grid.IsDisposed)
                    {
                        return;
                    }

                    _grid.Load(memoryStream, FileFormat.CSV);
                }

                var w = _grid.CurrentWorksheet;
                w.SetRows(data.Count);
                w.SetCols(columnCount);

                for (var i = 0; i < columnCount; i++)
                {
                    w.AutoFitColumnWidth(i);
                    var width = Math.Min(MAX_SHEET_COLUMN_WIDTH, w.GetColumnWidth(i) + 5);
                    w.SetColumnsWidth(i, 1, (ushort)width);
                }

                w.SetSettings(WorksheetSettings.Edit_Readonly, true);

                _setCellRangeLnk.Enabled = true;
            });
        }