日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#获取txt,word,excel文档内容方法

發布時間:2025/3/20 C# 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#获取txt,word,excel文档内容方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

??

?? ? 獲取txt文檔的內容?

?

?

?1?public?string?ResumeTxt(string?path)
?2?{
?3????string?str?=?string.Empty;
?4????????
?5????StreamReader?reader?=?new?StreamReader(path,?System.Text.Encoding.Default);
?6????str?=?reader.ReadToEnd();
?7??
?8????//再通過查詢解析出來的的字符串有沒有GB2312?的字段,來判斷是否是GB2312格式的,如果是,則重新以GB2312的格式解析
?9????Regex?reGB?=?new?Regex("GB2312",?RegexOptions.IgnoreCase);
10????Match?mcGB?=?reGB.Match(str);
11????if?(mcGB.Success)
12????{
13????????StreamReader?reader2?=?new?StreamReader(path,?System.Text.Encoding.GetEncoding("GB2312"));
14????????str?=?reader2.ReadToEnd();
15????}
16??
17????return?str;
18?}?19?

?

?

??? 獲取word文檔的內容

?

?1?private?string?ResumeWord(string?path)
?2?{
?3????string?str?=?string.Empty;
?4????Document?myWordDoc;?
?5????Microsoft.Office.Interop.Word.Application?myWordApp?=?new?Microsoft.Office.Interop.Word.ApplicationClass();
?6??
?7????object?filepath?=?path;
?8????object?oMissing?=?Missing.Value;
?9???
10????myWordDoc?=?myWordApp.Documents.Open(ref?filepath,?ref?oMissing,?ref?oMissing,?ref?oMissing,?ref?oMissing,
11??????????ref?oMissing,?ref?oMissing,?ref?oMissing,?ref?oMissing,?ref?oMissing,?ref?oMissing,
12??????????ref?oMissing,?ref?oMissing,?ref?oMissing,?ref?oMissing,?ref?oMissing);
13????str?=?myWordDoc.Content.Text;
14??
15????return?str;
16?}?

?

?

???獲取Excel文檔的內容
?

?1?private?string?ResumeExcel(string?path)
?2?{
?3????string?str?=?string.Empty;
?4????//創建Application對象
?5?????Microsoft.Office.Interop.Excel.Application?xApp?=?new?Microsoft.Office.Interop.Excel.ApplicationClass();
?6????xApp.Visible?=?false;
?7??
?8??
?9????//得到WorkBook對象,
10????Microsoft.Office.Interop.Excel.Workbook?xBook?=?xApp.Workbooks._Open(path,
11??????????Missing.Value,?Missing.Value,?Missing.Value,?Missing.Value,
12??????????Missing.Value,?Missing.Value,?Missing.Value,?Missing.Value,
13??????????Missing.Value,?Missing.Value,?Missing.Value,?Missing.Value);
14??
15????//指定要操作的Sheet:
16????Microsoft.Office.Interop.Excel.Worksheet?xSheet?=?(Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];
17??
18????//讀取,通過Range對象,但使用不同的接口得到Range
19????for?(int?i?=?1;?i?<=?100;?i++)
20????{
21????????for?(int?j?=?1;?j?<=?100;?j++)
22????????{
23????????????Microsoft.Office.Interop.Excel.Range?rng?=?(Microsoft.Office.Interop.Excel.Range)xSheet.Cells[i,?j];
24????????????if?(rng.Value2?!=?null)
25????????????{
26????????????????str?+=?rng.Value2.ToString();
27????????????}
28????????}
29?????}
30??
31??????return?str;
32?}

?

?

?

轉載于:https://www.cnblogs.com/sishierfei/archive/2009/11/25/1610612.html

總結

以上是生活随笔為你收集整理的c#获取txt,word,excel文档内容方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。