C#獲取Excel文件所有文本數(shù)據(jù)內(nèi)容的示例代碼
功能需求
獲取上傳的 EXCEL 文件的所有文本信息并存儲(chǔ)到數(shù)據(jù)庫(kù)里,可以進(jìn)一步實(shí)現(xiàn)對(duì)文件內(nèi)容資料關(guān)鍵字查詢的全文檢索。有助于我們定位相關(guān)文檔,基本實(shí)現(xiàn)的步驟如下:
1、上傳 EXCEL 文件,獲取二進(jìn)制數(shù)據(jù)并創(chuàng)建副本文件。
2、將EXCEL 副本文件通過(guò) COM API 導(dǎo)出到指定的文本文件。
3、獲取文本文件的內(nèi)容字符串并存儲(chǔ)到數(shù)據(jù)庫(kù)中。
范例運(yùn)行環(huán)境
操作系統(tǒng): Windows Server 2019 DataCenter
操作系統(tǒng)上安裝 Office Excel 2016
數(shù)據(jù)庫(kù):Microsoft SQL Server 2016
.net版本: .netFramework4.7.1 或以上
開發(fā)工具:VS2019 C#
關(guān)鍵代碼
組件庫(kù)引入

獲取Excel文件的文本內(nèi)容
getExcelContent 方法返回 string 類型內(nèi)容,即表示EXCEL 文件的文本內(nèi)容,說(shuō)明如下表:
| 序號(hào) | 參數(shù)名 | 類型 | 說(shuō)明 |
|---|---|---|---|
| 1 | _filename | string | 文件名為全路徑文件信息,方法會(huì)根據(jù)文件路徑創(chuàng)建_path+System.Guid.NewGuid()+".txt" 的臨時(shí)目標(biāo)文件路徑,導(dǎo)入EXCEL文件到 Excel Application ,使用 SAVEAS COM API 導(dǎo)出目標(biāo)文本文件,再獲文本文件內(nèi)容,刪除目標(biāo)文本臨時(shí)文件,將文件內(nèi)容字符串返回。 |
實(shí)現(xiàn)代碼如下:
public string getExcelContent(string _filename)
{
Object Nothing=System.Reflection.Missing.Value;
string _txtfile="",_path=Path.GetDirectoryName(_filename)+"\\",_ext="";
if(!Directory.Exists(_path))
{
Directory.CreateDirectory(_path);
}
_txtfile=_path+System.Guid.NewGuid()+".txt";
object filename=_filename;
//創(chuàng)建一個(gè)名為ExcelApp的組件對(duì)象
DateTime beforetime=DateTime.Now;
Excel.Application excel=new Excel.Application();
excel.DisplayAlerts=false;
excel.AskToUpdateLinks=false;
excel.Visible=true;
DateTime aftertime=DateTime.Now;
Excel.Workbook xb=excel.Workbooks.Add(filename);
Worksheet worksheet = (Worksheet) excel.ActiveSheet;
sheetCount=excel.Sheets.Count;
worksheet.Activate();
worksheet.SaveAs(@_txtfile,XlFileFormat.xlUnicodeText, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
FileEx fe=new FileEx();
excel.Workbooks.Close();
string rv=fe.LoadFromFile(@_txtfile,Encoding.Unicode);
File.Delete(@_txtfile);
excel.Quit();
if(worksheet != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
}
if(xb != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xb);
xb = null;
}
if(excel != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
excel = null;
}
GC.Collect();
KillProcessByStartTime("EXCEL",beforetime,aftertime);
return rv;
}
public string KillProcessByStartTime(string processName,DateTime beforetime,DateTime aftertime)
{
Process[] ps = Process.GetProcesses();
foreach (Process p in ps)
{
if(p.ProcessName.ToUpper()!=processName) continue;
if(p.StartTime > beforetime && p.StartTime < aftertime)
{
try
{
p.Kill();
}
catch(Exception e)
{
return e.Message;
}
}
}
return "";
}其中 KillProcessByStartTime 用于關(guān)閉未釋放的EXCEL應(yīng)用進(jìn)程。
總結(jié)
以上代碼我們提供了一些操作 EXCEL 的API關(guān)鍵方法,后續(xù)我們可以將文本內(nèi)容存儲(chǔ)到數(shù)據(jù)庫(kù)中,查詢或下載,可以參考我的文章:
《C# 將 Word 轉(zhuǎn)文本存儲(chǔ)到數(shù)據(jù)庫(kù)并進(jìn)行管理》
關(guān)于 EXCEL 文件導(dǎo)出方法可參考如下官方文檔:
Worksheet.SaveAs 方法 (Excel) | Microsoft Learn
到此這篇關(guān)于C#獲取Excel文件所有文本數(shù)據(jù)內(nèi)容的示例代碼的文章就介紹到這了,更多相關(guān)C#獲取Excel數(shù)據(jù)內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實(shí)現(xiàn)手機(jī)拍照并且保存水印照片
這篇文章主要介紹了C#實(shí)現(xiàn)手機(jī)拍照并且保存水印照片的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
WPF利用ScottPlot實(shí)現(xiàn)動(dòng)態(tài)繪制圖像
ScottPlot是基于.Net的一款開源免費(fèi)的交互式可視化庫(kù),支持Winform和WPF等UI框架,本文主要為大家詳細(xì)介紹了如何WPF如何使用ScottPlot實(shí)現(xiàn)動(dòng)態(tài)繪制圖像,需要的可以參考下2023-12-12

