Skip to content

Instantly share code, notes, and snippets.

@andriybuday
Last active February 29, 2016 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andriybuday/1db8a26d807078f73c7a to your computer and use it in GitHub Desktop.
Save andriybuday/1db8a26d807078f73c7a to your computer and use it in GitHub Desktop.
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