C# Create Document Word


//this.indentex();

string[] NamaBulan = new String[13]{"","Januari","Februari","Maret","April",
"Mei","Juni","Juli","Agustus","September","Oktober","November","Desember"};
string[] NamaHari = new String[8] { "", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu", "Minggu" };
long RapatId = 1;
MRapat mrapat = new MRapat();
rapat_tab rapat = mrapat.get(RapatId);
string[] DMY = rapat.waktu.ToShortDateString().Split('/');
string TanggalBulanTahun = DMY[0]+" "+NamaBulan[int.Parse(DMY[1])]+" "+ DMY[2];
int IntHari = (int)rapat.waktu.DayOfWeek;
MAgenda magenda = new MAgenda();
List<agenda_tab> listagenda = magenda.getAllByRapatId(RapatId);

var wordApp = new Application();
wordApp.Visible = true;

Document doc = wordApp.Documents.Add();

Range range = doc.Content;
Paragraph paragraph = range.Paragraphs.Add();
paragraph.Range.Text = "";
paragraph.Range.Bold = 1;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
paragraph.Range.Text = "Risalah Keputusan Rapat Direksi".ToUpper();
paragraph.Range.InsertAfter(Environment.NewLine + TanggalBulanTahun);
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();

//Waktu dan tempat title
paragraph.Range.Bold = 1;
paragraph.Range.Font.Underline = WdUnderline.wdUnderlineSingle;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
paragraph.Range.InsertAfter(Environment.NewLine + "WAKTU DAN TEMPAT");
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();

//content
paragraph.Range.Bold = 0;
paragraph.Range.Font.Underline = WdUnderline.wdUnderlineNone;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
paragraph.Range.InsertAfter("Rapat dilaksanakan pada hari "+NamaHari[IntHari]+" tanggal "+TanggalBulanTahun+
" jam ... WIB, bertempat di "+rapat.tempat+".");
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();

//Peserta RAPAT title
paragraph.Range.Bold = 1;
paragraph.Range.Font.Underline = WdUnderline.wdUnderlineSingle;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
paragraph.Range.InsertAfter(Environment.NewLine + "PESERTA RAPAT");
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();

//content
paragraph.Range.Bold = 0;
paragraph.Range.Font.Underline = WdUnderline.wdUnderlineNone;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify;
paragraph.Range.InsertAfter("Rapat dipimpin oleh ... dan dihadiri oleh ...");
paragraph.Range.InsertAfter(Environment.NewLine + Environment.NewLine + "Daftar hadir terlampir");
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();

//Agenda Rapat title
paragraph.Range.Bold = 1;
paragraph.Range.Font.Underline = WdUnderline.wdUnderlineSingle;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
paragraph.Range.InsertAfter(Environment.NewLine + "AGENDA RAPAT" + Environment.NewLine);
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();



Microsoft.Office.Interop.Word.Table firstTable = range.Tables.Add(paragraph.Range,
listagenda.Count()+1, 4);
int[] TableWidth = new int[4] { 30, 230, 130, 80 };
string[] ColumnName = new string[4] { "No.", "TOPIK", "PIC", "WAKTU" };
for (int i = 0; i < TableWidth.Count(); i++)
{
firstTable.Columns[i + 1].SetWidth(TableWidth[i], WdRulerStyle.wdAdjustSameWidth);
}

firstTable.Borders.Enable = 1;

int CounterAgenda = -1; // Diset -1 karena header ikut dihitung
foreach (Row row in firstTable.Rows)
{

foreach (Cell cell in row.Cells)
{
//Header row
if (cell.RowIndex == 1)
{

cell.Range.Paragraphs.SpaceAfter = 0;
cell.Range.Text = ColumnName[cell.ColumnIndex - 1];
cell.Shading.BackgroundPatternColor = WdColor.wdColorBlack;
cell.Range.Font.Underline = WdUnderline.wdUnderlineNone;
cell.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

}
//Data row
else
{
agenda_tab agenda  = listagenda.ElementAt(CounterAgenda);
cell.Range.Font.Underline = WdUnderline.wdUnderlineNone;
cell.Range.Bold = 0;

if (cell.ColumnIndex == 1) // NO
{
cell.Range.Text = (cell.RowIndex - 1).ToString();
}
else if (cell.ColumnIndex == 2) // TOPIK
{
cell.Range.Text = agenda.topik;
}
else if (cell.ColumnIndex == 3) //PIC
{
cell.Range.Text = agenda.PIC;
}
else if (cell.ColumnIndex == 4) // WAKTU
{
cell.Range.Text = agenda.waktu;
}
cell.Range.Paragraphs.SpaceAfter = 0;

}
}
CounterAgenda++;

}

//RISALAH ....
paragraph.Range.Bold = 1;
paragraph.Range.Font.Underline = WdUnderline.wdUnderlineSingle;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
paragraph.Range.InsertAfter(Environment.NewLine +
"RISALAH ATAS HASIL PEMBAHASAN YANG DILAKSANAKAN OLEH DIREKSI"  + Environment.NewLine);
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();

List<agenda_tab> listagendadetail  = new MAgenda().getAllByRapatId(RapatId);
CounterAgenda = 0; //Di reset lagi
paragraph.Range.Text = "";
for (int i = 0; i < listagenda.Count(); i++)
{
paragraph.Outdent();
agenda_tab agenda = listagendadetail.ElementAt(CounterAgenda);

paragraph.Range.Bold = 1;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
paragraph.Range.Font.Underline = WdUnderline.wdUnderlineNone;
paragraph.Range.InsertAfter(agenda.topik);
paragraph.Range.ListFormat.ApplyNumberDefault();
paragraph.Outdent();
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();

paragraph.Range.Bold = 0;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
paragraph.Range.Font.Underline = WdUnderline.wdUnderlineNone;
paragraph.Range.InsertAfter("Pembahasan : ");
paragraph.Outdent();
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();

paragraph.Range.Bold = 0;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
paragraph.Range.Font.Underline = WdUnderline.wdUnderlineNone;
paragraph.Range.InsertAfter(agenda.risalah);
paragraph.Outdent();
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();

//NEW LINE
paragraph.Range.Bold = 0;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
paragraph.Range.Font.Underline = WdUnderline.wdUnderlineNone;
paragraph.Range.InsertAfter("");
paragraph.Outdent();
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();

CounterAgenda++;
}


string[] Jabatan = new string[5] { "DIREKTUR UTAMA", "DIREKTUR ENJINIRING",
"DIREKTUR PEMASARAN & PENGEMBANGAN USAHA", "DIREKTUR KEUANGAN & SDM","NOTULIS"};

string[] Nama = new string[5] { "ZAINAL ABIDIN SIHITE","WAHYUDI AGUS", "EDWARD SIAGIAN",
"BURHANUDDIN", "KATHERINE AMARANILA" };

int JumlahBaris = Jabatan.Count() / 2;
bool OddFlag = false;
if (Jabatan.Count() % 2 != 0)
{
OddFlag = true;
JumlahBaris += 1;
}


paragraph.Range.InsertBreak();
paragraph.Reset();

paragraph.Outdent();
paragraph.Range.Bold = 0;
paragraph.Range.Font.Underline = WdUnderline.wdUnderlineNone;
paragraph.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
paragraph.Range.InsertAfter(Environment.NewLine + "Jakarta, "+TanggalBulanTahun + Environment.NewLine);
paragraph.Outdent();
paragraph.Range.InsertAfter(Environment.NewLine + "PT PLN ENJINIRING");
paragraph.Range.InsertParagraphAfter();
paragraph.Reset();


//TABLE BORDER 0 ; Tanda Tangan
Microsoft.Office.Interop.Word.Table TableTTD = range.Tables.Add(
paragraph.Range, JumlahBaris*6,3);
TableTTD.Borders.Enable = 0;
int CounterJabatan = 0;
int CounterName = 0;
foreach (Row row in TableTTD.Rows)
{
foreach (Cell cell in row.Cells)
{
if (OddFlag && (cell.RowIndex % ((JumlahBaris * 6) + 1) < 7))
{

cell.Merge(TableTTD.Cell(cell.RowIndex, cell.ColumnIndex + 2));

if (cell.RowIndex % ((JumlahBaris * 6) + 1) < 6)
{

if (cell.RowIndex % 6 == 1)
{
cell.Range.Bold = 1;
cell.Range.Text = Jabatan[CounterJabatan];
CounterJabatan++;
cell.Range.Underline = WdUnderline.wdUnderlineNone;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
}
else if (cell.RowIndex % 6 == 5)
{
cell.Range.Bold = 1;
cell.Range.Underline = WdUnderline.wdUnderlineSingle;
cell.Range.Text = Nama[CounterName];
CounterName++;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
}

}

}
else
{

if (cell.ColumnIndex == 1 || cell.ColumnIndex == 3)
{
if (cell.RowIndex % 6 == 1)
{
cell.Range.Bold = 1;
cell.Range.Text = Jabatan[CounterJabatan];
CounterJabatan++;
cell.Range.Underline = WdUnderline.wdUnderlineNone;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
}
else if (cell.RowIndex % 6 == 5)
{
cell.Range.Bold = 1;
cell.Range.Underline = WdUnderline.wdUnderlineSingle;
cell.Range.Text = Nama[CounterName];
CounterName++;
cell.Range.Underline = WdUnderline.wdUnderlineSingle;
cell.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
}
}


}
cell.Range.Paragraphs.SpaceAfter = 0;

}
}


//HEADER AND FOOTER
Section section = range.Sections.Add();
section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].
Range.InlineShapes.AddPicture(AppDomain.CurrentDomain.BaseDirectory + @"Assets\PLNE.png");
section.Range.InsertParagraph();
section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.InlineShapes.AddHorizontalLineStandard();
section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(WdPageNumberAlignment.wdAlignPageNumberRight);
section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.InsertAfter("Risalah Rapat Direksi "+TanggalBulanTahun);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: