【问题标题】:Create Excel using EPPlus and save it on server folder使用 EPPlus 创建 Excel 并将其保存在服务器文件夹中
【发布时间】:2017-04-25 11:19:41
【问题描述】:

我想使用 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());
            }

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

【问题讨论】:

  • 你试过写 Server.MapPath("~/files/12345" ...)。我指的是this,其中波浪号在服务端被替换。
  • 已经在使用这个

标签: c# asp.net excel epplus


【解决方案1】:

你可以使用...

pck.SaveAs(New FileInfo(ServerFilePath))

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

【讨论】:

    【解决方案2】:

    将代码中的 File.WriteAllText() 调用替换为:

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

    【讨论】:

      猜你喜欢
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多