public IActionResult Print(int[] toPrint) { XWPFDocument doc; using (Stream fileStream = System.IO.File.OpenRead("wwwroot\\template.docx")) { doc = new XWPFDocument(fileStream); fileStream.Close(); } ExcelParser excel = new ExcelParser(); int score = 0; while (score < toPrint.Count()) { for (int a = 0; a < 3; a++) { Subject subject = context.Subjects.First(x => x.Id == toPrint[score]); //if (subject.IsPrinted) //{ // score++; // if (score == toPrint.Count()) // break; // a--; // continue; //} context.Entry(subject).Reference("Student").Load(); Student student = subject.Student; context.Entry(student).Reference("Group").Load(); excel.Create(doc, subject); subject.IsPrinted = true; XWPFParagraph para = doc.CreateParagraph(); score++; if (score == toPrint.Count()) { break; } } if (score < toPrint.Count()) { XWPFParagraph para = doc.CreateParagraph(); XWPFRun run = para.CreateRun(); run.AddBreak(); } } context.SaveChanges(); using (FileStream fileStreamNew = System.IO.File.Create("wwwroot\\begunok.docx")) { doc.Write(fileStreamNew); fileStreamNew.Close(); } // Тип файла - content-type string file_type = "application/docx"; // Имя файла - необязательно string file_name = "begunok.docx"; var filepath = Path.Combine("~/", "begunok.docx"); return(File(filepath, file_type, file_name)); }