Fork me on GitHub
c# 常用文檔轉換txt文件

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

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

View Code

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

View Code

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

View Code

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

View Code

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();
        }
复制代码

 

 

 
分类: C#
posted on 2012-05-28 18:17  HackerVirus  阅读(240)  评论(0编辑  收藏  举报