Export Grid view to excel and save excel file to folder

前端 未结 7 787
Happy的楠姐
Happy的楠姐 2020-12-16 06:05

I want to save excel file which export data of grid view. I have written code to export gridview data to excel but I don\'t know how to save exported file.

Following

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 06:35

    first add EPPLUS reference library into application and add using OfficeOpenXml;

    //business object class

    class bocls {

        string name;
    
        public string NAME
        {
            get { return name; }
            set { name = value; }
        }
        string id;
    
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
    
    
    
        public bocls() { }
        public bocls(string name, string id)
        {
            this.name = name;
            this.id = id;         
    
        }
    

    //in export button click event

    protected void lbtnExport_Click(object sender, EventArgs e) {

                List list6 =  new List();
               //copy the grid view values into list
                list6 = (from row in dataGridView1.Rows.Cast()
                from cell in row.Cells.Cast()
                select new 
                {
                    //project into your new class from the row and cell vars.
                }).ToList();
        }
                ExcelPackage excel = new ExcelPackage();
                var workSheet = excel.Workbook.Worksheets.Add("Products");
                var totalCols = GridView1.Rows[0].Cells.Count;
                var totalRows = GridView1.Rows.Count;
                var headerRow = GridView1.HeaderRow;
                for (var i = 1; i <= totalCols; i++)
                {
                    workSheet.Cells[1, i].Value = headerRow.Cells[i - 1].Text;
                }
                for (var j = 1; j <= totalRows; j++)
                {
                    for (var i = 1; i <= totalCols; i++)
                    {
                        var item = list6.ElementAt(j - 1);
    
                        workSheet.Column(1).Width = 13;
                        workSheet.Column(2).Width = 10;
    
                        workSheet.Cells[j + 1, i].Style.WrapText = true;
    
                        if (headerRow.Cells[i - 1].Text == "ID")
                            workSheet.Cells[j + 1, i].Value = item.GetType().GetProperty("id").GetValue(item, null);
                        else if (headerRow.Cells[i - 1].Text == "NAME")
                            workSheet.Cells[j + 1, i].Value = item.GetType().GetProperty("name").GetValue(item, null);
    
                        workSheet.Cells[j + 1, i].Value = workSheet.Cells[j + 1, i].Value.ToString().Replace("
    ", ""); } } using (var memoryStream = new MemoryStream()) { Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; string filename = Guid.NewGuid().ToString() + ".xlsx"; Response.AddHeader("content-disposition", "attachment; filename=" + filename); excel.SaveAs(memoryStream); //add your destination folder FileStream fileStream = new FileStream(@"C:\Users\karthi\Downloads\New folder\" + filename, FileMode.Create,FileAccess.Write,FileShare.Write); memoryStream.WriteTo(fileStream); fileStream.Close(); memoryStream.WriteTo(Response.OutputStream); memoryStream.Close(); memoryStream.WriteTo(Response.OutputStream); Response.Flush(); Response.End(); } }

提交回复
热议问题