DevExpress GridView导出Excel表添加自定义标题和徽标

本文关键字:标题 自定义 添加 GridView 导出 Excel DevExpress | 更新日期: 2023-09-27 18:04:58

我使用ExportToXlsx()方法,以便从GridView导出数据到Excel工作表。这似乎可以很好地导出和格式化数据,甚至我的条件格式也可以原样导出。我想知道的唯一一件事是,我如何添加一个标志和标题在excel文件,而不必手动添加它。就像在Crystal Report中,我们可以在设计时添加这些东西。有什么可以做的吗?

DevExpress GridView导出Excel表添加自定义标题和徽标

您可以对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);
}

除了添加了标题的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);
}

只是要确保你保持坐标和大小正确,这样图像和文本就不会重叠。