Пример #1
0
        public void GenerateDocument(List <ExtratimeActive> table, DateTime fromDate, DateTime toDate, string week)
        {
            DocX   document = null;
            string fileName = "Tuan" + week + "_" + fromDate.Year.ToString();

            document = DocX.Create(Server.MapPath("~/Template/" + fileName + ".docx"), DocumentTypes.Document);

            //Image img = document.AddImage(Server.MapPath("~/Images/mvc.png"));

            //Picture pic = img.CreatePicture(100, 100);


            //Paragraph picturepara = document.InsertParagraph();
            //picturepara.Alignment = Alignment.center;
            // picturepara.Append("                                ");
            //picturepara.AppendPicture(pic).Alignment = Alignment.center;

            var headLineFormat = new Formatting();

            headLineFormat.FontFamily = new Xceed.Words.NET.Font("Times New Roman");
            headLineFormat.Size       = 14;
            headLineFormat.Position   = 12;

            string headlineText      = "LỊCH LÀM VIỆC CỦA KHOA KỸ THUẬT- CÔNG NGHỆ";
            string smallheadlineText = "Tuần thứ " + week + " (Từ ngày " + FunctionUtility.FotmatDateTime(fromDate) + " đến ngày " + FunctionUtility.FotmatDateTime(toDate) + " )";

            document.InsertParagraph(headlineText, false, headLineFormat).Alignment = Alignment.center;
            var smallheadLineFormat = new Formatting();

            //headLineFormat.FontFamily = new System.Drawing.FontFamily("Arial Black");
            smallheadLineFormat.Size       = 12;
            smallheadLineFormat.FontFamily = new Xceed.Words.NET.Font("Times New Roman");
            document.InsertParagraph(smallheadlineText, false, headLineFormat).Alignment = Alignment.center;
            int indexTemp = 0;
            int kTemp     = 1;

            for (DateTime date = fromDate; date < toDate; date = date.AddDays(1.0))
            {
                kTemp = kTemp + 1;
                int ktTemp = 0;
                foreach (var item in table)
                {
                    if (item.Date == date)
                    {
                        kTemp  = kTemp + 1;
                        ktTemp = 1;
                    }
                }
                if (ktTemp == 0)
                {
                    kTemp = kTemp + 1;
                    if (indexTemp == 6)
                    {
                        break;
                    }
                }
            }

            Table tbl = document.AddTable(kTemp, 4);

            tbl.Alignment = Alignment.center;

            //tbl.Design = TableDesign.LightGridAccent2;

            tbl.Rows[0].Cells[0].Paragraphs.First().Append("Thời gian");
            tbl.Rows[0].Cells[1].Paragraphs.First().Append("Nội dung");
            tbl.Rows[0].Cells[2].Paragraphs.First().Append("Thành phần ");
            tbl.Rows[0].Cells[3].Paragraphs.First().Append("Địa điểm");

            //for (int i = 1; i <= 4; i++)
            //{
            //    for (int j = 0; j <= 3; j++)
            //    {
            //        tbl.Rows[i].Cells[j].Paragraphs.First().Append("(" + i + "," + j + ")");
            //    }
            //}
            int index = 0;
            int k     = 1;

            for (DateTime date = fromDate; date < toDate; date = date.AddDays(1.0))
            {
                index = index + 1;
                tbl.Rows[k].Cells[0].Paragraphs.First().Append(date.ToShortDateString());
                tbl.Rows[k].MergeCells(0, 3);
                k = k + 1;
                int kt = 0;
                foreach (var item in table)
                {
                    if (item.Date == date)
                    {
                        string joiner = string.Empty;
                        tbl.Rows[k].Cells[0].Paragraphs.First().Append(item.Hour);
                        tbl.Rows[k].Cells[1].Paragraphs.First().Append(item.Content);
                        foreach (var tempItem in _extaSrv.GetDataJoinerToSendMail(item.PID))
                        {
                            joiner = joiner + "_" + _extaSrv.GetName(tempItem.TeacherCode) + " ";
                        }
                        tbl.Rows[k].Cells[2].Paragraphs.First().Append(joiner);
                        tbl.Rows[k].Cells[3].Paragraphs.First().Append(item.Location);
                        k  = k + 1;
                        kt = 1;
                    }
                }
                if (kt == 0)
                {
                    tbl.Rows[k].Cells[0].Paragraphs.First().Append("");
                    tbl.Rows[k].Cells[1].Paragraphs.First().Append("");
                    tbl.Rows[k].Cells[2].Paragraphs.First().Append("");
                    tbl.Rows[k].Cells[3].Paragraphs.First().Append("");
                    k = k + 1;
                    //if(index==6)
                    //{
                    //    break;
                    //}
                }
            }


            //tbl.Rows[1].MergeCells(0, 3);
            document.InsertTable(tbl);

            // For  Farsi, Arabic and Urdu.
            // document.SetDirection(Direction.RightToLeft);

            document.Save();


            MemoryStream ms = new MemoryStream();

            document.SaveAs(ms);
            // document.Save(ms, SaveFormat.Docx);
            byte[] byteArray = ms.ToArray();
            ms.Flush();
            ms.Close();
            ms.Dispose();
            //Response.Clear();
            //Response.AddHeader("Content-Disposition", "attachment; filename=report.docx");
            //Response.AddHeader("Content-Length", byteArray.Length.ToString());
            //Response.ContentType = "application/msword";
            //Response.BinaryWrite(byteArray);
            //Response.End();
        }