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

作者:小菜 更新时间:2025-03-15 点击数:
简介:1.pdf 轉換 txt通過 PDFBox 組件,生成txt文件。

需要下載PDFBox 組件。

2.word excell 轉換txt直接調用相應組件,另存為tx

【菜科解读】

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,轉換,

Win10文件夹设置密码,保护个人隐私

简介:本文将介绍如何在Win10系统中设置文件夹密码,以保护个人隐私。

在现代社会中,个人隐私的保护越来越重要,因此采取措施来保护个人文件和文件夹的安全性至关重要。

通过设置文件夹密码,我们可以有效地防止他人未经授权地访问我们的个人文件,确保个人隐私的安全。

工具原料:电脑品牌型号:Dell XPS 15操作系统版本:Windows 10软件版本:WinRAR 5.91一、Win10文件夹设置密码的方法1、使用WinRAR软件设置密码:步骤1:首先,确保已经安装了WinRAR软件,并且将需要设置密码的文件夹压缩成RAR格式。

步骤2:在文件夹上右键单击,选择“添加到压缩文件”选项。

步骤3:在弹出的WinRAR窗口中,点击“设置密码”按钮。

步骤4:输入所需的密码,并确认密码。

步骤5:点击“确定”按钮,完成文件夹密码的设置。

2、使用BitLocker加密文件夹:步骤1:在需要加密的文件夹上右键单击,选择“属性”选项。

步骤2:在属性窗口中,点击“高级”按钮。

步骤3:在高级属性窗口中,勾选“加密内容以保护数据”选项。

步骤4:点击“确定”按钮,系统将提示是否将文件夹和其内容加密。

步骤5:选择“加密文件夹和文件”选项,点击“确定”按钮。

二、Win10文件夹设置密码的重要性1、保护个人隐私:设置文件夹密码可以有效地保护个人隐私,防止他人未经授权地访问个人文件。

2、防止数据泄露:通过设置密码,可以防止个人文件被他人非法复制、删除或篡改,保护个人数据的安全。

3、防止误操作:有时候我们可能会误操作删除或修改文件,设置密码可以避免这种情况的发生。

总结:通过设置文件夹密码,我们可以有效地保护个人隐私,防止他人未经授权地访问个人文件。

在现代社会中,个人隐私的保护越来越重要,因此采取措施来保护个人文件和文件夹的安全性至关重要。

建议大家在使用Win10系统时,根据自己的需求选择合适的方法来设置文件夹密码,确保个人隐私的安全。

win10文件夹设置密码, win10文件夹密码设置, win10文件夹加密

Word2003插入图片来自文件

Word2003不仅可以编辑处理文字和排版,它还可以插入各种好看图片,让排版和制作文档时更丰富。

插入图片的方法多种,下面Word联盟就教大家如何插入图片。

方法一、打开Word选择 插入-->来自文件,此时你就可以在电脑中选择想插入的图片,点击“插入”按钮。

方法二、直接右键单击“工具栏”将“绘图”勾出来,然后点击 在电脑中想插入的图片。

怎么样操作是不是非常简单,都学会了吗? Word2003,插入,图片,来自,文件,Word2003,

加入收藏
               

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

点击下载文档

格式为doc格式

  • 账号登录
社交账号登录