コード例 #1
0
        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"));
            }
        }