I need to add a style in a word document by creating WordprocessingDocument
I am creating a word doc with below code:-
private void OpenWordDocument(DataTable dataTable, string xlsxFilePath, string xlsxReportTitle, ArrayList dataTypes, int policyId, int claimId, int eventId, int claimantId, Dictionary<string, string> objDictionary)
{
string notesText = string.Empty;
try
{
using (WordprocessingDocument doc = WordprocessingDocument.Create(xlsxFilePath, WordprocessingDocumentType.Document))
{
//// Defines the MainDocumentPart
MainDocumentPart mainDocumentPart = doc.AddMainDocumentPart();
mainDocumentPart.Document = new Document();
Body body = mainDocumentPart.Document.AppendChild(new Body());
//add main table
DocumentFormat.OpenXml.Wordprocessing.Table tbl = new DocumentFormat.OpenXml.Wordprocessing.Table();
tbl.Append(new TableWidth() { Type = TableWidthUnitValues.Dxa, Width = "9300" });
tbl.Append(new TableLayout() { Type = TableLayoutValues.Fixed });
TableProperties tblProperties = new TableProperties();
TableBorders tblBorders = new TableBorders();
DocumentFormat.OpenXml.Wordprocessing.TopBorder topBorder = new DocumentFormat.OpenXml.Wordprocessing.TopBorder();
topBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
topBorder.Color = "Black";
tblBorders.AppendChild(topBorder);
DocumentFormat.OpenXml.Wordprocessing.BottomBorder bottomBorder = new DocumentFormat.OpenXml.Wordprocessing.BottomBorder();
bottomBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
bottomBorder.Color = "Black";
tblBorders.AppendChild(bottomBorder);
DocumentFormat.OpenXml.Wordprocessing.RightBorder rightBorder = new DocumentFormat.OpenXml.Wordprocessing.RightBorder();
rightBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
rightBorder.Color = "Black";
tblBorders.AppendChild(rightBorder);
DocumentFormat.OpenXml.Wordprocessing.LeftBorder leftBorder = new DocumentFormat.OpenXml.Wordprocessing.LeftBorder();
leftBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
leftBorder.Color = "Black";
tblBorders.AppendChild(leftBorder);
InsideHorizontalBorder insideHBorder = new InsideHorizontalBorder();
insideHBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
insideHBorder.Color = "Black";
tblBorders.AppendChild(insideHBorder);
InsideVerticalBorder insideVBorder = new InsideVerticalBorder();
insideVBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
insideVBorder.Color = "Black";
tblBorders.AppendChild(insideVBorder);
tblProperties.AppendChild(tblBorders); // Add the table borders to the properties
tbl.AppendChild(tblProperties); // Add the table properties to the table
for (int i = 0; i < dataTable.Rows.Count; i++)
{
string altChunkId = "AltChunkId" + i;
DocumentFormat.OpenXml.Wordprocessing.Run run = new DocumentFormat.OpenXml.Wordprocessing.Run();
// 1st row start
DocumentFormat.OpenXml.Wordprocessing.TableRow tr1 = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
tr1.Append(new TableRowHeight() { Val = 500, HeightType = HeightRuleValues.Auto });
DocumentFormat.OpenXml.Wordprocessing.TableCell tr1c1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.Bold()), new DocumentFormat.OpenXml.Wordprocessing.Text(objDictionary["data1_field"].Trim() + ":"))));
tr1c1.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr1c1.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.RightBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr1c1.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr1c1.Append(new TableCellProperties(new TableCellMargin(new TopMargin() { Width = "230" })));
tr1c1.Append(new TableCellProperties(new TableCellMargin(new LeftMargin() { Width = "100" })));
DocumentFormat.OpenXml.Wordprocessing.TableCell tr1c1val = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(dataTable.Rows[i]["data1_value"].ToString()))));
tr1c1val.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr1c1val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = BorderValues.None })));
tr1c1val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr1c1val.Append(new TableCellProperties(new TableCellMargin(new LeftMargin() { Width = "100" })));
DocumentFormat.OpenXml.Wordprocessing.TableCell tr1c2 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.Bold()), new DocumentFormat.OpenXml.Wordprocessing.Text(objDictionary["data2_field"] + ":"))));
tr1c2.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr1c2.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr1c2.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.RightBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr1c2.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr1c2.Append(new TableCellProperties(new TableCellMargin(new LeftMargin() { Width = "100" })));
DocumentFormat.OpenXml.Wordprocessing.TableCell tr1c2val = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(dataTable.Rows[i]["data2_value"].ToString()))));
tr1c2val.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr1c2val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = BorderValues.None })));
tr1c2val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr1c2val.Append(new TableCellProperties(new TableCellMargin(new LeftMargin() { Width = "100" })));
tr1c2val.Append(new TableCellProperties(new TableCellMargin(new RightMargin() { Width = "100" })));
tr1.Append(tr1c1, tr1c1val, tr1c2, tr1c2val);
tbl.AppendChild(tr1);
// Second row starts.
DocumentFormat.OpenXml.Wordprocessing.TableRow tr2 = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
tr2.Append(new TableRowHeight() { Val = 300, HeightType = HeightRuleValues.Auto });
DocumentFormat.OpenXml.Wordprocessing.TableCell tr2c1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.Bold()), new DocumentFormat.OpenXml.Wordprocessing.Text(objDictionary["data3"].Trim() + ":"))));
tr2c1.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr2c1.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.RightBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr2c1.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr2c1.Append(new TableCellProperties(new TableCellMargin(new LeftMargin() { Width = "100" })));
DocumentFormat.OpenXml.Wordprocessing.TableCell tr2c1val = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(dataTable.Rows[i]["data3_field"].ToString()))));
tr2c1val.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr2c1val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = BorderValues.None })));
tr2c1val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.TopBorder() { Val = BorderValues.None })));
tr2c1val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr2c1val.Append(new TableCellProperties(new TableCellMargin(new LeftMargin() { Width = "100" })));
DocumentFormat.OpenXml.Wordprocessing.TableCell tr2c2 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.Bold()), new DocumentFormat.OpenXml.Wordprocessing.Text(objDictionary["subject_field"] + ":"))));
tr2c2.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr2c2.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr2c2.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.RightBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr2c2.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.TopBorder() { Val = BorderValues.None })));
tr2c2.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr2c2.Append(new TableCellProperties(new TableCellMargin(new LeftMargin() { Width = "100" })));
DocumentFormat.OpenXml.Wordprocessing.TableCell tr2c2val = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(dataTable.Rows[i]["subject_field"].ToString()))));
tr2c2val.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr2c2val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = BorderValues.None })));
tr2c2val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.TopBorder() { Val = BorderValues.None })));
tr2c2val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr2c2val.Append(new TableCellProperties(new TableCellMargin(new LeftMargin() { Width = "100" })));
tr2c2val.Append(new TableCellProperties(new TableCellMargin(new RightMargin() { Width = "100" })));
tr2.Append(tr2c1, tr2c1val, tr2c2, tr2c2val);
tbl.AppendChild(tr2);
// 3rd row(start)
DocumentFormat.OpenXml.Wordprocessing.TableRow tr3 = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
tr3.Append(new TableRowHeight() { Val = 300, HeightType = HeightRuleValues.Auto });
DocumentFormat.OpenXml.Wordprocessing.TableCell tr3c1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.Bold()), new DocumentFormat.OpenXml.Wordprocessing.Text(objDictionary["dateEntered_field"] + ":"))));
tr3c1.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr3c1.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.RightBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr3c1.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr3c1.Append(new TableCellProperties(new TableCellMargin(new LeftMargin() { Width = "100" })));
DocumentFormat.OpenXml.Wordprocessing.TableCell tr3c1val = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(dataTable.Rows[i]["dateEntered_field"].ToString()))));
tr3c1val.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr3c1val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = BorderValues.None })));
tr3c1val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.TopBorder() { Val = BorderValues.None })));
tr3c1val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.BottomBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr3c1val.Append(new TableCellProperties(new TableCellMargin(new LeftMargin() { Width = "100" })));
// 4th row(start)
DocumentFormat.OpenXml.Wordprocessing.TableRow tr4 = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
tr4.Append(new TableRowHeight() { Val = 300, HeightType = HeightRuleValues.Auto });
DocumentFormat.OpenXml.Wordprocessing.TableCell tr4c1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
tr4c1.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr4c1.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.RightBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr4c1.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.TopBorder() { Val = BorderValues.None })));
tr4c1.Append(new TableCellProperties(new TableCellMargin(new LeftMargin() { Width = "100" })));
DocumentFormat.OpenXml.Wordprocessing.TableCell tr4c1val = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
if (dataTable.Columns.Contains("data4Trimmed_field"))
{
tr4c1.Append(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.Bold()), new DocumentFormat.OpenXml.Wordprocessing.Text(objDictionary["data4Trimmed_field"] + ":"))));
notesText = "<html><head> <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" /></head>" + dataTable.Rows[i]["noteMemoTrimmed_field"].ToString() + "</html>";
}
else
{
tr4c1.Append(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.RunProperties(new DocumentFormat.OpenXml.Wordprocessing.Bold()), new DocumentFormat.OpenXml.Wordprocessing.Text(objDictionary["data4T_field"] + ":"))));
//notesText = "<html><head> <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" /></head>" + dataTable.Rows[i]["noteMemoCareTech_field"].ToString() + "</html>";
notesText = "<html><head> <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" /></head>" + dataTable.Rows[i]["noteMemoCareTech_field"].ToString().Replace("'", "'") + "</html>";
}
//notesText = CommonFunctions.HTMLCustomDecode(notesText);// commented for 26121 changes , as PDF and Word files were not holding the HTML tags as text unlike Excel files.
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(notesText));
AlternativeFormatImportPart formatImportPart = mainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, altChunkId); // Create alternative format import part.
formatImportPart.FeedData(ms); // Feed HTML data into format import part
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
tr4c1val.Append(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(altChunk)));
tr4c1val.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr4c1val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = BorderValues.None })));
tr4c1val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.TopBorder() { Val = BorderValues.None })));
TableCellProperties tr4c1ValProperties = new TableCellProperties();
HorizontalMerge horizontalMerge = new HorizontalMerge()
{
Val = MergedCellValues.Restart
};
tr4c1ValProperties.Append(horizontalMerge);
tr4c1val.Append(tr4c1ValProperties);
DocumentFormat.OpenXml.Wordprocessing.TableCell tr4c2 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(string.Empty))));
tr4c2.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr4c2.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr4c2.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.RightBorder() { Val = BorderValues.Dashed, Color = "white" })));
tr4c2.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.TopBorder() { Val = BorderValues.None })));
TableCellProperties tr4c2Properties = new TableCellProperties();
HorizontalMerge horizontalMerge2 = new HorizontalMerge()
{
Val = MergedCellValues.Continue
};
tr4c2Properties.Append(horizontalMerge2);
tr4c2.Append(tr4c2Properties);
DocumentFormat.OpenXml.Wordprocessing.TableCell tr4c2val = new DocumentFormat.OpenXml.Wordprocessing.TableCell(new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Text(string.Empty))));
tr4c2val.Append(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tr4c2val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.LeftBorder() { Val = BorderValues.None })));
tr4c2val.Append(new TableCellProperties(new TableCellBorders(new DocumentFormat.OpenXml.Wordprocessing.TopBorder() { Val = BorderValues.None })));
tr4c2val.Append(new TableCellProperties(new TableCellMargin(new RightMargin() { Width = "100" })));
TableCellProperties tr4c2ValProperties = new TableCellProperties();
HorizontalMerge horizontalMerge3 = new HorizontalMerge()
{
Val = MergedCellValues.Continue
};
tr4c2ValProperties.Append(horizontalMerge3);
tr4c2val.Append(tr4c2ValProperties);
tr4.Append(tr4c1, tr4c1val, tr4c2, tr4c2val);
tbl.Append(tr4);
}
body.AppendChild(tbl);
}
}
catch (Exception e)
{
throw e;
}
}
I the 4th row the text contains space so I need to remove the space line means need to add a style with no space in the word document. how I can do it.