티스토리 뷰

🌈 .NET MVC

엑셀 다운로드

James Wetzel 2018. 7. 11. 11:44

        public ActionResult GetInventoryExcel(GetInventoryListModel getInventoryListModel)

        {

            // Validation

            if (getInventoryListModel.SearchStartDate == DateTime.MinValue) { getInventoryListModel.SearchStartDate = DateTime.Now.AddYears(-50); }

            if (getInventoryListModel.SearchEndDate == DateTime.MinValue) { getInventoryListModel.SearchEndDate = DateTime.Now; }


            getInventoryListModel.CustomerNo = LoginService.GetLoginInfo.cust_no;

            getInventoryListModel.PlaceCode = LoginService.GetLoginInfo.pl_cd;


            if ("inventoryNo".Equals(getInventoryListModel.searchOption)) { getInventoryListModel.InventoryCode = getInventoryListModel.searchOptionDetail; }

            if ("inventoryName".Equals(getInventoryListModel.searchOption)) { getInventoryListModel.InventoryName = getInventoryListModel.searchOptionDetail; }


            Response.Clear();

            Response.Buffer = true;

            Response.ContentType = "application/vnd.ms-excel";

            Response.AddHeader("content-disposition", "attachment; filename=Inventory"+DateTime.Now.ToShortDateString()+".xls");

            Response.ContentEncoding = System.Text.Encoding.UTF8;

            Response.Charset = "UTF-8";

            

            

            System.IO.StringWriter sw = new System.IO.StringWriter();

            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);


            this.InventoryBiz.GetGridViewForExcel(base.InventoryBiz.GetInventoryList(getInventoryListModel)).RenderControl(htw);

            Response.Write(@"<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /><style> TD { mso-number-format:\@; } </style>");


            Response.Output.Write(sw.ToString());

            Response.Flush();

            Response.End();

            

            return new EmptyResult();

        }


public GridView GetGridViewForExcel(DataTable dt)

        {

            var inventoryDT = new System.Data.DataTable("Inventory");

            inventoryDT.Columns.Add("Inventory No ", typeof(string));

            inventoryDT.Columns.Add("Inventory Name", typeof(string));

            inventoryDT.Columns.Add("Inventory Total Qty", typeof(int));

            inventoryDT.Columns.Add("Registration Date", typeof(System.DateTime));


            if (dt != null)

            {

                for (int i = 0; i < dt.Rows.Count; i++)

                {

                    inventoryDT.Rows.Add(dt.Rows[i]["inv_code"], dt.Rows[i]["inv_name"], dt.Rows[i]["inv_qty"], dt.Rows[i]["reg_dt"]);

                }

            }


            var gridView = new System.Web.UI.WebControls.GridView { AllowPaging = false, DataSource = inventoryDT };

            gridView.DataBind();


            return gridView;

        }



반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함