public FileResult Export() { QLLaptopEntities entities = new QLLaptopEntities(); DataTable dt = new DataTable("Grid"); dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Mã Đơn Hàng"), new DataColumn("Tên Khách hàng"), new DataColumn("Ngày đặt hàng"), new DataColumn("giá đơn hàng"), new DataColumn("giao") }); var customers = from customer in entities.DON_DAT_HANG.Take(10) select customer; foreach (var customer in customers) { dt.Rows.Add(customer.MaDH, customer.TenKH, customer.NgayDatHang, customer.TriGiaDH, customer.Dagiao); } using (XLWorkbook wb = new XLWorkbook()) { wb.Worksheets.Add(dt); using (MemoryStream stream = new MemoryStream()) { wb.SaveAs(stream); return(File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Grid.xlsx")); } } }