文档检索——从Word、PPT、Excel文件中提取文本
要实现文档检索,首先需要得到纯文本形式的内容,其中一个办法是利用微软提供的类库,将文档另存为文本文件,缺点是需要运行环境
c#控制台程序代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Core; using Microsoft.Office.Interop.Word; using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.PowerPoint; using System.IO; //Office文档转换为Text http://www.cnblogs.com/libsource namespace OfficetoText { class Program { static void Main(string[] args) { //接受两个参数 inputdocfilepath ouputtextfilepath if (args.Length != 2) { Console.WriteLine("参数输入不正确,应为 inputdocfilepath ouputtextfilepath"); return; } string inputfilepath = args[0]; string outputfilepath = args[1]; if (!File.Exists(inputfilepath)) { Console.WriteLine("指定的文件不存在"); return; } FileConvert convert = FileConvertFactory.CreateFileConvert(inputfilepath); convert.Convert(inputfilepath, outputfilepath); } } public interface FileConvert { void Convert(string inputfilepath, string outputfilepath); } public class FileConvertFactory { private FileConvertFactory() { } public static FileConvert CreateFileConvert(string inputfilepath) { FileConvert fileConvert; string extension = Path.GetExtension(inputfilepath); switch (extension.ToLower()) { case (".xls"): case (".xlsx"): fileConvert = new ExcelFileConvert(); break; case (".ppt"): case (".pptx"): fileConvert = new PPTFileConvert(); break; case (".doc"): case (".docx"): default: fileConvert = new DocFileConvert(); break; } return fileConvert; } } public class DocFileConvert : FileConvert { public void Convert(string inputfilepath, string outputfilepath) { Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); object fileName = inputfilepath; object missing = Type.Missing; //打开文档 Document doc = wordApp.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, ref missing, ref missing, ref missing); object saveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatTextLineBreaks; //object saveEncoding = MsoEncoding.msoEncodingUTF8; fileName = outputfilepath; //保存为TXT文档 doc.SaveAs( ref fileName, ref saveFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); doc.Close(ref missing, ref missing, ref missing); wordApp.Quit(ref missing, ref missing, ref missing); } } public class ExcelFileConvert : FileConvert { public void Convert(string inputfilepath, string outputfilepath) { Microsoft.Office.Interop.Excel.ApplicationClass wordApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); object fileName = inputfilepath; object missing = Type.Missing; Workbook doc = wordApp.Workbooks.Open( inputfilepath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); object saveFormat = Microsoft.Office.Interop.Excel.XlFileFormat.xlUnicodeText; object saveResolution = XlSaveConflictResolution.xlLocalSessionChanges; doc.SaveAs( outputfilepath, saveFormat, missing, missing, missing, missing, XlSaveAsAccessMode.xlNoChange, saveResolution, missing, missing, missing, missing); wordApp.Quit(); } } public class PPTFileConvert : FileConvert { public void Convert(string inputfilepath, string outputfilepath) { Microsoft.Office.Interop.PowerPoint.ApplicationClass wordApp = new Microsoft.Office.Interop.PowerPoint.ApplicationClass(); object fileName = inputfilepath; object missing = Type.Missing; Presentation doc = wordApp.Presentations.Open( inputfilepath, MsoTriState.msoCTrue, MsoTriState.msoCTrue, MsoTriState.msoFalse); object saveFormat = PpSaveAsFileType.ppSaveAsRTF; object saveResolution = XlSaveConflictResolution.xlLocalSessionChanges; string tempfile = Path.GetDirectoryName(outputfilepath) + DateTime.Now.ToFileTime() + "tmp.rtf"; //保存为RTF文件 doc.SaveAs(tempfile, PpSaveAsFileType.ppSaveAsRTF, MsoTriState.msoFalse); wordApp.Quit(); //然后转换为txt文件 DocFileConvert newConvert = new DocFileConvert(); newConvert.Convert(tempfile, outputfilepath); File.Delete(tempfile); } } }