-
大小: 90KB文件类型: .rar金币: 1下载: 0 次发布日期: 2021-05-12
- 语言: 其他
- 标签:
资源简介
涉及知识点:word转xml、xml转字符串、doc转docx
此处主要试讲word文件转换,之前的替换只能替换正文,此处可进行全部替换,像脚注等,都可进行
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Data.OleDb;
using System.xml;
namespace WordReplace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_SelPath_Click(object sender EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = “请选择文件路径“;
if (dialog.ShowDialog() == DialogResult.OK)
{
textBox_SearchPath.Text = dialog.SelectedPath;
}
}
private void btn_BatchReplace_Click(object sender EventArgs e)
{
listBox_FileList.Items.Clear();
if (!Directory.Exists(textBox_SearchPath.Text))
{
MessageBox.Show(“查找路径不存在请确认!“);
return;
}
bool bSearchChildDir = checkBox_SearchChildDir.Checked;
List fileList = ReplaceTextInRecursiveDir(textBox_SearchPath.Text bSearchChildDir);
foreach (var file in fileList)
{
toolStripStatusLabel1.Text = “正在处理:“ + file;
//将word文件转换为xml文件
var xmlName = file.Replace(“docx“ “xml“);
ConvertWordPDF(file xmlName);
//将xml转为字符串,并进行替换
xmlDocument xmldoc = new xmlDocument();
xmldoc.Load(xmlName);
string xmlString = ConvertxmlToString(xmldoc);
foreach (var item in ContrastFont())
{
xmlString = xmlString.Replace(item.Key item.Value);
}
//将字符串转换为xml再转换为doc文件
xmlDocument newxmlDoc = ConvertStringToxml(xmlString);
newxmlDoc.Save(xmlName);
string content = System.IO.File.ReadAllText(xmlName System.Text.Encoding.UTF8);
StreamWriter sw = new StreamWriter(file.Replace(“docx“ “doc“));
sw.Write(content);
sw.Flush();
sw.Close();
//删除xml文件
File.Delete(xmlName);
object missing = Type.Missing;
object fileName = file.Replace(“docx“ “doc“);
object Target = file;
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
word.Documents.Open(ref fileName
ref missing ref missing ref missing ref missing ref missing
ref missing ref missing ref missing ref missing ref missing
ref missing ref missing
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 27648 2018-04-23 15:03 WordReplace\bin\Debug\WordReplace.exe
文件 28160 2018-04-23 15:03 WordReplace\bin\Debug\WordReplace.pdb
文件 19968 2018-04-04 16:40 WordReplace\bin\Release\WordReplace.exe
文件 30208 2018-04-04 16:40 WordReplace\bin\Release\WordReplace.pdb
文件 4286 2018-04-16 16:09 WordReplace\bitbug_favicon.ico
文件 13750 2018-04-20 09:46 WordReplace\Form1.cs
文件 9219 2018-04-16 16:10 WordReplace\Form1.Designer.cs
文件 12749 2018-04-16 16:10 WordReplace\Form1.resx
文件 2494 2018-04-13 17:00 WordReplace\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
文件 7452 2018-04-16 16:09 WordReplace\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 42 2018-04-16 16:09 WordReplace\obj\x86\Debug\WordReplace.csproj.CoreCompileInputs.cache
文件 1717 2018-04-20 09:43 WordReplace\obj\x86\Debug\WordReplace.csproj.FileListAbsolute.txt
文件 1012 2018-04-16 16:11 WordReplace\obj\x86\Debug\WordReplace.csproj.GenerateResource.cache
文件 972 2018-04-16 16:09 WordReplace\obj\x86\Debug\WordReplace.csproj.ResolveComReference.cache
文件 42042 2018-04-16 18:02 WordReplace\obj\x86\Debug\WordReplace.csprojResolveAssemblyReference.cache
文件 27648 2018-04-23 15:03 WordReplace\obj\x86\Debug\WordReplace.exe
文件 4852 2018-04-16 16:11 WordReplace\obj\x86\Debug\WordReplace.Form1.resources
文件 28160 2018-04-23 15:03 WordReplace\obj\x86\Debug\WordReplace.pdb
文件 180 2018-04-16 16:09 WordReplace\obj\x86\Debug\WordReplace.Properties.Resources.resources
文件 2494 2018-04-04 09:19 WordReplace\obj\x86\Release\DesignTimeResolveAssemblyReferences.cache
文件 7412 2018-04-04 09:19 WordReplace\obj\x86\Release\DesignTimeResolveAssemblyReferencesInput.cache
文件 42 2018-04-04 15:01 WordReplace\obj\x86\Release\WordReplace.csproj.CoreCompileInputs.cache
文件 1892 2018-04-04 16:40 WordReplace\obj\x86\Release\WordReplace.csproj.FileListAbsolute.txt
文件 1012 2018-04-04 15:01 WordReplace\obj\x86\Release\WordReplace.csproj.GenerateResource.cache
文件 972 2018-04-04 15:01 WordReplace\obj\x86\Release\WordReplace.csproj.ResolveComReference.cache
文件 8541 2018-04-04 16:40 WordReplace\obj\x86\Release\WordReplace.csprojResolveAssemblyReference.cache
文件 19968 2018-04-04 16:40 WordReplace\obj\x86\Release\WordReplace.exe
文件 180 2018-04-04 15:01 WordReplace\obj\x86\Release\WordReplace.Form1.resources
文件 30208 2018-04-04 16:40 WordReplace\obj\x86\Release\WordReplace.pdb
文件 180 2018-04-04 15:01 WordReplace\obj\x86\Release\WordReplace.Properties.Resources.resources
............此处省略21个文件信息
评论
共有 条评论