Message-ID: <965437996.10255.1711719173938.JavaMail.web05$@web05> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_10254_2078926898.1711719173938" ------=_Part_10254_2078926898.1711719173938 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Calendar Sample

Calendar Sample

Intro

=20

Create a calendar document for any month/year combination.

=20

The template document for this demo is a blank calendar with Word merge = fields in every date block.  The code in this demo determines the curr= ent month and year using a DateTime Object, does some calendar calculations= (eg, figuring out which day of the week the month begins on and how many d= ays are in the month), and finally pushes the date, month, and year data in= to the document with WordTemplate.

=20

Code

=20
=20
 public class CalendarGenerator
    {
        private int year;
        private int month;
 
       
        /// <summary>
        /// Build the report with WordTe=
mplate
        /// </summary>
        public void GenerateDocument()
        {
            //Set th=
e year and month to the current year and month
            DateTime=
 today =3D DateTime.Now;
            year =3D=
 today.Year; 
            month =
=3D today.Month;
           
            // Form =
the name array that will be passed to WordWriter.  The elements
            // of th=
is array are the names of the merge fields in the calendar document
           
            string[]=
 arrNames =3D {"Block1", "Block2", "Block3", =
"Block4", "Block5", "Block6",
            &nb=
sp;            =
           "Block7&q=
uot;, "Block8", "Block9", "Block10", "Bl=
ock11", "Block12",
            &nb=
sp;            =
           "Block13&=
quot;, "Block14", "Block15", "Block16", "=
;Block17", "Block18",
            &nb=
sp;             =
;          "Block19&=
quot;, "Block20", "Block21", "Block22", "=
;Block23", "Block24",
            &nb=
sp;            =
           "Block25&=
quot;, "Block26", "Block27", "Block28", "=
;Block29", "Block30",
            &nb=
sp;            =
           "Block31&=
quot;, "Block32", "Block33", "Block34", "=
;Block35", "Block36",
            &nb=
sp;            =
           "Block37&=
quot;, "Block38", "Block39", "Block40", "=
;Block41", "Block42", "MonthYear"};
           
            // Deter=
mine on which day of the week the month begins
            DateTime=
 thisDate =3D new DateTime(year, month, 1);
            DayOfWee=
k startday =3D thisDate.DayOfWeek;
 
            // Get t=
he number of days in the selected month
            int days=
inmonth =3D DateTime.DaysInMonth(year, month);
 
            //Declar=
e the object array that will be used to hold the
            // value=
s inserted into the calendar template
            
            object[]=
 arrDayBlocks =3D new object[43];
            &nb=
sp;        
            // The f=
irst date of the month is, as always, 1
            int dayo=
fmonth =3D 1;
 
            // Loop =
through all of the available blocks in the calendar     =
;        
            for(int =
iDayBlock =3D 0; iDayBlock <=3D 41; iDayBlock++)
            {
             &n=
bsp;  // If the block exists before the first day of the month, e=
nter an empty string
            &nb=
sp;   if((iDayBlock < (int)startday) || (dayofmonth > daysi=
nmonth))
            &nb=
sp;       arrDayBlocks[iDayBlock] =3D String.=
Empty;
            &nb=
sp;   else
             &n=
bsp;  {
            &nb=
sp;       // Otherwise, it's a valid block, s=
o enter the day of the month
            &nb=
sp;       arrDayBlocks[iDayBlock] =3D dayofmo=
nth;
            &nb=
sp;       dayofmonth++;
            &nb=
sp;   }
            }
            &nb=
sp;
            // The l=
ast element of the value array is reserved for a DateTime struct.
             //=
 Using MergeField date formatting codes, the template merge fields will
             //=
 display the Month name and 4-digit year in the appropriate places
            
            arrDayBl=
ocks[42] =3D thisDate;
           
            // Insta=
ntiate a WordTemplate object
            WordTemp=
late wt =3D new WordTemplate();
           
            // Open =
the template file
            string t=
emplatePath =3D @"..\..\WordTemplateFiles\CalendarTemplate.docx";
            wt.Open(=
templatePath);
           
            // Set t=
he datasource with the name and value arrays defined above
            wt.SetDa=
taSource(arrDayBlocks, arrNames);
           
            // Proce=
ss the template
            wt.Proce=
ss();
           
            // Save =
the document to the desired location
            string d=
ocName =3D String.Format("Calendar-{0}-{1}_output.docx", this.mon=
th, this.year);
            wt.Save(=
@"..\..\WordOutputFiles\"+docName);
        }     
    }

=20
=20


=20

 Downloads

=20
=20

 Template: CalendarTemplate.docx

=20

Output: Calendar-7-2013_output.docx

=20
=20



<= /p>

------=_Part_10254_2078926898.1711719173938--