/// <summary> /// Convert doc or docx to html with picture files /// </summary> /// <param name="inputFileName">Absolute name of input file</param> /// <param name="outputFolder">Folder for saving results</param> /// <param name="newFileName">Relative filename of new html</param> public static void ConvertToHtml(string inputFileName, string outputFolder, string newFileName, Dictionary <string, string> parameters) { if (!Directory.Exists(outputFolder)) { Directory.CreateDirectory(outputFolder); } Spire.Doc.Document document = new Spire.Doc.Document(inputFileName); using (var stream = new MemoryStream()) { document.SaveToStream(stream, Spire.Doc.FileFormat.Html); string result = System.Text.Encoding.UTF8.GetString(stream.ToArray()); if (result.IndexOf(replaceValueDocx) < 0 && result.IndexOf(replaceValueDoc) < 0) { result = System.Text.Encoding.UTF8.GetString(stream.ToArray()).Replace(replaceValueDocx, "").Replace(replaceValueDoc, "").Replace(replaceValue, ""); } else { result = System.Text.Encoding.UTF8.GetString(stream.ToArray()).Replace(replaceValueDocx, "").Replace(replaceValueDoc, ""); } AddImages(document, outputFolder, ref result); File.WriteAllText(outputFolder + "/" + newFileName, result, Encoding.UTF8); result = null; } document.Dispose(); }
/// <summary> /// 添加尾注 /// </summary> /// <param name="filename"></param> /// <param name="str"></param> public void AddWeizhu(string filename, string str) { //获得数据库中str对应的来源 string str_sql = $"select 来源 from {Setting._cijuchachongbiao} where 内容='{Hulue(str.Trim())}'"; Dictionary <string, object> dic = mysqliter.ExecuteRow(str_sql, null, null)[0] as Dictionary <string, object>; string laiyuan = dic["来源"].ToString(); Spire.Doc.Document mydoc = new Spire.Doc.Document(); mydoc.LoadFromFile(filename); TextSelection[] texts = mydoc.FindAllString(str, false, true); if (texts == null) { return; } //给所有查找到的文字添加脚注 foreach (TextSelection item in texts) { //实例化一个脚注的同时,直接创建在指定textselection所在的段落 Spire.Doc.Fields.Footnote footnote = item.GetAsOneRange().OwnerParagraph.AppendFootnote(Spire.Doc.FootnoteType.Endnote); //向脚注内添加文字 footnote.TextBody.AddParagraph().AppendText(laiyuan); //获得这个自然段 Spire.Doc.Documents.Paragraph mypara = item.GetAsOneRange().OwnerParagraph; //向这个段落内的子节点添加脚注 mypara.ChildObjects.Insert(mypara.ChildObjects.IndexOf(item.GetAsOneRange()) + 1, footnote); } mydoc.SaveToFile(filename); mydoc.Dispose(); //去水印 ClearShuiyin(filename); }
public void AddWeizhu(string filename, string str) { Dictionary <string, object> dic = new Dictionary <string, object> { { "内容", this.Hulue(str.Trim()) } }; var list = _sqlhelper.GetAnySet(Setting._cijuchachongbiao, dic); string text = list[0]["来源"].ToString(); Spire.Doc.Document document = new Spire.Doc.Document(); document.LoadFromFile(filename); TextSelection[] selectionArray = document.FindAllString(str, false, true); if (selectionArray != null) { TextSelection[] selectionArray2 = selectionArray; int index = 0; while (true) { if (index >= selectionArray2.Length) { document.SaveToFile(filename); document.Dispose(); this.ClearShuiyin(filename); break; } TextSelection selection = selectionArray2[index]; Spire.Doc.Fields.Footnote entity = selection.GetAsOneRange().OwnerParagraph.AppendFootnote(Spire.Doc.FootnoteType.Endnote); entity.TextBody.AddParagraph().AppendText(text); Paragraph ownerParagraph = selection.GetAsOneRange().OwnerParagraph; ownerParagraph.ChildObjects.Insert(ownerParagraph.ChildObjects.IndexOf(selection.GetAsOneRange()) + 1, entity); index++; } } }
/// <summary> /// Convert doc or docx to html with base64 pictures /// </summary> /// <param name="inputFileName">Absolute name of input file</param> /// <param name="outputFileName">Absolute name of output file</param> public static void ConvertToHtml(string inputFileName, string outputFileName) { Spire.Doc.Document document = new Spire.Doc.Document(inputFileName); using (var stream = new MemoryStream()) { string result = System.Text.Encoding.UTF8.GetString(stream.ToArray()); document.SaveToStream(stream, Spire.Doc.FileFormat.Html); if (result.IndexOf(replaceValueDocx) < 0 && result.IndexOf(replaceValueDoc) < 0) { result = System.Text.Encoding.UTF8.GetString(stream.ToArray()).Replace(replaceValueDocx, "").Replace(replaceValueDoc, "").Replace(replaceValue, ""); } else { result = System.Text.Encoding.UTF8.GetString(stream.ToArray()).Replace(replaceValueDocx, "").Replace(replaceValueDoc, ""); } result = ReplaceImages(document, result); File.WriteAllText(outputFileName, result, Encoding.UTF8); result = null; } document.Dispose(); }
public IActionResult Batchs(string command, string ids) { var response = ResponseModelFactory.CreateInstance; string sWebRootFolder = _hostingEnvironment.WebRootPath + "\\UploadFiles\\ImportUserInfoWord\\"; var timeInfo = DateTime.Now.ToString("yyyyMMddHHmmss"); var wordName = timeInfo + "address.docx"; string wordUrl = sWebRootFolder + wordName; MemoryStream ms = new MemoryStream(); XWPFDocument m_Docx = new XWPFDocument(); m_Docx.Write(ms); ms.Flush(); SaveToFile(ms, wordUrl); List <string> list = new List <string>(); using (_dbContext) { //document111.LoadFromFile(sWebRootFolder + "test.docx"); Document document111 = new Document(); document111.LoadFromFile(wordUrl); Section section = document111.Sections[0]; document111.Watermark = null; section.Paragraphs[0].AppendBookmarkStart("picture"); section.Paragraphs[0].AppendBookmarkEnd("picture"); var parameters = ids.Split(",").Select((id, index) => new SqlParameter(string.Format("@p{0}", index), id)).ToList(); var parameterNames = string.Join(", ", parameters.Select(p => p.ParameterName)); var entities = _dbContext.GrabageRoom.Where(x => ids.IndexOf(x.GarbageRoomUuid.ToString()) >= 0).ToList(); for (int i = 0; i < entities.Count; i++) { var pata = _hostingEnvironment.WebRootPath + EWM.GetEWM2("d_" + entities[i].GarbageRoomUuid.ToString(), _hostingEnvironment, entities[i].Ljname); //实例化BookmarksNavigator类,指定需要添加图片的书签“” BookmarksNavigator bn = new BookmarksNavigator(document111); bn.MoveToBookmark("picture", true, true); //添加段落,加载图片并插入到段落 Section section0 = document111.AddSection(); Spire.Doc.Documents.Paragraph paragraph = section0.AddParagraph(); Image image = Image.FromFile(pata); //DocPicture picture = paragraph.AppendPicture(image); DocPicture picture = document111.Sections[0].Paragraphs[0].AppendPicture(image); picture.Width = 160; picture.Height = 180; //picture.HorizontalPosition = 100.0f; //picture.VerticalPosition = 100.0f; bn.InsertParagraph(paragraph); document111.Sections.Remove(section0); //string output = sWebRootFolder + "test.docx"; document111.SaveToFile(wordUrl, FileFormat.Docx); } list.Add(wordUrl); //关闭进程 document111.Dispose(); var time = DateTime.Now.ToString("yyyyMMddHHmmssfff"); var check = ZIP.CompressMulti(list, _hostingEnvironment.WebRootPath + "\\UploadFiles\\EWM\\" + time, false); if (check) { response.SetSuccess("导出成功"); response.SetData("\\UploadFiles\\EWM\\" + time + ".zip"); } else { response.SetFailed("导出失败"); } return(Ok(response)); } }
public IActionResult yjExportInfo(AddressRequestPayload payload) { var response = ResponseModelFactory.CreateInstance; string sWebRootFolder = _hostingEnvironment.WebRootPath + "\\UploadFiles\\ImportUserInfoWord\\"; var timeInfo = DateTime.Now.ToString("yyyyMMddHHmmss"); var wordName = timeInfo + "address.docx"; string wordUrl = sWebRootFolder + wordName; MemoryStream ms = new MemoryStream(); XWPFDocument m_Docx = new XWPFDocument(); m_Docx.Write(ms); ms.Flush(); SaveToFile(ms, wordUrl); List <string> list = new List <string>(); using (_dbContext) { Document document111 = new Document(); document111.LoadFromFile(wordUrl); Section section = document111.Sections[0]; section.Paragraphs[0].AppendBookmarkStart("picture"); section.Paragraphs[0].AppendBookmarkEnd("picture"); var user = AuthContextService.CurrentUser; string s1 = payload.Kw2; string[] s2 = s1.Split(" "); StringBuilder where = new StringBuilder(); StringBuilder sql = new StringBuilder("SELECT * FROM HomeAddress HA "); for (int i = 0; i < s2.Length; i++) { if (s2.Length > 0 && i == 0) { where.Append("WHERE "); } where.Append("HA.Address LIKE '%" + s2[i] + "%' "); if (i >= 0 && i < s2.Length - 1) { where.Append(" AND "); } } sql.Append(where); //var query = _dbContext.HomeAdd.FromSql(sql.ToString()); var query = _dbContext.HomeAddress.FromSqlRaw(sql.ToString()); if (!string.IsNullOrEmpty(payload.Kw)) { query = query.Where(x => x.Address.ToString().Contains(payload.Kw)); } if (!string.IsNullOrEmpty(payload.Kw1)) { query = query.Where(x => x.Address.ToString().Contains(payload.Kw1)); } var entities = query.ToList(); //var entities = _dbContext.HomeAddress.Where(x => ids.IndexOf(x.HomeAddressUuid.ToString()) >= 0).ToList(); for (int i = 0; i < entities.Count(); i++) { var pata = _hostingEnvironment.WebRootPath + EWM.GetEWM2("h_" + entities[i].HomeAddressUuid.ToString(), _hostingEnvironment, entities[i].Address); //实例化BookmarksNavigator类,指定需要添加图片的书签“” BookmarksNavigator bn = new BookmarksNavigator(document111); bn.MoveToBookmark("picture", true, true); //添加段落,加载图片并插入到段落 Section section0 = document111.AddSection(); Spire.Doc.Documents.Paragraph paragraph = section0.AddParagraph(); Image image = Image.FromFile(pata); DocPicture picture = document111.Sections[0].Paragraphs[0].AppendPicture(image); picture.Width = 160; picture.Height = 180; //picture.HorizontalPosition = 100.0f; //picture.VerticalPosition = 50.0f; bn.InsertParagraph(paragraph); document111.Sections.Remove(section0); //string output = sWebRootFolder + "test.docx"; document111.SaveToFile(wordUrl, FileFormat.Docx); //Arraypata = pata.; //list.Add(pata); } list.Add(wordUrl); //关闭进程 document111.Dispose(); var time = DateTime.Now.ToString("yyyyMMddHHmmssfff"); var check = ZIP.CompressMulti(list, _hostingEnvironment.WebRootPath + "\\UploadFiles\\EWM\\" + time, false); if (check) { response.SetSuccess("导出成功"); response.SetData("\\UploadFiles\\EWM\\" + time + ".zip"); } else { response.SetFailed("导出失败"); } } return(Ok(response)); }