简体   繁体   English

如何在 EPPlus 中的 Excel 表格顶部设置标题标题

[英]How to set Header Title in top of the excel sheet in EPPlus

I can export excel using EPPlus library.我可以使用 EPPlus 库导出 excel。 When I have added the Header and Footer it can only show when I click the Ctrl + P, I mean only in print preview.当我添加了页眉和页脚时,它只能在我单击 Ctrl + P 时显示,我的意思是仅在打印预览中。 Now I want to set the title in the top row with marge the all cell and then write the column header.现在我想用所有单元格在顶行设置标题,然后写列标题。 So how can I set the title text in the 1st row of the sheet and column Header of the table in the secound list.那么如何在第二个列表中的工作表的第一行和表格的列标题中设置标题文本。 所以这里是我要画的excel的demo

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

I have added the the image and I am confused to set the Table Header row in the 2nd row.我已经添加了图像,但我很困惑在第二行中设置表格标题行。

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

I do not think what you are trying to do, is a possibility with EPPlus.我不认为你想要做的事情是 EPPlus 的可能性。 EPPlus provides only that functionality that excel supports by itself and Excel itself does not support viewing the header and footer in edit mode. EPPlus 仅提供 excel 本身支持的功能,Excel 本身不支持在编辑模式下查看页眉和页脚。 This is what the documentation about Headers and footers has to say regarding this.这就是有关页眉和页脚的文档对此的说明。 This is applicable to Excel for Microsoft 365, Excel 2019, Excel 2016, Excel 2013, Excel 2010, Excel 2007, Excel Starter 2010.这适用于 Excel for Microsoft 365、Excel 2019、Excel 2016、Excel 2013、Excel 2010、Excel 2007、Excel Starter 2010。

Headers and footers are displayed only in Page Layout view, Print Preview, and on printed pages.页眉和页脚仅在页面布局视图、打印预览和打印页面上显示。

Further if you take a look at the source of the ExcelHeaderFooter class, there is no provision that deals with viewing in Page Layout view or anything related (you can set have printer settings set on the worksheet though).此外,如果您查看ExcelHeaderFooter类的源代码,则没有处理在页面布局视图或任何相关内容中查看的规定(尽管您可以在工作表上设置打印机设置)。

Update更新

You can accommodate the Title in the first row with the below lines of code您可以使用以下代码行容纳第一行中的标题

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

Further, the header row can be added below the title row as shown below此外,标题行可以添加在标题行下方,如下所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM