我用Aspose.Words组件导出word,文字啥的都可以正常导出,但是之前录入数据时用的编辑器,有的带图,所以数据库字段的值就变成
<span style="color:#676A6C;font-family:"font-weight:600;background-color:#FFFFFF;"> 主要研究内容2</span><img src="/Files\image/20180119/20180119133748_9400.jpg" alt="" />
这样就没法识别样式啊图片啊啥的,求大神告诉我如何才能把文字和图片都导出来,样式无所谓,下面时我目前导出word的代码
public ActionResult XZJBXX(int guid=-1) { Sys_Project pro = db.Sys_Project.Find(guid); string tmppath = Server.MapPath("~/Files/交通运输科技项目申请书.doc");//路径 Aspose.Words.Document doc = new Aspose.Words.Document(tmppath); Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc); //成果名称 if (doc.Range.Bookmarks["CDDW"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["CDDW"]; if (pro.CDDW != null && pro.CDDW != "") { mark.Text = pro.CDDW; } } if (doc.Range.Bookmarks["CDDWDFG"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["CDDWDFG"]; if (pro.CDDWDFG != null && pro.CDDWDFG != "") { mark.Text = pro.CDDWDFG; } } if (doc.Range.Bookmarks["DDDKXJSSP"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["DDDKXJSSP"]; if (pro.DDDKXJSSP != null && pro.DDDKXJSSP != "") { mark.Text = pro.DDDKXJSSP; } } if (doc.Range.Bookmarks["GNWXZSPFZ"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["GNWXZSPFZ"]; if (pro.GNWXZSPFZ != null && pro.GNWXZSPFZ != "") { mark.Text = pro.GNWXZSPFZ; } } if (doc.Range.Bookmarks["JDMB"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["JDMB"]; if (pro.JDMB != null && pro.JDMB != "") { mark.Text = pro.JDMB; } } if (doc.Range.Bookmarks["JFGS"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["JFGS"]; if (pro.JFGS != null && pro.JFGS != "") { mark.Text = pro.JFGS; } } if (doc.Range.Bookmarks["ZYYJNR"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["ZYYJNR"]; if (pro.ZYYJNR != null && pro.ZYYJNR != "") { mark.Text = pro.ZYYJNR; } } if (doc.Range.Bookmarks["JSSJshort"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["JSSJshort"]; if (pro.JSSJshort!=null&&pro.JSSJshort!="") { mark.Text = pro.JSSJshort; } } if (doc.Range.Bookmarks["KSSJshort"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["KSSJshort"]; if (pro.KSSJshort != null && pro.KSSJshort != "") { mark.Text = pro.KSSJshort; } } if (doc.Range.Bookmarks["KTFZR"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["KTFZR"]; try { if (pro.KTFZR != null && pro.KTFZR != "") { mark.Text = pro.KTFZR; } } catch { } } if (doc.Range.Bookmarks["KTZZYCY"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["KTZZYCY"]; try { if (pro.KTZZYCY != null && pro.KTZZYCY != "") { mark.Text = pro.KTZZYCY; } } catch { } } if (doc.Range.Bookmarks["Name"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["Name"]; try { if (pro.Name != null && pro.Name != "") { mark.Text = pro.Name; } } catch { } } if (doc.Range.Bookmarks["NDJHJD"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["NDJHJD"]; try { if (pro.NDJHJD != null && pro.NDJHJD != "") { mark.Text = pro.NDJHJD; } } catch { } } if (doc.Range.Bookmarks["SBSJ"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["SBSJ"]; try { if (pro.SBSJ != null && pro.SBSJ != "") { mark.Text = pro.SBSJ; } } catch { } } if (doc.Range.Bookmarks["XYDJSTJJYSYQK"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["XYDJSTJJYSYQK"]; try { if (pro.XYDJSTJJYSYQK != null && pro.XYDJSTJJYSYQK != "") { mark.Text = pro.XYDJSTJJYSYQK; } } catch { } } if (doc.Range.Bookmarks["XYDYQSB"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["XYDYQSB"]; try { if (pro.XYDYQSB != null && pro.XYDYQSB != "") { mark.Text = pro.XYDYQSB; } } catch { } } if (doc.Range.Bookmarks["XZDW"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["XZDW"]; try { if (pro.XZDW != null && pro.XZDW != "") { mark.Text = pro.XZDW; } } catch { } } if (doc.Range.Bookmarks["XZDWDFG"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["XZDWDFG"]; try { if (pro.XZDWDFG != null && pro.XZDWDFG != "") { mark.Text = pro.XZDWDFG; } } catch { } } if (doc.Range.Bookmarks["YZFF"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["YZFF"]; try { if (pro.YZFF != null && pro.YZFF != "") { mark.Text = pro.YZFF; } } catch { } } if (doc.Range.Bookmarks["ZDYJQXJD"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["ZDYJQXJD"]; try { if (pro.ZDYJQXJD != null && pro.ZDYJQXJD != "") { mark.Text = pro.ZDYJQXJD; } } catch { } } if (doc.Range.Bookmarks["ZGBMSHYJ"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["ZGBMSHYJ"]; try { if (pro.ZGBMSHYJ != null && pro.ZGBMSHYJ != "") { mark.Text = pro.ZGBMSHYJ; } } catch { } } if (doc.Range.Bookmarks["ZHBM"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["ZHBM"]; try { if (pro.ZHBM != null && pro.ZHBM != "") { mark.Text = pro.ZHBM; } } catch { } } if (doc.Range.Bookmarks["ZJPSYJ"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["ZJPSYJ"]; try { if (pro.ZJPSYJ != null && pro.ZJPSYJ != "") { mark.Text = pro.ZJPSYJ; } } catch { } } if (doc.Range.Bookmarks["ZYJSJJZB"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["ZYJSJJZB"]; try { if (pro.ZYJSJJZB != null && pro.ZYJSJJZB != "") { mark.Text = pro.ZYJSJJZB; } } catch { } } if (doc.Range.Bookmarks["ZYYJNR"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["ZYYJNR"]; try { if (pro.ZYYJNR != null && pro.ZYYJNR != "") { mark.Text = pro.ZYYJNR; } } catch { } } if (doc.Range.Bookmarks["ZZMB"] != null) { Aspose.Words.Bookmark mark = doc.Range.Bookmarks["ZZMB"]; try { if (pro.ZZMB != null && pro.ZZMB != "") { System.IO.StringWriter writer = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer); mark.Text = pro.ZZMB; } } catch { } } doc.Save(tmppath, Aspose.Words.SaveFormat.Doc); //return new EmptyResult(); FileStream fs = new FileStream(tmppath, FileMode.Open); // 6. 返回致客服端提供下载 return File(fs, "application/octet-stream", "" + pro.Name + "交通运输科技项目申请书.doc"); //return Json(result); }
/// <summary> /// 提取word 中的图片 /// </summary> /// <param name="filePath">word文件路径</param> /// <param name="savePath">保存文件路径</param> /// <returns></returns> public static List<string> ExportImageFromWordFile(string filePath, string savePath = "") { if (!File.Exists(filePath)) return new List<string>(); if (string.IsNullOrEmpty(savePath)) savePath = AppDomain.CurrentDomain.BaseDirectory; //文件名集合 List<string> list = new List<string>(); //加载word Document doc = new Document(filePath); NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); int imageIndex = 0; foreach (Shape shape in shapes) { if (shape.HasImage) { string time = DateTime.Now.ToString("HHmmssfff"); //扩展名 string ex = FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType); //文件名 string fileName = string.Format("{0}_{1}{2}", time, imageIndex, ex); shape.ImageData.Save(savePath + fileName); //添加文件到集合 list.Add(fileName); imageIndex++; } } return list; }
DocumentBuilder builder = new DocumentBuilder(doc); builder.MoveToBookmark("img");//给img标签处添加内容 //builder.InsertCell();// 添加一个单元格 Shape shape = new Shape(doc, ShapeType.Image); //图片路径 string filePath = "a.png"; shape.ImageData.SetImage(filePath); shape.Width = 80; shape.Height = 100; shape.HorizontalAlignment = HorizontalAlignment.Center; //CompositeNode node = shape.ParentNode; //把此图片移动到那个单元格中 builder.MoveToCell(0, 0, 4, 0); builder.InsertNode(shape);