private void WriteToWord(string tempName, string fileName) { string _filePath = Server.MapPath(_exportPath) + tempName; XWPFDocument m_Docx; using (FileStream fs = new FileStream(_filePath, FileMode.Open, FileAccess.Read)) { m_Docx = new XWPFDocument(fs); //获得书签 XWPFTable tb = m_Docx.Tables[0]; int i1 = 0; int j = 0; foreach (XWPFTableRow row in tb.Rows) { i1 += 1; foreach (XWPFTableCell cell in row.GetTableCells()) { j = j + 1; if (cell.GetText().IndexOf("姓名") >= 0) { cell.Paragraphs[0].ReplaceText("姓名", "name"); } //cell.SetText(cell.Paragraphs.Count.ToString ()); } j = 0; } XWPFTableCell ce = tb.Rows[0].GetCell(1); //插入图片 var gfs = new FileStream("1.png", FileMode.Open, FileAccess.Read); var gp = m_Docx.CreateParagraph(); gp.Alignment = ParagraphAlignment.CENTER; //居中 var gr = gp.CreateRun(); gr.AddPicture(gfs, (int)PictureType.JPEG, "1.png", 1000000, 1000000); //1000000 差不多100像素多一点 gfs.Close(); List <CT_Bookmark> bkList = new List <CT_Bookmark>(); int pcount = ce.GetXWPFDocument().Document.body.ItemsElementName.Count; for (int i = 0; i < pcount; i++) { var ctp = ce.GetXWPFDocument().Document.body.GetPArray(i); if (ctp != null) { var tempBookMarkList = ctp.GetBookmarkStartList(); bkList.AddRange(tempBookMarkList); } } //int pcount = m_Docx.Document.body.ItemsElementName.Count ; //for (int i = 0; i < pcount; i++) //{ // var ctp = m_Docx.Document.body.GetPArray(i); // if (ctp != null) // { // var tempBookMarkList = ctp.GetBookmarkStartList(); // bkList.AddRange(tempBookMarkList); // //} foreach (var bookMark in bkList) {//替换书签内容?????????????????找不到示例代码还是不支持此功能? bookMark.colFirst = "1"; bookMark.colLast = "2"; bookMark.displacedByCustomXmlSpecified = true; } } //保存到本地文件 _filePath = Server.MapPath(_exportPath) + fileName; using (FileStream fs1 = new FileStream(_filePath, FileMode.Create, FileAccess.Write)) { m_Docx.Write(fs1); } m_Docx = null; divSaveAs.InnerHtml = "<a href='" + _exportPath + fileName + "'>单击下载</a>"; }