2、正文
代码如下:
string tmppath = Server.MapPath("~/template.doc");
Document doc = new Document(tmppath); //载入模板
if (doc.Range.Bookmarks["name"] != null)
{
Bookmark mark = doc.Range.Bookmarks["name"];
mark.Text = "张三公司";
}
doc.Save("demo.doc", SaveFormat.Doc, SaveType.OpenInWord, Response); //保存为doc,并打开
复制代码 代码如下:
DocumentBuilder builder = new DocumentBuilder(doc);
DataTable products = this.GetData(); //数据源
int count = 0;
//记录要显示多少列
for (var i = 0; i < products.Columns.Count; i++)
{
if (doc.Range.Bookmarks[products.Columns[i].ColumnName.Trim()] != null)
{
Bookmark mark = doc.Range.Bookmarks[products.Columns[i].ColumnName.Trim()];
mark.Text = "";
count++;
}
}
System.Collections.Generic.List<string> listcolumn = new System.Collections.Generic.List<string>(count);
for (var i = 0; i < count; i++)
{
builder.MoveToCell(0, 0, i, 0); //移动单元格
if (builder.CurrentNode.NodeType == NodeType.BookmarkStart)
{
listcolumn.Add((builder.CurrentNode as BookmarkStart).Name);
}
}
double width = builder.CellFormat.Width;//获取单元格宽度
builder.MoveToBookmark("table"); //开始添加值
for (var m = 0; m < products.Rows.Count; m++)
{
for (var i = 0; i < listcolumn.Count; i++)
{
builder.InsertCell(); // 添加一个单元格
builder.CellFormat.Borders.LineStyle = LineStyle.Single;
builder.CellFormat.Borders.Color = System.Drawing.Color.Black;
builder.CellFormat.Width = width;
builder.CellFormat.VerticalMerge = Aspose.Words.Tables.CellMerge.None;
builder.Write(products.Rows[m][listcolumn[i]].ToString());
}
builder.EndRow();
}
doc.Range.Bookmarks["table"].Text = ""; // 清掉标示
doc.Save("baojiadan.doc", SaveFormat.Doc, SaveType.OpenInWord, page.Response);
新闻热点
疑难解答
图片精选