Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Using EPPlus
using (var excelPackage = new ExcelPackage(newFileInfo))
{
var ws = excelPackage.Workbook.Worksheets["Work Sheet Name"];
// do the stuff, here are some examples
var cellRange = ws.Cells[string.Format("{0}{1}:{0}{2}", "A", 2, 4)];
// merging cells
cellRange.Merge = true;
// setting value
cellRange.Value = "Any object that serves as cell value";
// alignment
cellRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
cellRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
// setting color
cellRange.Style.Font.Color.SetColor(System.Drawing.Color.DarkRed);
// height for a row
ws.Row(5).Height = 50;
excelPackage.Save();
}
[HttpGet]
[Auth(Roles = "SomeRole")]
public HttpResponseMessage ExelFileDownload(Guid documentId)
{
using (var stream = new MemoryStream())
{
// get template stream
using (var templateStream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("TemplateResource.xlsx"))
{
using (var excelPackage = new ExcelPackage(stream, templateStream))
{
// do stuff
excelPackage.Save();
}
}
// content as array of bytes
var content = stream.ToArray();
// create response
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(content);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/msexcel");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "SomeNameOfYourFile.xlsx"
};
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment