C# 各種檔案的讀取方法記事本和pdf,word

來源:互聯網
上載者:User
 #region 檔案操作        /// <summary>        /// 讀取記事本        /// </summary>        /// <param name="file_model"></param>        /// <returns></returns>        private HotLine12345.Model.KL_CONTENT_INFO Read_Txt(HotLine12345.Model.KL_FILE file_model)        {            HotLine12345.Model.KL_CONTENT_INFO model = new HotLine12345.Model.KL_CONTENT_INFO();            model.KL_CONTENT_ID = Convert.ToInt32(file_model.KL_ID);            model.KL_CONTENT_TITLE = file_model.File_Name;            model.KL_FILE = file_model.ID.ToString();            #region 讀取記事本            string inforContent = "";            string FilePath = Server.MapPath(file_model.File_Path);            FileInfo file = new FileInfo(FilePath);            if (file.Exists)            {                try                {                    FileStream stream = new FileStream(FilePath, FileMode.Open);                    StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("gb2312"));//Encoding.GetEncoding("gb2312"),Encoding.UTF8                    //reader.ReadLine(); //一次性讀取一行                    inforContent = reader.ReadToEnd();  //一次性讀取全部資料                    reader.Close();                    stream.Close();                }                catch (Exception ce)                {                    throw ce;                }            }            #endregion            model.KL_CONTENT = inforContent;            return model;        }        /// <summary>        /// 讀取word文檔        /// </summary>        /// <param name="file_model"></param>        /// <returns></returns>        private HotLine12345.Model.KL_CONTENT_INFO Read_Word(HotLine12345.Model.KL_FILE file_model)        {            HotLine12345.Model.KL_CONTENT_INFO model = new HotLine12345.Model.KL_CONTENT_INFO();            model.KL_CONTENT_ID = Convert.ToInt32(file_model.KL_ID);            model.KL_CONTENT_TITLE = file_model.File_Name;            model.KL_FILE = file_model.ID.ToString();            #region 讀取word文檔            string inforContent = "";            string FilePath = Server.MapPath(file_model.File_Path);            FileInfo file = new FileInfo(FilePath);            if (file.Exists)            {                try                {                    Word.Application app = new Microsoft.Office.Interop.Word.Application();                    Word.Document doc = null;                    object unknow = Type.Missing;                    app.Visible = false;                    app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;                     object file2 = FilePath;                    doc = app.Documents.Open(ref file2, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);                    inforContent = doc.Content.Text;                    //關閉檔案                    doc.Close(ref unknow, ref unknow, ref unknow);                    //關閉COM                    app.Quit(ref unknow, ref unknow, ref unknow);                }                catch (Exception ce)                {                    throw ce;                }            }            #endregion            model.KL_CONTENT = inforContent;            return model;        }        /// <summary>        /// 讀取PDF內容        /// </summary>        /// <param name="file_model"></param>        /// <returns></returns>        private HotLine12345.Model.KL_CONTENT_INFO Read_PDF(HotLine12345.Model.KL_FILE file_model)        {            HotLine12345.Model.KL_CONTENT_INFO model = new HotLine12345.Model.KL_CONTENT_INFO();            model.KL_CONTENT_ID = Convert.ToInt32(file_model.KL_ID);            model.KL_CONTENT_TITLE = file_model.File_Name;            model.KL_FILE = file_model.ID.ToString();            #region 讀取PDF內容            string inforContent = "";            string FilePath = Server.MapPath(file_model.File_Path);            string txtPath = IndexDic + "\\" + Path.GetFileNameWithoutExtension(FilePath) + ".txt";            FileInfo file = new FileInfo(FilePath);            if (file.Exists)            {                try                {                    PDDocument doc = PDDocument.load(FilePath);                    PDFTextStripper pdfStripper = new PDFTextStripper();                    inforContent = pdfStripper.getText(doc);                    doc.close();                    //                }                catch (Exception ce)                {                    throw ce;                }            }            #endregion            model.KL_CONTENT = inforContent;            return model;        }        #endregion
<span style="color:#cc0000;background-color: rgb(255, 255, 255);">pdf請下載相關組件PDFBox-0.7.3.dllIKVM.Runtime.dllIKVM.GNU.Classpath.dllFontBox-0.1.0-dev.dll</span>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.