Word动态创建表格
Word动态创建表格
var files = Directory.GetFiles(@"C:\Users\dex82\Desktop\造影图片\T2017103026", "*.bmp");
object Nothing = System.Reflection.Missing.Value;
object fileName = Path.Combine(@"D:/", Guid.NewGuid() + ".doc");
Application WordApp = new ApplicationClass();
Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
// 创建表格
int rows = 8; //行数
int cols = 5; //列数
Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, rows, cols, ref Nothing, ref Nothing);
newTable.Columns.Width = 80f; //指定列宽
int execCount = rows / 2;
for (int i = 0; i < execCount; i++)
{
newTable.Rows[i * 2 + 1].Height = 80f; //指定奇数的行高
}
object LinkToFile = false;
object SaveWithDocument = true;
int index = 0;
for (int i = 0; i < execCount; i++)
{
for (int j = 0; j < cols; j++)
{
Cell cell = newTable.Cell(i * 2 + 1, j + 1);
cell.Select();
object Anchor = WordDoc.Application.Selection.Range;
cell.Range.InlineShapes.AddPicture(files[index], ref LinkToFile, ref SaveWithDocument, ref Anchor);
cell.Range.InlineShapes[1].Width = 80;
cell.Range.InlineShapes[1].Height = 80;
index++;
newTable.Cell(i * 2 + 2, j + 1).Range.Text = (index) + "";
}
}
// 文件保存
WordDoc.SaveAs(ref fileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
效果(奇数行为图片,偶数行为文本内容)
秋风
2017-11-21