public ActionResult DownloadCategoryDbf() { try { DataTable dtCategory = new DataTable("CT_CATEGORY"); DataColumn dcCode = dtCategory.Columns.Add("CODE", typeof(string)); dcCode.MaxLength = 100; DataColumn dcName = dtCategory.Columns.Add("NAME", typeof(string)); dcName.MaxLength = 250; DataColumn dcOrd = dtCategory.Columns.Add("ORD", typeof(int)); dcOrd.Caption = "[25]"; var categories = (from ct in repository.Categories select new { Code = ct.Code, Name = ct.Name, Ord = ct.Ord ?? int.MaxValue }).ToList(); foreach (var category in categories) { DataRow row = dtCategory.NewRow(); row.SetField <string>("CODE", category.Code); row.SetField <string>("NAME", category.Name); row.SetField <int>("ORD", category.Ord); dtCategory.Rows.Add(row); } var ms = new MemoryStream(); DbfFile.DataTableToDbf(dtCategory, ms); var stream = new StreamWithName() { Stream = ms, FileName = "Category.dbf" }; return(File(stream.Stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Soap, stream.FileName)); } catch { return(RedirectToAction("Categories")); } }
public ActionResult DownloadCategoryXls() { try { var workbook = new HSSFWorkbook(); var sheetInfo = workbook.CreateSheet("Лист"); // Заголовки var rowIndex = 0; var row = sheetInfo.CreateRow(rowIndex); row.CreateCell(0).SetCellValue("CODE"); row.CreateCell(1).SetCellValue("NAME"); row.CreateCell(2).SetCellValue("ORD"); rowIndex++; var format = workbook.CreateDataFormat(); // Данные foreach (var item in repository.Categories.OrderBy(x => x.CategoryId)) { row = sheetInfo.CreateRow(rowIndex); row.CreateCell(0).SetCellValue(item.Code); row.CreateCell(1).SetCellValue(item.Name); row.CreateCell(2).SetCellValue((item.Ord ?? int.MaxValue).ToString()); rowIndex++; } var ms = new MemoryStream(); workbook.Write(ms); var stream = new StreamWithName() { Stream = ms, FileName = "Category.xls" }; return(File(stream.Stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Soap, stream.FileName)); } catch { return(RedirectToAction("Categories")); } }
public ActionResult DownloadCategoryXml() { try { var obj = new Models.Category.packet(); obj.hdr = new Models.Category.packetHdr(); obj.hdr.type = "Category"; obj.hdr.version = 1.1m; obj.hdr.date = DateTime.Now.Date.ToString("yyyy.MM.dd"); obj.rec = new Models.Category.packetRec[repository.Categories.Count()]; var rowIndex = 0; foreach (var item in repository.Categories.OrderBy(x => x.CategoryId)) { obj.rec[rowIndex] = new Models.Category.packetRec(); obj.rec[rowIndex].Code = item.Code; obj.rec[rowIndex].Name = item.Name; obj.rec[rowIndex].Ord = item.Ord ?? int.MaxValue; rowIndex++; } var xs = new XmlSerializer(typeof(Models.Category.packet)); var ms = new MemoryStream(); var sw = new StreamWriter(ms, System.Text.Encoding.UTF8); var xn = new XmlSerializerNamespaces(); xn.Add("", ""); xs.Serialize(sw, obj, xn); sw.Close(); var stream = new StreamWithName() { Stream = ms, FileName = "Category.xml" }; return(File(stream.Stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Soap, stream.FileName)); } catch { return(RedirectToAction("Categories")); } }