首先,我使用 MS Word 2010 手动创建了表格。然后我使用 Open Xml Productivity 工具检查了该表格,并使用反映的代码使用 C# 生成具有相同单元格格式的动态表格。
但是当生成表格时,每个单元格的底部都有一个空格。
使用 MS Word 2010 增加行高后
我什至在这里尝试了基本示例Add tables to word processing documents (Open XML SDK)。但同样的结果。有什么办法可以去掉这条白线?
首先,我使用 MS Word 2010 手动创建了表格。然后我使用 Open Xml Productivity 工具检查了该表格,并使用反映的代码使用 C# 生成具有相同单元格格式的动态表格。
但是当生成表格时,每个单元格的底部都有一个空格。
使用 MS Word 2010 增加行高后
我什至在这里尝试了基本示例Add tables to word processing documents (Open XML SDK)。但同样的结果。有什么办法可以去掉这条白线?
请分享您的代码。我认为只有第一列是添加的。
MSDN 示例没有在每个单元格的底部添加任何空白。
尝试将以下代码复制粘贴到您的解决方案中。这是 msdn 示例
using System.Collections.Generic;
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
namespace word
{
class Program
{
static void Main(string[] args)
{
//Console.WriteLine("Hello World!");
// CreateWordprocessingDocument(@"D:\data\word.docx");
//string document = @"D:\data\pic1.docx";
//string fileName = @"D:\data\Capture.JPG";
//InsertAPicture(document, fileName);
string fileName = @"D:\data\Table7.docx";
CreateTable2(fileName);
}
public static void CreateTable2(string fileName)
{
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
body.AppendChild(para1("4.1 Process"));
// Create a table.
Table tbl = new Table();
tbl.AppendChild(tablestyle());
// Add 3 columns to the table.
TableGrid tg = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn(), new GridColumn(), new GridColumn());
tbl.AppendChild(tg);
// Add row to the table.
tbl.AppendChild(templaterow());
string[] items = { "Mike Gold", "Don Box",
"Sundar Lal", "Neel Beniwal","123" };
List<string> authorsRange = new List<string>(items);
tbl.AppendChild(rowcolumns(authorsRange));
body.AppendChild(tbl);
}
}
public static void CreateTable1(string fileName)
{
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
wordDocument.AddMainDocumentPart();
// siga a ordem
Document doc = new Document();
Body body = new Body();
// 1 paragrafo
Paragraph para = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Normal" };
Justification justification1 = new Justification() { Val = JustificationValues.Left };
ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
paragraphProperties1.Append(paragraphStyleId1);
paragraphProperties1.Append(justification1);
paragraphProperties1.Append(paragraphMarkRunProperties1);
Run run = new Run();
RunProperties runProperties1 = new RunProperties();
Text text = new Text() { Text = "4. Process Details" };
// siga a ordem
run.Append(runProperties1);
run.Append(text);
para.Append(paragraphProperties1);
para.Append(run);
// 2 paragrafo
Paragraph para2 = new Paragraph();
ParagraphProperties paragraphProperties2 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId() { Val = "Heading1" };
Justification justification2 = new Justification() { Val = JustificationValues.Start };
ParagraphMarkRunProperties paragraphMarkRunProperties2 = new ParagraphMarkRunProperties();
paragraphProperties2.Append(paragraphStyleId2);
paragraphProperties2.Append(justification2);
paragraphProperties2.Append(paragraphMarkRunProperties2);
Run run2 = new Run();
Text text2 = new Text() { Text = "4.1 Process Maps" };
//text2.Text = "4.1 Process Maps";
run2.AppendChild(text2);
para2.Append(paragraphProperties2);
para2.Append(run2);
//Para2
Paragraph para3 = new Paragraph();
ParagraphProperties paragraphProperties3 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId3 = new ParagraphStyleId() { Val = "Heading2" };
Justification justification3 = new Justification() { Val = JustificationValues.Start };
ParagraphMarkRunProperties paragraphMarkRunProperties3 = new ParagraphMarkRunProperties();
paragraphProperties3.Append(paragraphStyleId3);
paragraphProperties3.Append(justification3);
paragraphProperties3.Append(paragraphMarkRunProperties3);
Run run3 = new Run();
Text text3 = new Text() { Text = "4.2 Process Steps" };
//Text text3 = new Text();
//text3.Text = "4.2 Process Steps";
run3.AppendChild(text3);
para3.Append(paragraphProperties3);
para3.Append(run3);
////////////////////////////////////////////////////////////////////
// Create a table.
Table tbl = new Table();
// Set the style and width for the table.
TableProperties tableProp = new TableProperties();
TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };
// Make the table width 100% of the page width.
TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };
//// Create Table Borders
TableBorders tblBorders = new TableBorders();
TopBorder topBorder = new TopBorder();
topBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
topBorder.Color = "CC0000";
tblBorders.AppendChild(topBorder);
BottomBorder bottomBorder = new BottomBorder();
bottomBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
bottomBorder.Color = "CC0000";
tblBorders.AppendChild(bottomBorder);
RightBorder rightBorder = new RightBorder();
rightBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
rightBorder.Color = "CC0000";
tblBorders.AppendChild(rightBorder);
LeftBorder leftBorder = new LeftBorder();
leftBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
leftBorder.Color = "CC0000";
tblBorders.AppendChild(leftBorder);
InsideHorizontalBorder insideHBorder = new InsideHorizontalBorder();
insideHBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
insideHBorder.Color = "CC0000";
tblBorders.AppendChild(insideHBorder);
InsideVerticalBorder insideVBorder = new InsideVerticalBorder();
insideVBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
insideVBorder.Color = "CC0000";
tblBorders.AppendChild(insideVBorder);
//// Add the table borders to the properties
tableProp.AppendChild(tblBorders);
// Apply
tableProp.Append(tableStyle, tableWidth);
tbl.AppendChild(tableProp);
// Add 3 columns to the table.
TableGrid tg = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn(), new GridColumn(), new GridColumn());
tbl.AppendChild(tg);
// Create 1 row to the table.
TableRow tr1 = new TableRow();
// Add a cell to each column in the row.
TableCell tc1 = new TableCell(new Paragraph(new Run(new Text("Step"))));
TableCell tc2 = new TableCell(new Paragraph(new Run(new Text("SubStep"))));
TableCell tc3 = new TableCell(new Paragraph(new Run(new Text("Group"))));
TableCell tc4 = new TableCell(new Paragraph(new Run(new Text("Description"))));
TableCell tc5 = new TableCell(new Paragraph(new Run(new Text("ScreenShot"))));
tr1.Append(tc1, tc2, tc3, tc4, tc5);
// Add row to the table.
tbl.AppendChild(tr1);
// Add the table to the document
body.Append(para);
body.Append(para2);
body.Append(para3);
body.AppendChild(tbl);
doc.Append(body);
wordDocument.MainDocumentPart.Document = doc;
wordDocument.Save();
wordDocument.Close();
}
}
public static void CreateTable(string fileName)
{
// Use the file name and path passed in as an argument
// to open an existing Word 2007 document.
// Create a document by supplying the filepath.
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure and add some text.
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
// Create an empty table.
Table table = new Table();
// Create a TableProperties object and specify its border information.
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
},
new BottomBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
},
new LeftBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
},
new RightBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
},
new InsideHorizontalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
},
new InsideVerticalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
}
)
);
// Append the TableProperties object to the empty table.
table.AppendChild<TableProperties>(tblProp);
// Create a row.
TableRow tr = new TableRow();
// Create a cell.
TableCell tc1 = new TableCell();
// Specify the width property of the table cell.
tc1.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
// Specify the table cell content.
tc1.Append(new Paragraph(new Run(new Text("some text"))));
// Append the table cell to the table row.
tr.Append(tc1);
// Create a second table cell by copying the OuterXml value of the first table cell.
TableCell tc2 = new TableCell(tc1.OuterXml);
// Append the table cell to the table row.
tr.Append(tc2);
// Append the table row to the table.
table.Append(tr);
// Append the table to the document.
//doc.MainDocumentPart.Document.Body.Append(table);
body.Append(table);
}
}
public static void CreateWordprocessingDocument(string filepath)
{
// Create a document by supplying the filepath.
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure and add some text.
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
body.AppendChild(paragraph("12345"));
}
}
public static Paragraph para1(string txt)
{
// 1 paragrafo
Paragraph para = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Normal" };
Justification justification1 = new Justification() { Val = JustificationValues.Left };
ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
paragraphProperties1.Append(paragraphStyleId1);
paragraphProperties1.Append(justification1);
paragraphProperties1.Append(paragraphMarkRunProperties1);
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));
//para.Append(paragraphProperties1);
//para.Append(run);
return para;
}
public static TableProperties tablestyle()
{
// Set the style and width for the table.
TableProperties tableProp = new TableProperties();
TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };
// Make the table width 100% of the page width.
TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };
//// Create Table Borders
TableBorders tblBorders = new TableBorders();
TopBorder topBorder = new TopBorder();
topBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
topBorder.Color = "CC0000";
tblBorders.AppendChild(topBorder);
BottomBorder bottomBorder = new BottomBorder();
bottomBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
bottomBorder.Color = "CC0000";
tblBorders.AppendChild(bottomBorder);
RightBorder rightBorder = new RightBorder();
rightBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
rightBorder.Color = "CC0000";
tblBorders.AppendChild(rightBorder);
LeftBorder leftBorder = new LeftBorder();
leftBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
leftBorder.Color = "CC0000";
tblBorders.AppendChild(leftBorder);
InsideHorizontalBorder insideHBorder = new InsideHorizontalBorder();
insideHBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
insideHBorder.Color = "CC0000";
tblBorders.AppendChild(insideHBorder);
InsideVerticalBorder insideVBorder = new InsideVerticalBorder();
insideVBorder.Val = new EnumValue<BorderValues>(BorderValues.Thick);
insideVBorder.Color = "CC0000";
tblBorders.AppendChild(insideVBorder);
//// Add the table borders to the properties
tableProp.AppendChild(tblBorders);
// Apply
tableProp.Append(tableStyle, tableWidth);
return tableProp;
}
public static TableRow templaterow()
{
// Create 1 row to the table.
TableRow tr1 = new TableRow();
// Add a cell to each column in the row.
TableCell tc1 = new TableCell(new Paragraph(new Run(new Text("Step"))));
TableCellProperties tcr1 = new TableCellProperties();
var shading = new Shading()
{
Color = "auto",
Fill = "B4C6E7",
Val = ShadingPatternValues.Clear
};
tcr1.Append(shading);
tc1.Append(tcr1);
TableCell tc2 = new TableCell(new Paragraph(new Run(new Text("SubStep"))));
TableCell tc3 = new TableCell(new Paragraph(new Run(new Text("Group"))));
TableCell tc4 = new TableCell(new Paragraph(new Run(new Text("Description"))));
TableCell tc5 = new TableCell(new Paragraph(new Run(new Text("ScreenShot"))));
TableCellProperties tcr2 = new TableCellProperties();
var shading2 = new Shading()
{
Color = "auto",
Fill = "B4C6E7",
Val = ShadingPatternValues.Clear
};
tcr2.Append(shading2);
tc2.Append(tcr2);
//tc2.Append(tcr1);
tr1.Append(tc1, tc2, tc3, tc4, tc5);
//tr1.Append(shading);
return tr1;
}
public static TableRow rowcolumns(List<string> items)
{
// Create 1 row to the table.
TableRow tr1 = new TableRow();
for (var i = 0; i < items.Count; i++)
{
//Console.WriteLine("Amount is {0} and type is {1}", myMoney[i].amount, myMoney[i].type);
//TableCell tc1 = new TableCell(new Paragraph(new Run(new Text(items[i]))));
tr1.Append(cellcreaction(items[i]));
}
// Add a cell to each column in the row.
//TableCell tc1 = new TableCell(new Paragraph(new Run(new Text("Step"))));
//TableCell tc2 = new TableCell(new Paragraph(new Run(new Text("SubStep"))));
//TableCell tc3 = new TableCell(new Paragraph(new Run(new Text("Group"))));
//TableCell tc4 = new TableCell(new Paragraph(new Run(new Text("Description"))));
//TableCell tc5 = new TableCell(new Paragraph(new Run(new Text("ScreenShot"))));
//tr1.Append(tc1, tc2, tc3, tc4, tc5);
return tr1;
}
public static TableCell cellcreaction(string txt)
{
return new TableCell(new Paragraph(new Run(new Text(txt))));
}
public static Paragraph paragraph(string txt)
{
Paragraph para = new Paragraph();
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(txt));
return para;
}
public static void InsertAPicture(string document, string fileName)
{
using (WordprocessingDocument wordprocessingDocument =
WordprocessingDocument.Open(document, true))
{
MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
imagePart.FeedData(stream);
}
AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));
}
}
private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
// Define the reference of the image.
var element =
new Drawing(
new DW.Inline(
new DW.Extent() { Cx = 990000L, Cy = 792000L },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = (UInt32Value)1U,
Name = "Picture 1"
},
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties()
{
Id = (UInt32Value)0U,
Name = "New Bitmap Image.jpg"
},
new PIC.NonVisualPictureDrawingProperties()),
new PIC.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension()
{
Uri =
"{28A0092B-C50C-407E-A947-70E740481C1C}"
})
)
{
Embed = relationshipId,
CompressionState =
A.BlipCompressionValues.Print
},
new A.Stretch(
new A.FillRectangle())),
new PIC.ShapeProperties(
new A.Transform2D(
new A.Offset() { X = 0L, Y = 0L },
new A.Extents() { Cx = 990000L, Cy = 792000L }),
new A.PresetGeometry(
new A.AdjustValueList()
)
{ Preset = A.ShapeTypeValues.Rectangle }))
)
{ Uri = "https://schemas.openxmlformats.org/drawingml/2006/picture" })
)
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U,
//EditId = "50D07946"
});
// Append the reference to body, the element should be in a Run.
wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
}
}
}