Last active
February 29, 2016 11:14
-
-
Save andriybuday/1db8a26d807078f73c7a to your computer and use it in GitHub Desktop.
Using EPPlus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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