Rev 2128 | Blame | Compare with Previous | Last modification | View Log | RSS feed
using System;using System.Collections;using System.Text;using System.ComponentModel;using System.Windows.Forms;using Microsoft.Office.Interop.Word;namespace EA_DocGen{/// <summary>/// Summary description for TabularContent./// </summary>public class TabularContent{private static Range WordRange;private static object startLocation;private static object endLocation;public static void initialise(){}/// <summary>/// This is an attempt to create a table similar in style to those made by the ERG doc template macros./// It is not exaclty that same though, and further work could be done to align it more precisely./// </summary>/// <param name="tableTitle"></param>/// <param name="numRows"></param>/// <param name="numCols"></param>/// <returns></returns>public static int Table_Create(string tableTitle, bool borders, int numRows, int numCols){createWordDoc.WordDocument.Content.InsertParagraphAfter();SelectInsertionPointAtEndOfDocument();Table_InsertCaption(tableTitle);startLocation = createWordDoc.WordDocument.Content.End;createWordDoc.WordDocument.Content.InsertParagraphAfter();endLocation = createWordDoc.WordDocument.Content.End;WordRange = createWordDoc.WordDocument.Range(ref startLocation, ref endLocation);object defaultTableBehaviour = Type.Missing;object autofitBehaviour = Type.Missing;Table Table = createWordDoc.WordDocument.Tables.Add( WordRange, numRows, numCols, ref defaultTableBehaviour, ref autofitBehaviour );if (borders){Table.Rows[1].Shading.BackgroundPatternColor = WdColor.wdColorGray20;Table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;Table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;}else{Table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleNone;Table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleNone;}Table.Select();object tableTextStyle = EA_Constants.styleName_TableText;Table.Range.set_Style( ref tableTextStyle );return createWordDoc.WordDocument.Tables.Count;}public static void Table_SetTableColumnTitle(Table table, string title, int column){table.Rows[1].Shading.BackgroundPatternColor = WdColor.wdColorGray10;table.Cell(1,column).Range.Text = title;table.Cell(1,column).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;table.Cell(1,column).Range.Font.Bold = 1;}public static void Table_SetTableCellTitle(Table table, string title, int row, int column){table.Cell(row,column).Shading.BackgroundPatternColor = WdColor.wdColorGray20;table.Cell(row,column).Range.Text = title;table.Cell(row,column).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;table.Cell(row,column).Range.Font.Bold = 1;}public static int Table_InsertNewRowAfterThisRow(Table table, int row){table.Rows[row].Select();object rowC = 1;createWordDoc.WordApp.Selection.InsertRowsBelow(ref rowC);table.Rows[row+1].Shading.BackgroundPatternColor = WdColor.wdColorWhite;return row+1;}public static void Table_DeleteThisRow(Table table, int row){table.Rows[row].Delete();}public static void Table_DeleteRows(Table table, int row, int count){for (int i=0; i<count; i++){table.Rows[row].Delete();}}public static int Table_AppendSeperatorAndNewRowAfterThisRow(Table table, int row){// add two more rows, the first being a seperatorint seperatorRow = Table_InsertNewRowAfterThisRow(table, row);int newRow = Table_InsertNewRowAfterThisRow(table, seperatorRow);// Make seperator row, one cell wide, and remove its left & right borderstable.Rows[seperatorRow].Select();createWordDoc.WordApp.Selection.Cells.Merge();createWordDoc.WordApp.Selection.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleNone;createWordDoc.WordApp.Selection.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;return newRow;}public static void Table_MergeCellsInThisRow(Table table, int row){table.Rows[row].Select();createWordDoc.WordApp.Selection.Cells.Merge();}public static void Table_InsertCaption(string captionText){if (captionText.Length > 0){object Label = "Table";object Title = Type.Missing;object TitleAutoText = Type.Missing;object Position = WdCaptionPosition.wdCaptionPositionAbove;object ExcludeLabel = 0;createWordDoc.WordApp.Selection.InsertCaption( ref Label, ref Title, ref TitleAutoText, ref Position, ref ExcludeLabel);createWordDoc.WordApp.Selection.TypeText( ": " + captionText);createWordDoc.WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;}}private static void SelectInsertionPointAtEndOfDocument(){object unit;object extend;unit = WdUnits.wdStory;extend = WdMovementType.wdMove;createWordDoc.WordApp.Selection.EndKey(ref unit, ref extend);}/// <summary>/// Processes an EA_DocGen table element, taking the notes that it contains, and forming a/// table in the generated document with them. The notes contain instructions on how to format/// the table, as well as the cell content. This must all be parsed and tokenised to extract the/// options and data./// </summary>/// <param name="theElement"></param>/// <param name="recurse_level"></param>public static void processTableElement(EA.Element theElement, int recurse_level ){processTableElement(theElement.Notes, recurse_level, 0);}public static void processTableElement(string tableDef, int recurse_level, float indent_pts ){string [] EA_DocGenTable = null;string [] cells = null;string delimStr = "\r\n";char [] delim = delimStr.ToCharArray();string trimStr = " ";char [] trimChars = trimStr.ToCharArray();EA_DocGenTable = tableDef.Split(delim,200);int columnCount = 0;int rowCount = 0;char seperator = ',';string tableTitle = "";bool borders=true;bool gotSeperator = false;bool gotTitle = false;bool gotBorders = false;bool gotWidths = false;bool gotIndent = false;ArrayList colWidths = new ArrayList();int indent = 0;// Scan the notes content line by line looking for table parameters and counting// the number of table rows (which is not specified as an explicit table parameter).int s_ElementsConsumedByTableParams = 0;foreach(string s in EA_DocGenTable){if (s.Length > 0 && s != "\n" && s != "\r" ){if (gotTitle == false && s.StartsWith("title=")){s_ElementsConsumedByTableParams++;tableTitle = EA_DocGenOptions.getOptionValue(s, tableTitle);gotTitle = true;//if (tableTitle == "")//{// MessageBox.Show( "Table Element Serialisation Failed - Bad Title" );// break;//}}else if (columnCount == 0 && s.StartsWith("columns=")){s_ElementsConsumedByTableParams++;columnCount = EA_DocGenOptions.getOptionValue(s, columnCount);if (columnCount == 0){MessageBox.Show( "Table Element Serialisation Failed - bad column count" );break;}}else if (gotSeperator == false && s.StartsWith("seperator=")){s_ElementsConsumedByTableParams++;seperator = EA_DocGenOptions.getOptionValue(s, seperator);gotSeperator = true;}else if (gotWidths == false && s.StartsWith("widths=")){s_ElementsConsumedByTableParams++;gotWidths = true;string optValStr = EA_DocGenOptions.getOptionValue(s, "");string width_delimStr = ",";char [] width_delim = width_delimStr.ToCharArray();string [] width_strings = optValStr.Split(width_delim, 50);foreach (string ws in width_strings){if (ws.Length > 0 && ws != "\n" && ws != "\r" ){colWidths.Add( System.Convert.ToDouble(ws) );}}}else if (gotIndent == false && s.StartsWith("indent=")){s_ElementsConsumedByTableParams++;gotIndent = true;indent = EA_DocGenOptions.getOptionValue(s, 0);}else if (gotBorders == false && s.StartsWith("borders=")){s_ElementsConsumedByTableParams++;gotBorders = true;borders = EA_DocGenOptions.getOptionValue(s, borders);}else{rowCount++;}}}if (columnCount > 0 && rowCount > 0){if (rowCount < 2){MessageBox.Show( "Table Element Serialisation Failed - Insufficient Rows" );}else{// create the table in the word docint tableNum = Table_Create( tableTitle, borders, rowCount, columnCount );Table table = createWordDoc.WordDocument.Tables[tableNum];object center = WdParagraphAlignment.wdAlignParagraphCenter;int col = 1;foreach (double d in colWidths){if (col <= columnCount)table.Columns[col].SetWidth( createWordDoc.WordApp.CentimetersToPoints((float)d), WdRulerStyle.wdAdjustNone );col++;}if (indent > 0){table.Select();while (indent > 0){createWordDoc.WordApp.Selection.Paragraphs.Indent();indent--;}}// scan the element notes again to extract the cell content and add it to the// table we just created.int row = 1;col = 0;foreach(string s in EA_DocGenTable){if (s.Length > 0 && s != "\n" && s != "\r" ){if (s_ElementsConsumedByTableParams > 0){s_ElementsConsumedByTableParams--;}else{delimStr = seperator.ToString();delim = delimStr.ToCharArray();cells = s.Split(delim,columnCount);if (cells.Length != columnCount){MessageBox.Show( "Table Element Serialisation Failed - Bad Row" );break;}else{// serialise table rowfor(col=1; col<=columnCount; col++){cells[col-1].TrimStart( trimChars );cells[col-1].TrimEnd( trimChars );cells[col-1] = cells[col-1].Replace("<br>","\r\n");table.Cell(row,col).Range.Text = cells[col-1];// special handling for heading rowif (row == 1){table.Cell(row,col).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;table.Cell(row,col).Range.Font.Bold = 1;}}row++;}}}}}}else{MessageBox.Show( "Table Element Serialisation Failed - Table Parameters Incomplete" );}}}}