c# 常用文檔轉換txt檔案

來源:互聯網
上載者:User

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

View Code

 public abstract class BaseDocument    {        /// <summary>        /// 目標檔案夾        /// </summary>        protected string TargetFolder;        /// <summary>        /// 原檔案        /// </summary>        protected string source;        /// <summary>        /// 目標檔案        /// </summary>        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,根據傳入的轉換文檔後綴,產生不同的子類。

View Code

 public   class FactoryDocument    {      /// <summary>      /// 得到操作的文檔      /// </summary>      /// <param name="TargetFolder">產生的檔案夾</param>      /// <param name="source">要讀取的檔案</param>      /// <returns></returns>      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

View Code

    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

View Code

  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

View Code

 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();        }

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.