123 lines
4.4 KiB
C#
123 lines
4.4 KiB
C#
using Microsoft.Office.Interop.Word;
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using Application = Microsoft.Office.Interop.Word.Application;
|
||
|
||
namespace UnitTestProject2
|
||
{
|
||
[TestClass]
|
||
public class WordDocTest
|
||
{
|
||
|
||
|
||
[TestMethod]
|
||
public void Word2Xml()
|
||
{
|
||
|
||
OpenFileDialog dialog = new OpenFileDialog();
|
||
dialog.Filter = "Word|*.docx";
|
||
DialogResult result = dialog.ShowDialog();
|
||
if (result == DialogResult.OK)
|
||
{
|
||
string filepath = dialog.FileName;
|
||
|
||
|
||
//IEnumerable<string> temp = Directory.EnumerateFiles(filepath, "*.docx?");
|
||
//foreach (string path in temp)
|
||
//{
|
||
//FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||
//string inputFile = fileStream.Name;
|
||
string inputFile = dialog.FileName;
|
||
string fileName = System.IO.Path.GetFileNameWithoutExtension(inputFile);
|
||
string fileExs = System.IO.Path.GetExtension(inputFile);
|
||
string fileDirectory = System.IO.Path.GetDirectoryName(inputFile);
|
||
//string fileName = dialog.SafeFileName;
|
||
string outputFile = inputFile.Replace(fileExs,".xml");
|
||
string outpuFileTxt = $"{fileDirectory}/Word2Xml/{fileName}.txt";
|
||
if (Directory.Exists(outputFile) == true)
|
||
{
|
||
File.Delete(outputFile);
|
||
}
|
||
if (Directory.Exists(outputFile) == false)
|
||
{
|
||
Directory.CreateDirectory($"{fileDirectory}/Word2Xml");
|
||
if (!File.Exists(outpuFileTxt))
|
||
{
|
||
using (FileStream fs = new FileStream(outpuFileTxt, FileMode.Create, FileAccess.Write))
|
||
{
|
||
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
|
||
sw.WriteLine();
|
||
|
||
}
|
||
|
||
}
|
||
|
||
string fileType = System.IO.Path.GetExtension(inputFile);
|
||
try
|
||
{
|
||
WordToXml(inputFile, outputFile);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
File.AppendAllText(outpuFileTxt, fileName + e.Message + "\r\n");
|
||
//continue;
|
||
}
|
||
|
||
}
|
||
//}
|
||
}
|
||
}
|
||
|
||
public void WordToXml(string strFileName, string strSaveFileName)
|
||
{
|
||
|
||
Microsoft.Office.Interop.Word.Document WordDoc;
|
||
Object oMissing = System.Reflection.Missing.Value;
|
||
Microsoft.Office.Interop.Word._Application WordApp = new Application();
|
||
object fileName = strFileName;
|
||
|
||
WordDoc = WordApp.Documents.Open(ref fileName,
|
||
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
|
||
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
|
||
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
|
||
|
||
Type wordType = WordApp.GetType();
|
||
// 打开文件
|
||
Type docsType = WordApp.Documents.GetType();
|
||
// 转换格式,另存为
|
||
Type docType = WordDoc.GetType();
|
||
object saveFileName = strSaveFileName;
|
||
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, WordDoc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML });
|
||
|
||
#region 其它格式:
|
||
|
||
///wdFormatHTML
|
||
///wdFormatDocument
|
||
///wdFormatDOSText
|
||
///wdFormatDOSTextLineBreaks
|
||
///wdFormatEncodedText
|
||
///wdFormatRTF
|
||
///wdFormatTemplate
|
||
///wdFormatText
|
||
///wdFormatTextLineBreaks
|
||
///wdFormatUnicodeText
|
||
//-----------------------------------------------------------------------------------
|
||
#endregion
|
||
WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
|
||
WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
|
||
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
}
|