This way of doing things needs love if we are going to have a header. In fact, the approach radically changes. I have had an example working, but it yet needs spruce up. The code I offer below is really dirty code but I've been sitting on this for a few days I and I wanted to share it instead of maybe getting to it later. Most of it is stolen from elsewhere, but I don't recall where.
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrintingLinks;
using Modernity.Models;
namespace Modernity.Controllers
{
public class ExportingController : Controller
{
private System.Drawing.Image headerImage;
public ActionResult Export()
{
var gridViewSettings = new GridViewSettings();
gridViewSettings.Name = "whatever";
gridViewSettings.KeyFieldName = "CustomerID";
gridViewSettings.Columns.Add("ContactName");
gridViewSettings.Columns.Add("CompanyName");
gridViewSettings.Columns.Add("ContactTitle");
gridViewSettings.Columns.Add("City");
gridViewSettings.Columns.Add("Phone");
var printable = GridViewExtension.CreatePrintableObject(gridViewSettings,
NorthwindDataProvider.GetCustomers());
PrintingSystem ps = new PrintingSystem();
using (this.headerImage = Image.FromFile(Server.MapPath("~\\Content\\pic.png")))
{
Link header = new Link();
header.CreateDetailArea += new
CreateAreaEventHandler(header_CreateDetailArea);
PrintableComponentLink link1 = new PrintableComponentLink(ps);
link1.Component = printable;
CompositeLink compositeLink = new CompositeLink(ps);
compositeLink.Links.AddRange(new object[] { header, link1 });
compositeLink.CreateDocument();
using (MemoryStream stream = new MemoryStream())
{
compositeLink.PrintingSystem.ExportToXls(stream);
WriteToResponse("filename", true, "xls", stream);
}
ps.Dispose();
}
return PartialView("View");
}
void WriteToResponse(string fileName, bool saveAsFile, string fileFormat,
MemoryStream stream)
{
string disposition = saveAsFile ? "attachment" : "inline";
Response.Clear();
Response.Buffer = false;
Response.AppendHeader("Content-Type", string.Format("application/{0}",
fileFormat));
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Response.AppendHeader("Content-Disposition", string.Format("{0}; filename={1}.
{2}", disposition, fileName, fileFormat));
Response.BinaryWrite(stream.GetBuffer());
Response.End();
}
void header_CreateDetailArea(object sender, CreateAreaEventArgs e)
{
e.Graph.BorderWidth = 0;
Rectangle r = new Rectangle(0, 0, headerImage.Width, headerImage.Height);
e.Graph.DrawImage(headerImage, r);
r = new Rectangle(0, headerImage.Height, 400, 50);
e.Graph.DrawString("Additional Header information here....", r);
}
}
}
No comments:
Post a Comment