ä¸è½½spire.doc, å¨ç¨åºç¨å¼ç¨spire.doc.dll
å并Wordææ¡£ï¼
using Spire.Doc;
namespace Merge_Doc
{
class Program
{
static void Main(string[] args)
{
//è·åæ档路å¾
string filePath_1 = @"C:\Users\Administrator\Desktop\Word_1.docx";
string filePath_2 = @"C:\Users\Administrator\Desktop\Word_2.docx";
//å è½½ææ¡£1å°Document对象
Document doc= new Document(filePath_1);
//使ç¨InsertTextFromFileæ¹æ³å°ææ¡£2å并å°æ°ææ¡£
doc.InsertTextFromFile(filePath_2, FileFormat.Docx2013);
//ä¿åææ¡£
doc.SaveToFile("å并ææ¡£.docx", FileFormat.Docx2013);
}
}
}
æåWordææ¡£ï¼
1ãæåè符æåææ¡£
using Spire.Doc;
namespace Split_Doc
{
class Program
{
static void Main(string[] args)
{
//å®ä¾åDocument对象
Document document = new Document();
//è½½å
¥å¾
æåçWordææ¡£
document.LoadFromFile("æµè¯ææ¡£.docx");
Document newWord;
for (int i = 0; i < document.Sections.Count; i++)
{
//æ¯æä¸ä¸ªsectionå°±å建ä¸ä¸ªæ°çææ¡£
newWord = new Document();
//å¤å¶sectionå
容å°æ°ææ¡£
newWord.Sections.Add(document.Sections[i].Clone());
//ä¿åææ¡£
newWord.SaveToFile(String.Format("åè符æåçç»æææ¡£_{0}.docx", i));
}
}
}
}
2ãæå页符æåææ¡£
using Spire.Doc;
using Spire.Doc.Documents;
namespace Split_Doc
{
class Program
{
static void Main(string[] args)
{
//å®ä¾åDocument对象
Document original = new Document();
//è½½å
¥å¾
æåçWordææ¡£
original.LoadFromFile(@"C:\Users\Administrator\Desktop\template.docx");
//å®ä¾åä¸ä¸ªæ°çæ档并添å æ°ç« è
Document newWord = new Document();
Section section = newWord.AddSection();
int index = 0;
//æ ¹æ®ç« èï¼æ®µè½çå±æ¬¡ç±å¤§å°å°ä¾æ¬¡éåææ¡£å
ç´ ï¼å¤å¶å
容å°æ°çææ¡£
foreach (Section sec in original.Sections)
{
foreach (DocumentObject obj in sec.Body.ChildObjects)
{
if (obj is Paragraph)
{
Paragraph para = obj as Paragraph;
section.Body.ChildObjects.Add(para.Clone());
foreach (DocumentObject parobj in para.ChildObjects)
{
//æ¾å°æ®µè½ä¸çå页符ï¼ä¿åå°æ°ææ¡£
if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak)
{
int i = para.ChildObjects.IndexOf(parobj);
section.Body.LastParagraph.ChildObjects.RemoveAt(i);
newWord.SaveToFile(String.Format("å页符æåçç»æææ¡£_{0}.docx", index), FileFormat.Docx);
index++;
//ä¸ä¸ªææ¡£å®æä¹åæ°å»ºä¸ä¸ªææ¡£
newWord = new Document();
section = newWord.AddSection();
//å¤å¶ä¸ä¸ä¸ªå页符æå¨ç段è½çææå
容å°æ°ææ¡£
section.Body.ChildObjects.Add(para.Clone());
//å¦ææ°æ档第ä¸æ®µï¼ä¹å°±æ¯ååå¤å¶çé£ä¸æ®µï¼æ²¡æåå
ç´ ,
//åæææ¡£ç第ä¸ä¸ªåå
ç´ å é¤
if (section.Paragraphs[0].ChildObjects.Count == 0)
{
section.Body.ChildObjects.RemoveAt(0);
}
else
{
//å¦ææå
容åå é¤å页符ä¹åçææå
容
while (i >= 0)
{
section.Paragraphs[0].ChildObjects.RemoveAt(i);
i--;
}
}
}
}
}
if (obj is Table)
{
section.Body.ChildObjects.Add(obj.Clone());
}
}
}
newWord.SaveToFile(String.Format("å页符æåçç»æææ¡£_{0}.docx", index), FileFormat.Docx);
}
}
}