简体   繁体   中英

Create Excel using EPPlus and save it on server folder

I want to create an Excel file using EPPlus and save it to a folder on server.

DataTable dt_final = get_gridview_format();
            for (int i = 0; i < gridreport.Rows.Count; i++)
            {
                string[] temp = new string[13];
                for (int j = 0; j < gridreport.Columns.Count; j++)
                {
                    if (j != 12 && j != 13)
                    {
                        int index = j;
                        if (j > 13) index = j - 2;

                        if (j <= 11)
                        {
                            Label lbl = (Label)gridreport.Rows[i].Cells[j].FindControl("lbl" + j);
                            temp[index] = lbl.Text.Replace("->", "").Replace("<br/>", "");
                        }
                        else
                        {
                            TextBox txt = (TextBox)gridreport.Rows[i].Cells[j].FindControl("txtfinal");
                            temp[index] = txt.Text.Replace("->", "").Replace("<br/>", "");
                        }
                    }
                }
                dt_final.Rows.Add(temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8], temp[9], temp[10], temp[11], temp[12]);
            }

            DataTable tbl = dt_final.Copy();

            DataView dv = tbl.DefaultView;
            dv.Sort = "SrNo ASC";
            tbl = dv.ToTable();

            using (ExcelPackage pck = new ExcelPackage())
            {
                //Create the worksheet
                ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Updation");

                //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
                ws.Cells["A1"].LoadFromDataTable(tbl, true);

                //Format the header for column 1-3
                using (ExcelRange rng = ws.Cells["A1:K1"])
                {
                    rng.Style.Font.Bold = true;
                    rng.Style.Fill.PatternType = ExcelFillStyle.Solid;                      //Set Pattern for the background to Solid
                    rng.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.FromArgb(79, 129, 189));  //Set color to dark blue
                    rng.Style.Font.Color.SetColor(System.Drawing.Color.White);
                }

                //Example how to Format Column 1 as numeric 
                using (ExcelRange col = ws.Cells[2, 2, 2 + tbl.Rows.Count, 2])
                {
                    col.Style.Numberformat.Format = "#,##0.00";
                    col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
                }
File.WriteAllText(Server.MapPath("files/12345"+txtfilename.Text+".xlsx"), pck.ToString()); // for save file on server but this is not working


                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;  filename=Qa_Report.xlsx");
                Response.BinaryWrite(pck.GetAsByteArray());
            }

this code download the file but i want to save this also on server folder. i found some code from a post but this is not working

You can use...

pck.SaveAs(New FileInfo(ServerFilePath))

but you can't use this object anymore. So you have to export saved file to client side.

Replace the File.WriteAllText() call in your code with:

File.WriteAllBytes(
    Server.MapPath("files/12345"+txtfilename.Text+".xlsx"), 
    pck.GetAsByteArray()
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM