首先设置word模板,可以点击某个单元格插入书签
插入方法:鼠标点击word文件某处,然后 插入--书签 即可
//关键代码如下……………………
string tmppath = Server.MapPath("~/Template.doc");
Document doc = new Document(tmppath); //载入模板
//使用书签
if (doc.Range.Bookmarks["mark"] != null)
{
//Bookmark mark = doc.Range.Bookmarks["mark"];//根据书签名称获取书签
//mark.Text = "sean";
}
//不使用书签 通过移动单元格 来添加值
DocumentBuilder builder = new DocumentBuilder(doc);
//设置单元格内容对齐方式
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.MoveToCell(0, 2, 3,0); //tableIndex,rowIndex,columnIndex,characterIndex 初始索引0
builder.Write("chang");
doc.Save("demo.doc", SaveFormat.Doc, SaveType.OpenInWord, Response); //保存为doc,并打开
//doc.Save(HttpContext.Current.Server.MapPath