我使用ExportToXlsx()方法,以便从GridView导出数据到Excel工作表。这似乎可以很好地导出和格式化数据,甚至我的条件格式也可以原样导出。我想知道的唯一一件事是,我如何添加一个标志和标题在excel文件,而不必手动添加它。就像在Crystal Report中,我们可以在设计时添加这些东西。有什么可以做的吗?
您可以对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);
}
只是要确保你保持坐标和大小正确,这样图像和文本就不会重叠。