public void Export(int id) { var products = _productService.GetProducts(id); var category = _shopService.GetCategory(id); if (category == null) { return; } var workbook = new Workbook(); var worksheet = new Worksheet("First Sheet"); for (var k = 0; k < 200; k++) { worksheet.Cells[k, 0] = new Cell(null); } worksheet.Cells[0, 0] = new Cell(category.ID.ToString(CultureInfo.InvariantCulture)); worksheet.Cells[1, 0] = new Cell("ID"); worksheet.Cells[1, 1] = new Cell("ReviewLink"); worksheet.Cells[1, 2] = new Cell("Title"); worksheet.Cells[1, 3] = new Cell("SmallDescription"); worksheet.Cells[1, 4] = new Cell("Description"); worksheet.Cells[1, 5] = new Cell("IsActive"); worksheet.Cells[1, 6] = new Cell("Производитель"); var sections = category.CategoryParametersSection.OrderBy(w => w.Name); var q = 7; foreach (var section in sections) { foreach (var p in section.CategoryParameter.OrderBy(w => w.Name)) { worksheet.Cells[0, q] = new Cell(p.ID); worksheet.Cells[1, q] = new Cell(p.Name); q++; } } var i = 2; foreach (var product in products) { var tempParameters = _productService.GetProductParameters(category.ID, product.ID); var j = 0; foreach (var section in tempParameters.OrderBy(w => w.Key.Name)) { foreach (var o in section.OrderBy(w => w.Parameter.Name)) { if (o.ParameterValue != null) { worksheet.Cells[i, j + 7] = new Cell(o.ParameterValue.Value); } j++; } } worksheet.Cells[i, 0] = new Cell(product.ID.ToString(CultureInfo.InvariantCulture)); worksheet.Cells[i, 1] = new Cell(product.ReviewLink); worksheet.Cells[i, 2] = new Cell(product.Title); worksheet.Cells[i, 3] = new Cell(product.SmallDescription); worksheet.Cells[i, 4] = new Cell(product.Description); worksheet.Cells[i, 5] = new Cell(product.IsActive ? "1" : "0"); worksheet.Cells[i, 6] = new Cell(product.ManufacturerID); i++; } //worksheet.Cells[0, 1] = new Cell((short)1); //worksheet.Cells[2, 0] = new Cell(9999999); //worksheet.Cells[3, 3] = new Cell((decimal)3.45); //worksheet.Cells[2, 2] = new Cell("Text string"); //worksheet.Cells[2, 4] = new Cell("Second string"); //worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00"); //worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY\-MM\-DD"); worksheet.Cells.ColumnWidth[1] = 4000; workbook.Worksheets.Add(worksheet); var stream = new MemoryStream(); workbook.SaveToStream(stream); stream.Position = 0; Response.Clear(); Response.ContentType = "application/force-download"; Response.AddHeader("content-disposition", String.Format("attachment; filename={0}_products.xls", category.Name.Replace(" ", "_"))); Response.BinaryWrite(stream.ToArray()); Response.End(); }
public void ExportProduct(int categoryId, int shopID, ImportType type) { if (categoryId == 0) { return; } var category = _shopService.GetCategory(categoryId); string fileName = ""; IEnumerable <ExportProductForShop> products = null; switch (type) { case ImportType.All: fileName = "all"; products = _productService.GetProductsForExportByAll(shopID); break; case ImportType.Binded: fileName = "all_binded"; products = _productService.GetProductsForExportByBinded(shopID); break; case ImportType.Category: fileName = category.Name.Replace(" ", "_").Replace("-", "_"); products = _productService.GetProductsForExportByCategory(categoryId, shopID); break; } var workbook = new Workbook(); var worksheet = new Worksheet("First Sheet"); for (var k = 0; k < 200; k++) { worksheet.Cells[k, 0] = new Cell(null); } worksheet.Cells[0, 0] = new Cell("Category"); worksheet.Cells[0, 1] = new Cell("Manufacturer"); worksheet.Cells[0, 2] = new Cell("Product"); worksheet.Cells[0, 3] = new Cell("Is in stock"); worksheet.Cells[0, 4] = new Cell("Price"); worksheet.Cells[0, 8] = new Cell("Доступные статусы товара"); var statuses = _productService.GetShopProductStatus(); var z = 1; foreach (var status in statuses) { worksheet.Cells[z, 8] = new Cell(status.Name); z++; } if (products.Any()) { var i = 1; foreach (var product in products) { worksheet.Cells[i, 0] = new Cell(product.Product.Category.Name); worksheet.Cells[i, 1] = new Cell(product.Product.Manufacturer.Name); worksheet.Cells[i, 2] = new Cell(product.Product.Title); if (product.ShopProduct != null) { worksheet.Cells[i, 3] = new Cell(product.ShopProduct.ShopProductStatus.Name); worksheet.Cells[i, 4] = new Cell(product.ShopProduct.Price); } i++; } } worksheet.Cells.ColumnWidth[0] = 7000; worksheet.Cells.ColumnWidth[1] = 5000; worksheet.Cells.ColumnWidth[2] = 10000; worksheet.Cells.ColumnWidth[3] = 4000; worksheet.Cells.ColumnWidth[8] = 7000; workbook.Worksheets.Add(worksheet); var stream = new MemoryStream(); workbook.SaveToStream(stream); stream.Position = 0; Response.Clear(); Response.ContentType = "application/force-download"; Response.AddHeader("content-disposition", String.Format("attachment; filename={0}_products.xls", fileName)); Response.BinaryWrite(stream.ToArray()); Response.End(); }