• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

c#实现常用文件转换成txt文件代码实现

时间:2015年04月02日 15:31:20    来源:魔法猪系统重装大师官网    人气:16581

1.pdf 轉換 txt

通過 PDFBox 組件,生成txt文件。需要下載PDFBox 組件。

2.word excell 轉換txt

直接調用相應組件,另存為txt。

需要注意:

2.1 word 文檔關閉,需要調用

         object SaveChange = false;
                app.Quit(ref SaveChange, ref obj, ref obj);

2.2 excell 文檔關閉,需要調用

       wbk.Close(Type.Missing, Type.Missing, Type.Missing);

       wst = null;
                wbk = null;
                app.Quit();

在打開excell文檔的時候,賦值2個變量

 app.Visible = false;//打開的excell不可見
 app.DisplayAlerts = false;//不是顯示彈出對話框

3.下面是實現代碼:

3.1 構建IcDocument接口

   public interface IcDocument    {       void TransformDocument();    }

3.2 構建操作基類 BaseDocument

 public abstract class BaseDocument    {        ///         /// 目標文件夾        ///         protected string TargetFolder;        ///         /// 原文件        ///         protected string source;        ///         /// 目標文件        ///         protected string Target;        protected virtual void GetCurrentTarget() {            if (!Directory.Exists(TargetFolder))            {                Directory.CreateDirectory(TargetFolder);            }            string fileName =  Guid.NewGuid().ToString()+".txt";            Target= TargetFolder + @"\" + fileName;        }        public BaseDocument(string TargetFolder, string source)        {            this.source = source;            this.TargetFolder = TargetFolder;            GetCurrentTarget();        }    }

3.3 構建 工程類 FactoryDocument,根據傳入的轉換文檔後綴,生成不同的子類。

 public   class FactoryDocument    {      ///       /// 得到操作的文檔      ///       /// 生成的文件夾      /// 要讀取的文件      ///       public static  IcDocument GetDocoment(string TargetFolder,string source) {          FileInfo file = new FileInfo(source);          IcDocument document = null;          if (file.Exists)          {              switch (Path.GetExtension(source).ToUpper())              {                  case ".PDF":                      document = new PdfDocument(TargetFolder, source);                  break;                  case ".DOC":                  document = new WordDocument(TargetFolder, source);                  break;                  case ".XLS":                  document = new EexcelDocument(TargetFolder, source);                  break;                  default:                  document = new PdfDocument(TargetFolder, source);                      break;              }          }          else {              Console.WriteLine("文件沒有找");          }          return document;      }    }

3.4 構建excell操作類 EexcelDocument : BaseDocument, IcDocument

    public class EexcelDocument : BaseDocument, IcDocument    {        public EexcelDocument(string TargetFolder, string source)            : base(TargetFolder,source )        {                 }        #region IcDocument 成員        public void TransformDocument()        {            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();            app.Visible = false;            app.DisplayAlerts = false;                    Microsoft.Office.Interop.Excel.Workbook wbk = app.Workbooks.Open(source, System.Type.Missing,                System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,                System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,                System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,                System.Type.Missing);            Microsoft.Office.Interop.Excel.Worksheet wst = (Worksheet)wbk.Worksheets[1];            try            {                wbk.SaveAs(Target, XlFileFormat.xlUnicodeText,                    System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,                    XlSaveAsAccessMode.xlNoChange, System.Type.Missing, System.Type.Missing, System.Type.Missing,                    System.Type.Missing, System.Type.Missing);                wbk.Close(Type.Missing, Type.Missing, Type.Missing);                         }            catch (COMException ex)            {                Console.WriteLine(ex.Message);            }            finally            {                wst = null;                wbk = null;                app.Quit();                            GC.Collect();            }                        }        #endregion    }

3.5 構建word 操作類  WordDocument : BaseDocument, IcDocument

  public class WordDocument : BaseDocument, IcDocument    {        public WordDocument(string TargetFolder, string source)            : base(TargetFolder,source )        {                 }        #region IcDocument 成員        public void TransformDocument()        {            Application app = new Application();            Documents Docs = app.Documents;            object obj = Missing.Value;            object FileName = source;            Docs.Open(ref FileName, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj);            Document ad = app.ActiveDocument;            try            {                FileName = Target;                object FileFormat = null;                FileFormat = WdSaveFormat.wdFormatText;                ad.SaveAs(ref FileName, ref FileFormat, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj);                       }            catch (COMException ex)            {                Console.WriteLine(ex.Message);            }            finally            {                object SaveChange = false;                app.Quit(ref SaveChange, ref obj, ref obj);                GC.Collect();            }        }        #endregion    }

3.6 構建pdf 操作類 PdfDocument : BaseDocument,IcDocument

 public class PdfDocument : BaseDocument,IcDocument    {        public PdfDocument(string TargetFolder, string source)            : base(TargetFolder,source )        {                 }      public void pdf2txt(FileInfo file)        {            PDDocument doc = PDDocument.load(file.FullName);            PDFTextStripper pdfStripper = new PDFTextStripper();            string text = pdfStripper.getText(doc);            StreamWriter swPdfChange = new StreamWriter(Target, false, Encoding.GetEncoding(65001));            swPdfChange.Write(text);            swPdfChange.Close();        }      #region IcDocument 成員      public void TransformDocument()      {          FileInfo pdffile = new FileInfo(source);         if (pdffile.Exists)         {             pdf2txt(pdffile);         }         else         {           Console.WriteLine("The File is NOT Exist.");         }      }      #endregion    }

3.7 在程序中使用

     static void Main(string[] args)        {            IcDocument document = FactoryDocument.GetDocoment("c:\\temp", @"C:\Users\Desktop\changes.pdf");            document.TransformDocument();            document = FactoryDocument.GetDocoment("c:\\temp", @"D:\WorkDocuments\201203.xls");            document.TransformDocument();        }

实现,常用,文件,转,换成,txt,代码,1.pdf,轉換,
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部