首页 > 电脑 > 电脑教程

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

电脑教程 2026-05-18 菜科探索 +
简介: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); 1eak; case ".DOC": document = new WordDocument(TargetFolder, source); 1eak; case ".XLS": document = new EexcelDocument(TargetFolder, source); 1eak; default: document = new PdfDocument(TargetFolder, source); 1eak; } } 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,轉換,

“读脑术”或许很快就能实现 实时解码大脑信息精确率95%

据英国每日邮报报道,目前,科学家最新研制一套计算机程序,能够实时解码人类大脑思考的内容,其精确率达到95%。

  这项研究结果是基于对美国港景医疗中心7位接受护理的癫痫患者观察分析获得的,华盛顿大学医学神经外科医师杰夫-奥杰曼(Jeff Ojemann)称,每位患者曾出现癫痫发作,并且药物治疗并未缓解,因此他们接受了临时性大脑颞叶植入电极手术,持续时间大约1个星期,电极能够定位癫痫发作的聚焦点。

  这个计算机软件每秒1000次采样和数字化大脑信号,从而提取相关特征,同时,该软件还能分析信号数据,从而将电极位置和信号类型结合在一起,分析出人们真实看到的每个图像。

统计结果显示,软件识别图像的精确率高达95%。

拉奥强调称,最新研制的计算机软件有助于实现对瘫痪患者的沟通交流  你或许会认为思想是无形无实,而且无法预测的,但实际上所有的大脑活动都是通过电脉冲信号的传递完成的。

随着对大脑的认识日益加深,我们有望在不久的将来成功解码大脑中的电信号,并将我们的想法转化成自动化设备的驱动力。

  在真实体验中,当体验者戴上头戴设备时,玩具车保持静止。

Sigurnjak称,将字母表形象化或者思考数学问题有助于玩具车的启动,但此时体验者的脑电波信号还不够强烈,玩具车依然静止;

直到体验者听到Sigurnjak博士的解释之后,玩具车才开始移动。

一旦开始移动,玩具车就很难停下来。

体验者尝试不再思考,集中注意力在冥想呼吸上时,玩具车却移动得越来越快。

这意味着,当你试图停下思绪的时候,反而会使大脑的活动更为强烈。

 目前,我们根据大脑扫描结果只能读出是否饥饿或口渴等简单信息,科学家的"读脑术"还处于相当原始的阶段。

不过,破解大脑密码的时刻可能不需要再等待太长时间     科学家把3种颜色的萤光分子分别用于果蝇的三种主要生物系统上:嗅觉、温觉、视觉,然后先让果蝇到处体验不同的经验:闻不同的气味、放在不同温度,看不同东西,然后就是科幻恐怖片登场的时候了,可怜的果蝇将被抓来剖开头壳,挖出大脑,在显微镜下看那些地方出现萤光,由于萤光可持续1 ~ 3个小时,因此科学家可以在3小时之后都能读取果蝇的大脑最后的活动,就跟科幻恐怖片中能读取死前最后记忆一样。

未来百年8大科学未解之谜:能否实现时空旅行

据国外媒体报道,在2000年,美国《大众机械网站》列举了可能在22世纪初才能揭晓的8项最具深度的科学之谜,十年过去了,这些科学之谜获得了哪些重大突破?日前,该网站再次深度剖析这些科学之谜的最新进展。

  能否实现时空旅行?   在许多科幻电影中均表现出了未来科学探索的展望,比如:神秘的外星生命、无法想像的时空旅行等。

目前,光速的极限瓶颈仍是科学界研究的一个重要主题,时空旅行看似更加遥不可及。

  相对论表明,一个人如果高速运动着,时间对他来说就会变慢;

如果他的运动速度趋近于光速,时间对他来说就会趋近于停滞——以光速运行就可以永生。

那么再进一步,如果运动的速度超过光速(尽管相对论假定这是不可能的),会发生什么情况?推理表明,时间就会倒转,人就能够回到过去——这就有点像时间机器。

当然这只是从理论上来说是如此,因为事实上,人类至今所能做到的最快的旅行,其速度也是远远小于光速的。

  在天体物理学中,"虫洞" 的意思,按照史蒂芬-霍金在《时间简史》中的通俗解释是这样的:"虫洞就是一个时空细管,它能把几乎平坦的相隔遥远的区域连接起来。

……因此,虫洞正和其他可能的超光速旅行方式一样,允许人们往过去旅行。

"说得更直白一点,就是从理论上说,虫洞可以让人从一个世界(时空)到达另一个世界。

这种想法,当然是科幻电影最欢迎的思想资源之一。

电影《回到未来》、《2009迷失记忆》、《时间线》等等的故事结构,就是在此基础上建立起来的。

  通过虫洞前往过去或未来世界,毕竟是一个相当抽象的概念。

《时间简史》之类的书中所画的虫洞图像,通常是两片平行宇宙,代表两个不同的时空,也即两个不同的世界,中间有圆锥状的通道,那个通道就是虫洞,也就是所谓的"爱因斯坦-罗森桥"。

目前,科学家认为,有可能存在着可以通过的爱因斯坦-罗森桥,这时它就变成一个"虫洞"——通往另一宇宙的通道。

"虫洞"可以进行时间旅行,还能够通往不同的空间。

猜你喜欢

win10死机怎么办
win10死机怎么办
电脑教程 2026-05-30
enable电脑上是什么意思?
enable电脑上是什么意思?
电脑教程 2026-05-28
asus电脑是哪个品牌
asus电脑是哪个品牌
电脑教程 2026-05-20
首雨180插卡收音机拆机图文
首雨180插卡收音机拆机图文
电脑教程 2026-05-18

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

点击下载文档

格式为doc格式