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,并打開
復(fù)制代碼 代碼如下:
DocumentBuilder builder = new DocumentBuilder(doc);
DataTable products = this.GetData(); //數(shù)據(jù)源
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 = ""; // 清掉標(biāo)示
doc.Save("baojiadan.doc", SaveFormat.Doc, SaveType.OpenInWord, page.Response);

新聞熱點
疑難解答
圖片精選