【问题标题】:How to set Header Title in top of the excel sheet in EPPlus如何在 EPPlus 中的 Excel 工作表顶部设置标题标题
【发布时间】:2020-12-18 08:16:33
【问题描述】:

我可以使用 EPPlus 库导出 excel。当我添加页眉和页脚时,它只能在我单击 Ctrl + P 时显示,我的意思是仅在打印预览中。现在我想用marge所有单元格在第一行设置标题,然后写列标题。那么如何在第二个列表中的表格的第一行和表格的列标题中设置标题文本。

 using (ExcelPackage excel = new ExcelPackage())
                    {
                        var sheet = excel.Workbook.Worksheets.Add("Worksheet1");
    
                        var headerRow = new List<string[]>()
                        {
                            new string[]
                            {
                                "Transaction Id", "Date", "Time", "Id", "Name", "Amount"
                            }
                        };
    
                        string headerRange = "A1:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "1";
                        // Target a worksheet
                        var worksheet = excel.Workbook.Worksheets["Worksheet1"];   
                       // Popular header row data
                        worksheet.Cells[headerRange].LoadFromArrays(headerRow);    
                        var totalNoOfRows = depositList.Count() + 1;
    
                        //ExcelWorksheet ws = worksheet.Workbook.Worksheets.Add("Demo");
                        //ws.Cells["A1:G1"].Merge = true;
    
                        // Header Text Setup 
                        var header = sheet.HeaderFooter.OddHeader;
                        header.CenteredText = "&18&U&\"Times New Roman,Regular Bold\"&14& " + ClientName + " \n  Report \n";
                        worksheet.PrinterSettings.TopMargin = 1;
                        // Footer Text Setup 
                        ExcelHeaderFooterText footer = sheet.HeaderFooter.OddFooter;
                        header.RightAlignedText = "&10&P of &N";
                        footer.LeftAlignedText = "&16&\"Aril, Bold\"Download Date and Time " + DateTime.Now;            
   
    
                        using (MemoryStream stream = new MemoryStream())
                        {
                            excel.SaveAs(stream);
                            return File(stream.ToArray(),
                                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                                "Report(" + ClientName + ").xlsx");
                        }
                    }

我已经添加了图像,但我很困惑将表头行设置在第二行。

ExcelWorksheet ws = worksheet.Workbook.Worksheets.Add("Demo");
ws.Cells["A1:J1"].Merge = true;
var headerRow = new List<string[]>()
                            {
                                new string[]
                                {
                                    "Transaction Id", "Date", "Time", "Id", "Name", "Amount"
                                }
                            };

【问题讨论】:

    标签: c# asp.net-core epplus


    【解决方案1】:

    我不认为你想要做的是 EPPlus 的可能性。 EPPlus 仅提供 excel 本身支持的功能,Excel 本身不支持在编辑模式下查看页眉和页脚。这就是关于页眉和页脚的documentation 对此的看法。这适用于 Excel for Microsoft 365、Excel 2019、Excel 2016、Excel 2013、Excel 2010、Excel 2007、Excel Starter 2010。

    页眉和页脚仅显示在页面布局视图、打印预览和打印页面上。

    此外,如果您查看ExcelHeaderFooter 类的source,则没有任何规定可以处理在页面布局视图中查看或任何相关内容(尽管您可以在工作表上设置打印机设置)。

    更新

    您可以使用以下代码行在第一行容纳标题

    // This is similar to the snippet that you have added.
    // You can specify your custom range, content and required styling.
    sheet.Cells["A1:J1"].Merge = true;
    sheet.Cells["A1"].Value = "Title";
    

    另外,可以在标题行下方添加标题行,如下所示

    // Notice here, this is similar to what you were trying. However, we have A2
    // as the beginning of our headerRange and not A1. Similarly, notice the modification
    // done to the upper bound so as to have a valid excel range.
    // You can play around with the range here. But key takeaway is the second row we are dealing with
    string headerRange = "A2:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "2";
    

    【讨论】:

    • 谢谢。我已经更新了我的问题并添加了一张图片来显示我正在尝试做的事情。你能帮我在第一行设置文本标题并在第二行设置列标题吗?
    • @Antorajahan - 我已经更新了答案以包含有关如何在工作表顶部(第一行)具有标题行然后在其下方填充标题行的信息。希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 2023-03-25
    • 2021-04-13
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多