49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
using DocumentFormat.OpenXml.Packaging;
|
|
using DocumentFormat.OpenXml.Wordprocessing;
|
|
|
|
namespace ETS_Data
|
|
{
|
|
/// <summary>
|
|
/// gestioen documetni office/xml
|
|
/// </summary>
|
|
public class officeXmlMan
|
|
{
|
|
/// <summary>
|
|
/// fa sostituzioni di testo in un doc .docx
|
|
/// </summary>
|
|
/// <param name="fullPathFile"></param>
|
|
/// <param name="textOrig"></param>
|
|
/// <param name="textNew"></param>
|
|
public static void replaceDocxText(string fullPathFile, string textOrig, string textNew)
|
|
{
|
|
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fullPathFile, true))
|
|
{
|
|
#if false
|
|
// lettura
|
|
string docText = null;
|
|
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
|
|
{
|
|
docText = sr.ReadToEnd();
|
|
}
|
|
// sostituzione
|
|
Regex regexText = new Regex(textOrig);
|
|
docText = regexText.Replace(docText, textNew);
|
|
// scrittura
|
|
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
|
|
{
|
|
sw.Write(docText);
|
|
}
|
|
#endif
|
|
// uso questo codice: http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2011/05/12/148357.aspx
|
|
SearchAndReplacer.SearchAndReplace(wordDoc, textOrig, textNew, true);
|
|
}
|
|
}
|
|
}
|
|
}
|