c# - 如何生成Excel epplus C#新表,每张表内容不同

标签 c# asp.net-mvc excel web epplus

我想在我的网站上用 Epplus C# 生成 excel。 我想在 Excel 中创建多张工作表,每张工作表上都有不同的内容。 我想也许我必须使用循环 For。 但我不知道如何编码。 请帮我。抱歉我的英语不好。

    using (ExcelPackage pkg = new ExcelPackage())
        {
            var fullFilename = "warming up specification.xlsx";
            var oldFilePath = Server.MapPath("~/File" + "/" + fullFilename);

            using (FileStream stream = new FileStream(oldFilePath, FileMode.Open))
            {


                pkg.Load(stream);
                ExcelWorksheet ws = pkg.Workbook.Worksheets[1];


                //set Fixed Cell value
                foreach (var item in dict)
                {
                    ws.Cells[item.Key].Value = item.Value; 
                }
                ws.Cells.Style.Font.Size = 13;
                ws.Cells.Style.WrapText = true;
                ws.PrinterSettings.PaperSize = ePaperSize.A4;

                ws.PrinterSettings.FitToPage = true;
                ws.PrinterSettings.FooterMargin = 0M;
                ws.PrinterSettings.TopMargin = 0M;
                ws.PrinterSettings.LeftMargin = 0M;
                ws.PrinterSettings.RightMargin = 0M;

                ws.Cells["A1:AC1"].Merge = true;
                ws.Cells["A1:AC1"].Style.Font.Bold = true;
                ws.Cells["A1:AC1"].Style.Font.Size = 25;
                ws.Cells["A1:AC1"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                ws.Cells["A1:AC1"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
                ws.Cells["A1:AC1"].Value = "TEST";
                // ws.Cells["F1:L1"].Style.Border.Bottom.Style = ws.Cells["F1:L1"].Style.Border.Left.Style = ws.Cells["F1:L1"].Style.Border.Right.Style = ws.Cells["F1:L1"].Style.Border.Top.Style = ExcelBorderStyle.Thin;

                Random rand = new Random();
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;  filename=" + DateTime.Now.ToString("yyyyMMdd") + "_" + rand.Next(1, 999) + "_" + fullFilename);
                Response.BinaryWrite(pkg.GetAsByteArray());
                Response.End();
        }

最佳答案

如果您要查找的是同一文件中的多个工作表,则只需添加另一个工作表即可。

ExcelWorksheet ws1 = pkg.Workbook.Worksheets.Add("WorkSheet1");
//your operations for worksheet1

ExcelWorksheet ws2 = pkg.Workbook.Worksheets.Add("WorkSheet2");
//your operations for worksheet2

关于c# - 如何生成Excel epplus C#新表,每张表内容不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50732542/

相关文章:

c# - 将字符串数据插入 blob

c# - 从 ListView 中检索数据

c# - string.Remove 不起作用

asp.net-mvc - Entity Framework 6.2 首次启动非常慢并且 EFInteractiveViews

excel - 如何引用 Excel 命名范围内的单元格?

vba - 从 Outlook 下载附件并在 Excel 中打开

c# - .Net PropertyGrid DropDownList - 返回值不同于显示值

c# - 从 MVC Controller 返回一个字符串到 jQuery

mysql - 如何连接mvc和mysql?

java - Apache Poi - 现有工作簿中存在错误的单元格中的值不会重新计算