public ExcelResult Export() { const string fileName = "Reestr.xls"; // get data int dm = (int)Session["Month"]; int year = (int)Session["Year"]; int ContractID; string dog = GetCookie("Dog"); if (!String.IsNullOrWhiteSpace(dog)) { ContractID = Convert.ToInt32(dog); } else { ContractID = 0; } //Установили 1е число DateTime begDt = new DateTime(year, dm, 01, 0, 0, 0); //Установили 1е число следующего месяца DateTime endDt = begDt.AddMonths(1); TtnRepository repo = new TtnRepository(); List <Ttn> ttn = repo.GetTtn(Cust.CustId, begDt, endDt, ContractID); var items = repo.GetTtn(Cust.CustId, begDt, endDt, ContractID); var title = $"Реестр отрузки Абз-4 с {begDt.ToShortDateString()} по {endDt.ToShortDateString()}"; var html = ExportBuilder.Build(items, title); // prepare virtual file using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream)) { writer.Write(html); writer.Flush(); stream.Position = 0; } return(new ExcelResult(fileName, html)); }
/// <summary> /// Build Excel file /// </summary> /// <param name="items">product</param> /// <param name="title"></param> /// <returns></returns> /// public ActionResult Export() { var result = string.Empty; var wb = new Workbook(); // properties wb.Properties.Author = "ABZ"; wb.Properties.Created = DateTime.Today; wb.Properties.LastAutor = "ABZ"; wb.Properties.Version = "14"; // options sheets wb.ExcelWorkbook.ActiveSheet = 1; wb.ExcelWorkbook.DisplayInkNotes = false; wb.ExcelWorkbook.FirstVisibleSheet = 1; wb.ExcelWorkbook.ProtectStructure = false; wb.ExcelWorkbook.WindowHeight = 800; wb.ExcelWorkbook.WindowTopX = 0; wb.ExcelWorkbook.WindowTopY = 0; wb.ExcelWorkbook.WindowWidth = 600; // create style s1 for header var s1 = new Style("s1") { Font = new Font { Bold = true, Italic = true, Color = "#FF0000" } }; wb.AddStyle(s1); // create style s2 for header var s2 = new Style("s2") { Font = new Font { Bold = true, Italic = true, Size = 12, Color = "#0000FF" } }; wb.AddStyle(s2); var ws3 = new Worksheet("Реестр отпущенной продукции"); // Adding Headers ws3.AddCell(0, 0, "ТТН № ", 0); ws3.AddCell(0, 1, "Дата", 0); ws3.AddCell(0, 2, "Время", 0); ws3.AddCell(0, 3, "Продукция", 0); ws3.AddCell(0, 4, "Кол", 0); ws3.AddCell(0, 5, "Цена", 0); ws3.AddCell(0, 6, "Сумма", 0); ws3.AddCell(0, 7, "Модель", 0); ws3.AddCell(0, 8, "Автобаза", 0); ws3.AddCell(0, 9, "Гос. №", 0); ws3.AddCell(0, 10, "Водитель", 0); ws3.AddCell(0, 11, "Адрес", 0); // get data int dm = (int)Session["Month"]; int year = (int)Session["Year"]; //Установили 1е число DateTime begDt = new DateTime(year, dm, 01, 0, 0, 0); //Установили 1е число следующего месяца DateTime endDt = begDt.AddMonths(1); TtnRepository repo = new TtnRepository(); List <Ttn> ttn = repo.GetTtn(Cust.CustId, begDt, endDt); int totalRows = 0; // appending rows with data for (int i = 0; i < ttn.Count; i++) { ws3.AddCell(i + 1, 0, ttn[i].Num, 0); ws3.AddCell(i + 1, 1, ttn[i].Dat, 0); ws3.AddCell(i + 1, 2, ttn[i].Tm, 0); ws3.AddCell(i + 1, 3, ttn[i].Good, 0); ws3.AddCell(i + 1, 4, ttn[i].Kol, 0); ws3.AddCell(i + 1, 5, ttn[i].Price, 0); ws3.AddCell(i + 1, 6, ttn[i].Sm, 0); ws3.AddCell(i + 1, 7, ttn[i].Amodel, 0); ws3.AddCell(i + 1, 8, ttn[i].Ab, 0); ws3.AddCell(i + 1, 9, ttn[i].Gn, 0); ws3.AddCell(i + 1, 10, ttn[i].Driv, 0); ws3.AddCell(i + 1, 11, ttn[i].Adres, 0); totalRows++; } totalRows++; wb.AddWorksheet(ws3); // generate xml var workstring = wb.ExportToXML(); // Send to user file return(new ExcelResult("Reestr.xlsx", workstring)); }