【问题标题】:DevExpress GridView Exported Excel Sheet Adding Custom Heading and LogoDevExpress GridView 导出的 Excel 工作表添加自定义标题和徽标
【发布时间】:2014-05-26 22:18:54
【问题描述】:

我正在使用 ExportToXlsx() 方法将数据从 GridView 导出到 Excel 工作表。 这可以很好地导出和格式化数据,甚至我的条件格式也按原样导出。 我唯一想知道的是,如何在该 excel 文件中添加徽标和标题,而无需手动添加。就像在水晶报表中一样,我们可以在设计时添加这些东西。有什么可以做的吗?

【问题讨论】:

    标签: c# excel gridview crystal-reports devexpress


    【解决方案1】:

    您可以使用PrintableComponentLink 来做到这一点并编写一个CreateReportHeaderArea 事件处理程序:

    private void button_Export_Click(object sender, EventArgs e)
    {
    
            var y = new DevExpress.XtraPrinting.PrintableComponentLink(new PrintingSystem());
            y.Component = gridControl1;
            y.CreateReportHeaderArea += y_CreateReportHeaderArea;
    
            if (saveFileDialog_Report.ShowDialog() == DialogResult.OK)
            {
                if (saveFileDialog_Report.FileName.ToLower().EndsWith("xlsx"))
                {
                    y.ExportToXlsx(saveFileDialog_Report.FileName);
                }               
            }   
    }
    
    
    void y_CreateReportHeaderArea(object sender, CreateAreaEventArgs e)
    {
            DevExpress.XtraPrinting.TextBrick brick;
    
            brick = e.Graph.DrawString("My Report Title Here", Color.Navy, new RectangleF(0, 0, 500, 40), DevExpress.XtraPrinting.BorderSide.None);
    
            brick.Font = new Font("Arial", 16);
    
            brick.StringFormat = new DevExpress.XtraPrinting.BrickStringFormat(StringAlignment.Center);
    
    }
    

    【讨论】:

      【解决方案2】:

      除了添加标题的 Milen 的回答之外,我还可以向报告中添加图像,如下所示

          void y_CreateReportHeaderArea(object sender, CreateAreaEventArgs e)
      {
       Image newimage = Image.FromFile("path");
      
              DevExpress.XtraPrinting.ImageBrick iBrick;
              iBrick = e.Graph.DrawImage(newimage, new RectangleF(0,0,40,40));
      
      DevExpress.XtraPrinting.TextBrick brick;
      
              brick = e.Graph.DrawString("My Report Title Here", Color.Navy, new RectangleF(40, 0, 500, 40), DevExpress.XtraPrinting.BorderSide.None);
      
              brick.Font = new Font("Arial", 16);
      
              brick.StringFormat = new DevExpress.XtraPrinting.BrickStringFormat(StringAlignment.Center);
      
      }
      

      只需确保保持坐标和大小正确,以免图像和文本重叠。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多