使用EPPlus创建Excel并将其保存在服务器文件夹中

时间:2017-04-25 11:19:41

标签: c# asp.net excel epplus

我想使用EPPlus创建一个Excel文件并将其保存到服务器上的文件夹中。

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());
            }

此代码下载文件但我想将其保存在服务器文件夹中。我从帖子中找到了一些代码,但这不起作用

2 个答案:

答案 0 :(得分:2)

你可以使用......

pck.SaveAs(New FileInfo(ServerFilePath))

但您不能再使用此对象了。所以你必须将保存的文件导出到客户端。

答案 1 :(得分:0)

将代码中的File.WriteAllText()电话替换为:

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