本篇介紹的是如何在C#中往word里面寫入數據。
如何在線的操作文檔: c#在線操作文檔
關於Aspose.Word控件的介紹,請戳→ 介紹
首先需要去下載這個dll文件,然后引用到你的項目當中。地址→查看下載地址
附一個輔助類來操作此dll
利用書簽把數據寫入到Word當中
文本和圖片
第一步,你先要去准備Word模板(新建一個Word然后復制到項目中)
第二步,在Word模板中插入書簽
在word中准備你需要把數據填寫到那個位子,(Text:我所傳進來的文本所在位子 Img:傳進來的圖片所在位子)

然后我們就可以在word中插入書簽了

插入Img標簽完成后,定位在img把img刪除,我們只是需要定位。
我們的頁面

<div>寫文字</div>
<label for="txtText">你想寫的數據:</label> <input type="text" name="txtText" runat="server" id="txtText" />
<hr />
<div>插入圖片</div>
<label>上傳你的圖片:</label>
<asp:FileUpload ID="fuUpload" runat="server" />
<br />
后台

protected void btnSubmit_Click(object sender, EventArgs e) { if (fuUpload.HasFile) { string fileName = ""; string SavaPath = Server.MapPath("~/img/"); string ext = Path.GetExtension(fuUpload.FileName); List<string> extList = new List<string>() { ".jpg", ".png", ".jpeg", ".gif", ".bmp" }; if (extList.Any(x => x == ext)) { string random = Guid.NewGuid().ToString().Substring(0, 4); fileName = random + ext; fuUpload.SaveAs(SavaPath + fileName); //圖片保存到項目中 //寫入word string FilePath = "WriteWord+" + new Random().Next(0, 99) + ".doc"; System.IO.File.Copy(Server.MapPath("~/WriteWord.doc"), FilePath, true); //復制 System.IO.File.SetAttributes(FilePath, System.IO.FileAttributes.Normal);//設置文件屬性 只讀 還是可修改
Document doc = new Document(FilePath); //這段代碼可以封裝,用的時候調用就行 //if (doc.Range.Bookmarks["text"] != null) //{ // Bookmark mark = doc.Range.Bookmarks["text"]; // mark.Text=txtText.Value.Trim(); //} BookAddMark(doc, "text", txtText.Value.Trim()); //把文本寫入到指定標簽的位子 DocumentBuilder builder = new DocumentBuilder(doc); //builder里面有個write方法 builder.MoveToBookmark("img"); //找到你圖片插入的位子,定位到這里 //builder.InsertHtml("<img scr='~/img/"+ fileName + "'>"); 這樣word里面是讀不出圖片的 //InserImage()有很多重載它需要什么你就給什么進去 // builder.InsertImage(Server.MapPath("~/img/" + fileName)); //第一種,直接給路徑,當設置不了寬高 //圖片的路徑要找對 FileStream fs = new FileStream(Server.MapPath("~/img/" + fileName), FileMode.Open); byte[] imgByte = new byte[fs.Length]; fs.Read(imgByte, 0, imgByte.Length); builder.InsertImage(imgByte, 200, 200); //以字節組的方式寫入word fs.Close(); builder = null; doc.Save(Server.MapPath("~/Word/" + FilePath)); //保存word文檔 } else { Page.ClientScript.RegisterStartupScript(Page.ClientScript.GetType(), "myscript", "<script>alert('上傳的文件類型不支持');</script>"); } } else { Page.ClientScript.RegisterStartupScript(Page.ClientScript.GetType(), "myscript", "<script>alert('請選擇你所需要上傳的文件');</script>"); } }
public void BookAddMark(Document doc, string parama, string paramb) { if (doc.Range.Bookmarks[parama] != null) { Bookmark mark = doc.Range.Bookmarks[parama]; mark.Text = paramb; } }
word里面生成的數據如下

如果需要寫一個集合里面的數據,用書簽就不是很方法,因為我們不能控制集合的數量,拿我們就會用到上面說的wirte方法,配合表格插入數據
我們在word里面生成一個表格
例:我們需要生成很多數據

我們可以先把模板表格確定好

這就是我們的模板
再來看我們是如何寫數據
protected void btnWriteData_Click(object sender, EventArgs e) { string FilePath = "WriteWord+" + new Random().Next(0, 99) + ".doc"; System.IO.File.Copy(Server.MapPath("~/WriteWord.doc"), FilePath, true); //復制 System.IO.File.SetAttributes(FilePath, System.IO.FileAttributes.Normal);//設置文件屬性 只讀 還是可修改 Document doc = new Document(FilePath); List<Person> perList = new List<Person>() { new Person(){ Name="張三", Age="18", sex="男"}, new Person(){ Name="李四", Age="18", sex="男"}, new Person(){ Name="王五", Age="18", sex="男"}, new Person(){ Name="趙六", Age="18", sex="男"}, new Person(){ Name="……", Age="……", sex="……"} }; DocumentBuilder builder = new DocumentBuilder(doc); NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true); //拿到所有表格 Aspose.Words.Tables.Table table = allTables[0] as Aspose.Words.Tables.Table; //拿到第1個表格 foreach (Person item in perList) { var row = table.Rows[table.Rows.Count - 1].Clone(true); //復制最后一行 table.Rows.Insert(table.Rows.Count - 1, row); //插入到這行的上面 builder.MoveToCell(0, table.Rows.Count - 2, 0, 0); //移動單元格到第幾行第幾列 builder.Write(item.Name); //寫入數據 builder.MoveToCell(0, table.Rows.Count - 2, 1, 0); builder.Write(item.Age); builder.MoveToCell(0, table.Rows.Count - 2, 2, 0); builder.Write(item.sex); } builder = null; doc.Save(Server.MapPath("~/Word/" + FilePath)); } private class Person { public string Name { get; set; } public string Age { get; set; } public string sex { get; set; } }
生成的Word里面的表格如下:

