12. 将以下程序加到foreach语句的后面(最外面的那个foreach语句),但是要在using语句的里面; 为避免手工输入这些语句,可以通过插入代码段 | My Code Snippets | AddRow.
// Append table and save
doc.Body.AppendChild(table);
// Save changes
// this step is not necessary as doc has been configured to automatically save changes
doc.Save();
doc.Body.AppendChild(table);
// Save changes
// this step is not necessary as doc has been configured to automatically save changes
doc.Save();
备注:在这一步中,我们只是把生成的table加到文档中并保存文档。至此我们完成了所有需要加在Main()里的语句。
13. 将以下程序加到method:static void Main(string[] args)的后面,在class Program 的closing bracket (}) 里面;为避免手工输入这些语句,可以通过插入代码段 | My Code Snippets | CreateRow.
private static TableRow CreateRow(string[] cellText, Drawing d)
{
// create a table row and insert content
TableRow tr = new TableRow();
// Add text
foreach (string s in cellText)
{
TableCell tc = new TableCell();
Paragraph p = new Paragraph();
Run r = new Run();
Text t = new Text();
t.Text = s;
r.AppendChild(t);
p.AppendChild(r);
tc.AppendChild(p);
tr.AppendChild(tc);
}
// Add image
if (d != null)
{
TableCell tcDrawing = new TableCell();
Paragraph pDrawing = new Paragraph();
Run rDrawing = new Run();
rDrawing.AppendChild(d);
pDrawing.AppendChild(rDrawing);
tcDrawing.AppendChild(pDrawing);
tr.AppendChild(tcDrawing);
}
return tr;
}
{
// create a table row and insert content
TableRow tr = new TableRow();
// Add text
foreach (string s in cellText)
{
TableCell tc = new TableCell();
Paragraph p = new Paragraph();
Run r = new Run();
Text t = new Text();
t.Text = s;
r.AppendChild(t);
p.AppendChild(r);
tc.AppendChild(p);
tr.AppendChild(tc);
}
// Add image
if (d != null)
{
TableCell tcDrawing = new TableCell();
Paragraph pDrawing = new Paragraph();
Run rDrawing = new Run();
rDrawing.AppendChild(d);
pDrawing.AppendChild(rDrawing);
tcDrawing.AppendChild(pDrawing);
tr.AppendChild(tcDrawing);
}
return tr;
}
备注:这是一个辅助Method。我们生成每一个TableRow的内容,分别用TableCell来存放产品的名称,类别,价钱和图像(Name, Subcategory Name, Price and Drawing)。每个TableCell有是用Paragraph和Run组成的。一个Run是OpenXML中最基本的单位。
14. 将以下程序直接加到上一段程序的后面,也是要在class Program 的closing bracket (}) 里面;为避免手工输入这些语句,可以通过插入代码段 | My Code Snippets | GenerateDrawing.
public static Drawing GenerateDrawing(string relId, string imageName, int width, int height)
{
Drawing element =
new Drawing(
new wp.Inline(
new wp.Extent() { Cx = width, Cy = height },
new wp.EffectExtent() {
LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
new wp.DocProperties() { Id = (UInt32Value)1U, Name = imageName, Description = imageName },
new wp.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 = imageName },
new pic.NonVisualPictureDrawingProperties()),
new pic.BlipFill(
new a.Blip() { Embed = relId, 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 = width, Cy = height }),
new a.PresetGeometry(
new a.AdjustValueList()
) { Preset = a.ShapeTypeValues.Rectangle }))
) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U });
return element;
}
{
Drawing element =
new Drawing(
new wp.Inline(
new wp.Extent() { Cx = width, Cy = height },
new wp.EffectExtent() {
LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
new wp.DocProperties() { Id = (UInt32Value)1U, Name = imageName, Description = imageName },
new wp.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 = imageName },
new pic.NonVisualPictureDrawingProperties()),
new pic.BlipFill(
new a.Blip() { Embed = relId, 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 = width, Cy = height }),
new a.PresetGeometry(
new a.AdjustValueList()
) { Preset = a.ShapeTypeValues.Rectangle }))
) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U });
return element;
}
备注:这也是一个辅助Method。在这段程序中我们根据OpenXML文档规范生成一个Drawing对象。这个Drawing对象会被包含在我们上面生成的TableRow中的最后一个TableCell里面。