Пример #1
0
        public IDictionary <string, Stream> Export(IBinnerDb db)
        {
            const string delimiter = ",";
            const string lineBreak = "\r\n";
            var          streams   = new Dictionary <string, Stream>();
            var          builder   = new DataSetBuilder();
            var          dataSet   = builder.Build(db);

            foreach (DataTable dataTable in dataSet.Tables)
            {
                var stream = new MemoryStream();
                using (var writer = new StreamWriter(stream, Encoding.UTF8, 4096, true))
                {
                    foreach (DataRow row in dataTable.Rows)
                    {
                        var rowValues = new List <string>();
                        foreach (DataColumn col in dataTable.Columns)
                        {
                            rowValues.Add($"{EscapeValue(row[col], col.DataType)}");
                        }
                        writer.Write($"{string.Join(delimiter, rowValues)}{lineBreak}");
                    }
                }
                streams.Add(dataTable.TableName, stream);
            }
            return(streams);
        }
Пример #2
0
        public IDictionary <StreamName, Stream> Export(IBinnerDb db)
        {
            var streams = new Dictionary <StreamName, Stream>();
            var builder = new DataSetBuilder();
            var dataSet = builder.Build(db);

            var       stream   = new MemoryStream();
            IWorkbook workbook = new XSSFWorkbook();
            var       styles   = CreateStyles(workbook);

            foreach (DataTable table in dataSet.Tables)
            {
                ISheet sheet = workbook.CreateSheet(table.TableName);
                sheet.DefaultColumnWidth = 120;

                // insert header row
                var  rowIndex  = 0;
                IRow headerRow = sheet.CreateRow(rowIndex);
                rowIndex++;
                var columnIndex = 0;
                foreach (DataColumn dataColumn in table.Columns)
                {
                    var c = headerRow.CreateCell(columnIndex);
                    c.SetCellValue(dataColumn.ColumnName);
                    c.CellStyle = styles["header"];
                    columnIndex++;
                }
                // insert all the row data for table
                foreach (DataRow dataRow in table.Rows)
                {
                    IRow row = sheet.CreateRow(rowIndex);
                    rowIndex++;
                    columnIndex = 0;
                    foreach (var value in dataRow.ItemArray)
                    {
                        SetCellValue(row.CreateCell(columnIndex), value, styles);
                        columnIndex++;
                    }
                }
                for (var i = 0; i < table.Columns.Count; i++)
                {
                    sheet.AutoSizeColumn(i);
                }
            }
            workbook.Write(stream);
            var bytes = stream.ToArray();

            stream.Dispose();
            streams.Add(new StreamName("BinnerParts", "xlsx"), new MemoryStream(bytes, false));
            return(streams);
        }
Пример #3
0
        public IDictionary <string, Stream> Export(IBinnerDb db)
        {
            var streams = new Dictionary <string, Stream>();
            var builder = new DataSetBuilder();
            var dataSet = builder.Build(db);

            var stream = new MemoryStream();

            using (var writer = new StreamWriter(stream, Encoding.UTF8, 4096, true))
            {
                DataSetHelper.CreateWorkbook(stream, dataSet);
            }
            streams.Add("BinnerParts", stream);
            return(streams);
        }