Friday 16 December 2011

Microsoft Word Template for Reoport Using ASP.NET C#

private void GenerateWords(string sPO, string sSup)

{

Object oMissing = System.Reflection.Missing.Value;

Object oTrue = true;

Object oFalse = false;

Object savechanges = true;

Word.ApplicationClass oWord = new Word.ApplicationClass();

Word.Document oWordDoc = new Word.Document();

oWord.Visible = true;

Object oTemplatePath = Server.MapPath("Reports/Word/PurchaseOrder.docx");

 

oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

oWordDoc.Activate();

 

foreach (Word.Field myMergeField in oWordDoc.Fields)

{

iTotalFields++;

Word.Range rngFieldCode = myMergeField.Code;

String fieldText = rngFieldCode.Text;

 

// Start filling information in Word file

if (fieldText.StartsWith(" MERGEFIELD"))

{

Int32 endMerge = fieldText.IndexOf("\\");

Int32 fieldNameLength = fieldText.Length - endMerge;

String fieldName = fieldText.Substring(11, endMerge - 11);

 

fieldName = fieldName.Trim();

 

if (fieldName == "PONo")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(sPO);

}

 

if (fieldName == "SupNo")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(sSup);

}

 

if (fieldName == "VendorID")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["VendorID"].ToString().Trim());

}

 

if (fieldName == "VName")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Name"].ToString().Trim());

}

 

if (fieldName == "Contact")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Contact"].ToString().Trim());

}

 

if (fieldName == "Designation")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Designation"].ToString().Trim());

}

 

if (fieldName == "Tel")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Tel"].ToString().Trim());

}

 

if (fieldName == "Fax")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Fax"].ToString().Trim());

}

 

if (fieldName == "PODate")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["PODate"].ToString().Trim());

}

 

if (fieldName == "ClientName")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["ClientName"].ToString().Trim());

}

 

if (fieldName == "JobDescription")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["JobDescription"].ToString().Trim());

}

 

if (fieldName == "JobNo")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["JobNo"].ToString().Trim());

}

 

if (fieldName == "CostCode")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["CostCode"].ToString().Trim());

}

 

if (fieldName == "SchDlvy")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["SchDlvy"].ToString().Trim());

}

 

if (fieldName == "DlvyPoint")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["DlvyPoint"].ToString().Trim());

}

 

if (fieldName == "Amount")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Amount"].ToString().Trim());

}

 

if (fieldName == "tbl")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeParagraph();

Word.Table tbl = oWordDoc.Tables.Add(rngFieldCode, 1, 5, ref oMissing, ref oMissing);

//oWordDoc.Tables.Add(rngFieldCode, dtItems(sPO, sSup).Rows.Count, 5, ref oMissing, ref oMissing);

 

//SET HEADER

SetHeadings(tbl.Cell(1, 1), "Item No.");

SetHeadings(tbl.Cell(1, 2), "Description");

SetHeadings(tbl.Cell(1, 3), "Unit");

SetHeadings(tbl.Cell(1, 4), "Unit Price");

SetHeadings(tbl.Cell(1, 5), "Amount");

//END SET HEADER

 

//Add Row

for (int i = 0; i < dtItems(sPO, sSup).Rows.Count; i++)

{

Word.Row newRow = tbl.Rows.Add(ref oMissing);

newRow.Range.Font.Bold = 0;

newRow.Range.Underline = 0;

newRow.Range.ParagraphFormat.Alignment =

Word.WdParagraphAlignment.wdAlignParagraphCenter;

 

newRow.Cells[1].Range.Text = dtItems(sPO, sSup).Rows[i][3].ToString();

newRow.Cells[2].Range.Text = dtItems(sPO, sSup).Rows[i][4].ToString();

newRow.Cells[3].Range.Text = dtItems(sPO, sSup).Rows[i][8].ToString();

newRow.Cells[4].Range.Text = dtItems(sPO, sSup).Rows[i][10].ToString();

newRow.Cells[5].Range.Text = dtItems(sPO, sSup).Rows[i][11].ToString();

}

//END ROW

 

oWord.Selection.TypeParagraph();

}

 

if (fieldName == "TItems")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Unit"].ToString().Trim());

}

 

if (fieldName == "Discount")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Discount"].ToString().Trim());

}

 

if (fieldName == "TAmount")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtTotal(sPO, sSup).Rows[0]["Amount"].ToString().Trim());

}

 

if (fieldName == "Summary")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["Amount"].ToString().Trim());

}

 

if (fieldName == "ReqNo")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["ReqNo"].ToString().Trim());

}

 

if (fieldName == "RevNo")

{

myMergeField.Select();

oWord.Selection.Font.Color = Word.WdColor.wdColorBlue;

oWord.Selection.TypeText(dtPOSup(sPO, sSup).Rows[0]["RevNo"].ToString().Trim());

}

}

}

// End filling information in Word file

 

Object oSaveAsFile = (Object)Server.MapPath("Reports/Word/tmp2.docx");

oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,

ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

ref oMissing, ref oMissing);

 

oWordDoc.Close(ref savechanges, ref oMissing, ref oMissing);

oWord.Application.Quit(ref savechanges, ref oMissing, ref oMissing);

 

//foreach (Process p in System.Diagnostics.Process.GetProcessesByName("winword"))

//{

// try

// {

// if (p.ProcessName == "WINWORD")

// {

// if (!p.HasExited)

// {

// p.Kill();

// p.WaitForExit(); // possibly with a timeout

// }

// }

// else

// {

// lblMessage.Text = "cannot kill. try again!";

// }

// }

// catch (Win32Exception winException)

// {

// //process was terminating or can't be terminated - deal with it

// Session["error"] = winException.Message;

// Response.Redirect("MessageBoard.aspx");

// }

// catch (InvalidOperationException invalidException)

// {

// //process has already exited - might be able to let this one go

// Session["error"] = invalidException.Message;

// Response.Redirect("MessageBoard.aspx");

// }

//}

 

Response.ClearContent();

Response.ClearHeaders();

Response.ContentType = "application/msword";

Response.WriteFile(Server.MapPath("Reports/Word/tmp2.docx"), false);

Response.Flush();

Response.Close();

}

 

 

No comments:

Post a Comment