Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. // Dto class for object mapping to excel template
  2. class ReportDto
  3. {
  4. public string ParentItem { get; set; }
  5. public string Description { get; set; }
  6. public string Branch { get; set; }
  7. public IEnumerable<Record> Records { get; set; }
  8. }
  9.  
  10. class Record
  11. {
  12. public string ComponentItem { get; set; }
  13. public string Serial { get; set; }
  14. public string VendorName { get; set; }
  15. public string ProductName { get; set; }
  16. public string Location { get; set; }
  17. }
  18.  
  19. // service class for writing excel document
  20. public class ExcelServiceWriter
  21. {
  22. private Service1 _srv1;
  23. // dependency inject from container
  24. public ExcelServiceWriter(Service1 srv1)
  25. {
  26. _srv1 = srv1;
  27. //....
  28. }
  29.  
  30. // generate excel document via template
  31. // template path might be: \...\folder1\folder2\template.xlsx
  32. public void CreateExcelViaTemplate(IEnumerable<ReportDto> sheets, string saveAsPath, string templatePath)
  33. {
  34. foreach (var sheet in sheets)
  35. {
  36. using (var template = new XLTemplate(templatePath))
  37. {
  38. template.AddVariable(sheet);
  39. template.Generate();
  40. template.Workbook.Worksheet(1).Rows().AdjustToContents();
  41. template.SaveAs(saveAsPath);
  42. }
  43. }
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement