Beispiel #1
0
 private static void siteBasicInfoSection(vw_surveys s, vw_Basicinfo b, Word.Document worddoc, int i)
 {
     worddoc.Tables[i].Columns[2].Cells[2].Range.Text  = s.SSID;
     worddoc.Tables[i].Columns[2].Cells[3].Range.Text  = s.SiteName;
     worddoc.Tables[i].Columns[2].Cells[4].Range.Text  = s.Lat.ToString();
     worddoc.Tables[i].Columns[2].Cells[5].Range.Text  = s.Lon.ToString();
     worddoc.Tables[i].Columns[2].Cells[6].Range.Text  = string.IsNullOrEmpty(b.Type) ? "N/A" : b.Type;
     worddoc.Tables[i].Columns[2].Cells[7].Range.Text  = string.IsNullOrEmpty(b.classification) ? "N/A" : b.classification;
     worddoc.Tables[i].Columns[2].Cells[8].Range.Text  = s.State;
     worddoc.Tables[i].Columns[2].Cells[9].Range.Text  = s.LGA;
     worddoc.Tables[i].Columns[2].Cells[10].Range.Text = s.Ward;
 }
Beispiel #2
0
        public ActionResult ExportWord(string ssid, string date1, string tokenId)
        {
            DateTime dt = DateTime.Parse(date1);

            DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
            Calendar           cal = dfi.Calendar;

            int weekno = cal.GetWeekOfYear(dt, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);

            string weekNoYear = string.Format("{0}-{1}", weekno, dt.Year);


            vw_surveys s = db.vw_surveys.Where(x => x.WeekNo == weekno && x.weekNoYear == weekNoYear && x.SSID == ssid).FirstOrDefault();
            // string sql = "SELECT * FROM vw_surveys s WHERE s.WeekNo = @strWeekNo@ AND s.WeekNoYear = '@strWeekNoYear@'@strTem@;";
            vw_Basicinfo b = db.vw_Basicinfo.Where(x => x.SSID == ssid).FirstOrDefault();;

            spGetDemoTrend_Result[] res = db.spGetDemoTrend(ssid, weekno).ToArray();

            //count age_group
            int t1, t1_5, t6_12, t13_17, t18_59, t60p, tpct, totF, totM;

            //computations
            t1     = (s.f_lt1 ?? 0) + (s.m_lt1 ?? 0);
            t1_5   = (s.f_1_5 ?? 0) + (s.m_1_5 ?? 0);
            t6_12  = (s.f_6_12 ?? 0) + (s.m_6_12 ?? 0);
            t13_17 = (s.f_13_17 ?? 0) + (s.m_13_17 ?? 0);
            t18_59 = (s.f_18_59 ?? 0) + (s.m_18_59 ?? 0);
            t60p   = (s.f_60p ?? 0) + (s.m_60p ?? 0);

            tpct = t1 + t1_5 + t6_12 + t13_17 + t18_59 + t60p;

            totF = (s.f_lt1 ?? 0) + (s.f_1_5 ?? 0) + (s.f_6_12 ?? 0) +
                   (s.f_13_17 ?? 0) + (s.f_18_59 ?? 0) + (s.f_60p ?? 0);
            totM = (s.m_lt1 ?? 0) + (s.m_1_5 ?? 0) + (s.m_6_12 ?? 0) +
                   (s.m_13_17 ?? 0) + (s.m_18_59 ?? 0) + (s.m_60p ?? 0);


            int[]    breakdown_tots   = { t1, t1_5, t6_12, t13_17, t18_59, t60p };
            string[] breakdown_labels = { "'<1'", "'1 - 5'", "'6 - 12'", "'13 - 17'", "'18 - 59'", "'60+'" };
            int[]    agg_tots         = { tpct, totF, totM };
            int[]    margins          = { 10, 10, 20, 35, 10, 10 };

            int[] gender_colors = { 3, 41 };

            object missing   = System.Reflection.Missing.Value;
            object readOnly  = false;
            object isVisible = true;

            var applicationWord = new Word.Application();

            Word.Document worddoc;

            object fileName = HttpContext.Server.MapPath("~/ReportXlsx/template.doc");

            worddoc = applicationWord.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

            try
            {
                worddoc.Activate();


                int i = 1;

                //worddoc.Tables[i].Columns[3].Cells[2].Range.Text = "Site Name: " & rs("SiteName").Value & "  ===  Week: " & rs("WeekNo").Value
                docHeaderSection(s, worddoc, 1);

                //applicationWord.Visible = true;

                householdFiguresSection(s, totF, totM, worddoc, 2);

                //i = 5;
                siteBasicInfoSection(s, b, worddoc, 5);

                i = 6;

                string[] WeekDate;
                string[] N;

                trendOfIDPsSection(res, applicationWord, worddoc, 6, out WeekDate, out N);

                drawGraph1(applicationWord, worddoc, WeekDate, N);

                i = 7;
                surveyDetailsSection(s, worddoc, 7);

                demographicSection(s, t1, t1_5, t6_12, t13_17, t18_59, t60p, totF, totM, applicationWord, worddoc, 7);


                i = 8;
                ageSexBreakdownSection(t1, t1_5, t6_12, t13_17, t18_59, t60p, tpct, totF, totM, applicationWord, worddoc, 8);
                //======================================

                drawGraph2(applicationWord, worddoc, breakdown_tots, breakdown_labels, agg_tots, margins);

                drawGraph3(applicationWord, worddoc, agg_tots, gender_colors);

                i = 9;//Table CCCM

                cccmEsNFISection(s, applicationWord, worddoc, 9);

                washSection(s, applicationWord, worddoc, 11);

                protectionSection(s, applicationWord, worddoc, 13);

                educationSection(s, applicationWord, worddoc, 15);

                healthNutritionSection(s, applicationWord, worddoc, 17);

                i = 6;// 'Table Survey Details

                worddoc.Tables[i].Columns[1].Cells[3].Select();

                applicationWord.Selection.MoveDown(Unit: Word.WdUnits.wdLine, Count: 20);
                applicationWord.Selection.Delete(Unit: Word.WdUnits.wdCharacter, Count: 1);
                applicationWord.Selection.MoveDown(Unit: Word.WdUnits.wdLine, Count: 1);
                applicationWord.Selection.Delete(Unit: Word.WdUnits.wdCharacter, Count: 1);
                applicationWord.Selection.MoveDown(Unit: Word.WdUnits.wdLine, Count: 5);
                applicationWord.Selection.Delete(Unit: Word.WdUnits.wdCharacter, Count: 1);
                applicationWord.Selection.MoveDown(Unit: Word.WdUnits.wdLine, Count: 1);
                applicationWord.Selection.Delete(Unit: Word.WdUnits.wdCharacter, Count: 1);
                applicationWord.Selection.MoveDown(Unit: Word.WdUnits.wdLine, Count: 5);
                applicationWord.Selection.Delete(Unit: Word.WdUnits.wdCharacter, Count: 1);
                applicationWord.Selection.MoveDown(Unit: Word.WdUnits.wdLine, Count: 1);
                applicationWord.Selection.Delete(Unit: Word.WdUnits.wdCharacter, Count: 1);
                applicationWord.Selection.MoveDown(Unit: Word.WdUnits.wdLine, Count: 5);
                applicationWord.Selection.Delete(Unit: Word.WdUnits.wdCharacter, Count: 1);
                applicationWord.Selection.MoveDown(Unit: Word.WdUnits.wdLine, Count: 1);
                applicationWord.Selection.Delete(Unit: Word.WdUnits.wdCharacter, Count: 1);
                applicationWord.Selection.MoveDown(Unit: Word.WdUnits.wdLine, Count: 6);
                applicationWord.Selection.Delete(Unit: Word.WdUnits.wdCharacter, Count: 1);

                //applicationWord.Visible = true;


                //  worddoc.savea
                //using (var entryStream = demoFile.Open())
                //{ }


                IFormatProvider culture = new System.Globalization.CultureInfo("en-GB", true);
                String          date    = DateTime.Now.ToShortDateString();

                date = date.Replace("/", ".");

                date = formatStrDate(date);

                //String ReportName = "cm_tracker_one_pager";
                string myName = Server.UrlEncode(ssid + "_" + weekNoYear + "_" + date + ".doc");

                // string myName = Server.UrlEncode(ReportName + ".xlsx");
                //MemoryStream stream = new MemoryStream();
                object fname = Path.GetTempFileName();
                worddoc.SaveAs(ref fname, ref missing, ref missing, ref missing, ref missing);
                //new MemoryStream();
                //worddoc.SaveAs(stream);
                //GetStream(worddoc).CopyTo(stream);

                object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                worddoc.Application.Quit(ref saveChanges, ref missing, ref missing);
                Marshal.ReleaseComObject(worddoc);

                byte[] byteArray = ReadFile((string)fname);
                //MemoryStream ms = new MemoryStream();
                //fs.CopyTo(ms);



                //worddoc.Close();
                //GC.SuppressFinalize(worddoc);
                //applicationWord
                //.Dispose();

                Response.Clear();

                var cookie = new HttpCookie("fileDownloadToken", tokenId);
                Response.AppendCookie(cookie);

                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment; filename=" + myName);
                Response.ContentType = "application/vnd.ms-word";
                Response.BinaryWrite(byteArray);
                Response.End();

                Response.Flush();

                //Call stored procedure to add user activity here
                db.spAddUserActivity(User.Identity.Name, "Generate report (one pager)", "[User]='" + User.Identity.Name + " generated report for [Week]='"
                                     + weekNoYear + "' and [SSID]=" + ssid + "'.", DateTime.Now);

                return(File(byteArray, "application/vnd.ms-word", myName));
            }
            catch (COMException ex)
            {
            }
            finally {
                //applicationWord.Quit(ref missing, ref missing, ref missing);
                GC.SuppressFinalize(worddoc);
                GC.SuppressFinalize(applicationWord);
            }
            //Guid parameter,



            //string filePath = "~/ReportXlsx/template.doc";



            return(null);
        }