如何在EPPlus中的Excel工作表顶部设置标题标题 更新

问题描述

我可以使用EPPlus库导出excel。添加页眉和页脚后,只有在单击Ctrl + P时才能显示,这意味着仅在打印预览中。现在,我要在顶部行中设置标题,以使所有单元格都充满,然后编写列标题。因此,如何在工作表的第一行和第二列的表的表标题列中设置标题文本。

So here is the demo of the excel that I want to draw

 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","Amount"
                                }
                            };

解决方法

我不认为您要尝试做的事,EPPlus可能实现。 EPPlus仅提供excel本身支持的功能,而Excel本身不支持在编辑模式下查看页眉和页脚。这是关于页眉和页脚的documentation必须说的。这适用于Microsoft 365,Excel 2019,Excel 2016,Excel 2013,Excel 2010,Excel 2007,Excel Starter 2010的Excel。

页眉和页脚仅在“页面布局”视图,“打印预览”和打印的页面中显示。

此外,如果您查看EVENT_ID| USER_ID | RECORD_CREATED_DATE | COUNT(T2.Id) | _______________________________________________________| | 5f0172| 111 | 2020.07.13 | 1 | -> because tabl "T2" has 1 record less date 2020.07.13 | 5f0173| 222 | 2020.06.11 | 1 | | 5f0174| 111 | 2020.08.20 | 2 | 类的source,则没有规定可以处理“页面布局”视图中的内容或其他相关内容(您可以在工作表)。

更新

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

ExcelHeaderFooter

此外,可以将标题行添加到标题行下方,如下所示

// 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";

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...