Пример #1
0
        public HttpResponseMessage Respond(ApiController apiController, string sp, IDictionary <string, object> parameters,
                                           MediaTypeHeaderValue negotiatedMediaType, Encoding negotiatedEncoding)
        {
            HttpResponseMessage          csvResponse  = apiController.Request.CreateResponse();
            IDictionary <string, object> queryStrings = apiController.Request.GetQueryStringDictionary();
            MemoryStream memoryStream = new MemoryStream();             // TBD: To find a more efficient way later

            using (XLWorkbook workbook = new XLWorkbook())
                using (DalCenter dbContext = new DalCenter(queryStrings))
                {
                    IXLWorksheet currentWorksheet = null;

                    dbContext.ExecuteDbApi(sp, parameters, rs =>
                    {
                        currentWorksheet = workbook.AddWorksheet(string.Format("Sheet{0}", rs + 1));
                    },
                                           readHeader =>
                    {
                        if (currentWorksheet != null)
                        {
                            string[] fieldNames = dbContext.NameAllFields(readHeader);

                            for (int col = 0; col < fieldNames.Length; col++)
                            {
                                currentWorksheet.Cell(1, col + 1).SetValue(fieldNames[col]).Style.Font.Bold = true;
                            }
                        }
                    },
                                           readRow =>
                    {
                        if (currentWorksheet != null)
                        {
                            currentWorksheet.Cell(2, 1).Value = readRow;
                        }
                    },
                                           foot =>
                    {
                        if (currentWorksheet != null)
                        {
                            currentWorksheet.Columns().AdjustToContents();
                        }
                    }, null, null, true);

                    workbook.SaveAs(memoryStream);
                    memoryStream.Seek(0, SeekOrigin.Begin);
                }

            csvResponse.Content = new StreamContent(memoryStream);
            csvResponse.Content.Headers.ContentType        = negotiatedMediaType;
            csvResponse.Content.Headers.ContentLength      = memoryStream.Length;
            csvResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = queryStrings.GetQueryFileName(DbWebApiOptions.QueryStringContract.FileNameParameterName, FormatShortName)
            };

            return(csvResponse);
        }
Пример #2
0
        public HttpResponseMessage Respond(ApiController apiController, string sp, IDictionary <string, object> parameters,
                                           MediaTypeHeaderValue negotiatedMediaType, Encoding negotiatedEncoding)
        {
            HttpResponseMessage          csvResponse  = apiController.Request.CreateResponse();
            IDictionary <string, object> queryStrings = apiController.Request.GetQueryStringDictionary();

            int[] resultSetChoices = new int[] { GetQueryResultSetIndex(queryStrings, DbWebApiOptions.QueryStringContract.ResultSetParameterName) };

            csvResponse.Content = new PushStreamContent((stream, httpContent, transportContext) =>
            {
                StreamWriter textWriter = (negotiatedEncoding == null) ? new StreamWriter(stream) : new StreamWriter(stream, negotiatedEncoding);

                using (DalCenter dbContext = new DalCenter(queryStrings))
                {
                    CsvExporter csvExporter = new CsvExporter(textWriter);

                    dbContext.ExecuteDbApi(sp, parameters, null,
                                           readHeader =>
                    {
                        csvExporter.WriteHeader(dbContext.NameAllFields(readHeader));
                    },
                                           readRow =>
                    {
                        object[] values = new object[csvExporter.ColumnCount];

                        readRow.GetColumnValues(values);
                        csvExporter.WriteRow(values);
                    },
                                           null, null, resultSetChoices);

                    textWriter.Flush();
                }

                stream.Close();
            }, negotiatedMediaType ?? DefaultMediaType);

            csvResponse.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = queryStrings.GetQueryFileName(DbWebApiOptions.QueryStringContract.FileNameParameterName, FormatShortName)
            };

            return(csvResponse);
        }