diff --git a/.gitignore b/.gitignore
index 8796d56c7bb31d89be96e3d321e73ba8d984a4fe..9ac242371b55bf294aac29959dc8af1dbdbd8456 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
/TextLocator/bin
/TextLocator/obj
/packages
+/.vs/TextLocator/v16/*.suo
+/.vs
diff --git a/.vs/TextLocator/v16/.suo b/.vs/TextLocator/v16/.suo
deleted file mode 100644
index 7ad29fa39e3143f141a24b0f9dae99ff35255bb1..0000000000000000000000000000000000000000
Binary files a/.vs/TextLocator/v16/.suo and /dev/null differ
diff --git a/README.md b/README.md
index dd157698c961d14882cffef8ec92e9670c43c6ed..b2d48e8a88eb5e40863249f48bfe5845b5f47dd6 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,5 @@
# 文本定位器
-
+
#### 介绍
基于.net实现的本地文档的全文索引定位器,根据关键词搜索定位本地文档内容。便于查找历史文档时节省时间。
diff --git a/TextLocator/Core/AppCore.cs b/TextLocator/Core/AppCore.cs
new file mode 100644
index 0000000000000000000000000000000000000000..ad88dfbbb19b64c79211e81e321c7c67647b4c89
--- /dev/null
+++ b/TextLocator/Core/AppCore.cs
@@ -0,0 +1,46 @@
+using log4net;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace TextLocator.Core
+{
+ ///
+ /// AppCore
+ ///
+ public class AppCore
+ {
+ private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
+
+ #region App退出或重启
+ ///
+ /// 退出应用
+ ///
+ public static void ExitProcess()
+ {
+ log.Debug("退出当前应用程序进程");
+ Environment.Exit(Environment.ExitCode); //Application.Current.Shutdown(-1);
+ }
+
+ ///
+ /// 重启应用
+ ///
+ public static void RestartProcess()
+ {
+ log.Debug("重启当前应用程序");
+
+ // 启动新进程
+ System.Reflection.Assembly.GetEntryAssembly();
+ String processPath = Directory.GetCurrentDirectory();
+ String processName = Process.GetCurrentProcess().ProcessName;
+ Process.Start(processPath + "/" + processName);
+
+ ExitProcess();
+ }
+ #endregion
+ }
+}
diff --git a/TextLocator/FileInfoItem.xaml.cs b/TextLocator/FileInfoItem.xaml.cs
index f10c88ca4d1ec1355161e4dfd87aacbd19341883..510fe716f067ce528225068a750fb6c5ec1f7587 100644
--- a/TextLocator/FileInfoItem.xaml.cs
+++ b/TextLocator/FileInfoItem.xaml.cs
@@ -45,29 +45,12 @@ namespace TextLocator
public void Refresh(Entity.FileInfo fileInfo)
{
// 根据文件类型显示图标
- this.FileTypeIcon.Source = FileUtil.GetFileTypeIcon(fileInfo.FileType);
+ this.FileTypeIcon.Source = FileUtil.GetFileIcon(fileInfo.FileType);
// 显示文件信息
this.FileName.Text = fileInfo.FileName;
this.FileFolder.Text = fileInfo.FilePath.Replace(fileInfo.FileName, "");
- long fileSize = fileInfo.FileSize;
- string fileSizeUnit = "b";
- if (fileSize > 1024)
- {
- fileSize = fileSize / 1024;
- fileSizeUnit = "KB";
- }
- if (fileSize > 1024)
- {
- fileSize = fileSize / 1024;
- fileSizeUnit = "MB";
- }
- if (fileSize > 1024)
- {
- fileSize = fileSize / 1024;
- fileSizeUnit = "GB";
- }
- this.FileSize.Text = fileSize + "" + fileSizeUnit;
+ this.FileSize.Text = FileUtil.GetFileSizeFriendly(fileInfo.FileSize);
this.CreateTime.Text = fileInfo.CreateTime;
this.FileContent.Document.Blocks.Clear();
diff --git a/TextLocator/Index/LuceneIndexCore.cs b/TextLocator/Index/LuceneIndexCore.cs
index f7af428567394c8b0a5615ebe94655661f371a0e..9c905ec48c541b2121a9dcc270ae0a54ed20c920 100644
--- a/TextLocator/Index/LuceneIndexCore.cs
+++ b/TextLocator/Index/LuceneIndexCore.cs
@@ -37,11 +37,12 @@ namespace TextLocator.Index
if (rebuild)
{
create = rebuild;
- }
+ }
// 索引写入初始化(FSDirectory表示索引存放在硬盘上,RAMDirectory表示放在内存上)
Lucene.Net.Index.IndexWriter writer = new Lucene.Net.Index.IndexWriter(AppConst.INDEX_DIRECTORY, AppConst.INDEX_ANALYZER, create, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);
+ // 文件总数
int count = filePaths.Count();
// 遍历读取文件,并创建索引
@@ -54,6 +55,13 @@ namespace TextLocator.Index
{
continue;
}
+ if (!create && !string.IsNullOrEmpty(AppUtil.ReadIni("FileIndex", filePath, "")))
+ {
+ continue;
+ }
+
+ // 写入
+ AppUtil.WriteIni("FileIndex", filePath, "1");
// 开始时间
DateTime beginMark = DateTime.Now;
diff --git a/TextLocator/MainWindow.xaml b/TextLocator/MainWindow.xaml
index 8647b2636963cd222a47ba1a17d0749329da8753..a3c069f7974f564f39ac97dc175c0f5d101fe161 100644
--- a/TextLocator/MainWindow.xaml
+++ b/TextLocator/MainWindow.xaml
@@ -6,7 +6,7 @@
xmlns:local="clr-namespace:TextLocator"
xmlns:rubyer="clr-namespace:Rubyer;assembly=Rubyer"
mc:Ignorable="d"
- Title="搜索文本定位" Width="1366" Height="768" WindowStartupLocation="CenterScreen" Icon="Resource/App.ico" Loaded="Window_Loaded">
+ Title="文本搜索定位" Width="1366" Height="768" WindowStartupLocation="CenterScreen" Icon="Resource/App.ico" Loaded="Window_Loaded">
@@ -87,7 +87,7 @@
-
+
@@ -192,11 +192,14 @@
-
-
-
-
- 网站维护合同书
+
+
+
+
+
+
+
+ 网站维护合同书
甲方名称:尚迪(上海)铝业有限公司 乙方名称:
联系电话:021-37595166 联系电话:
@@ -213,9 +216,10 @@
甲方要求在当前网站风格、版式以及页面数的基础上,重新增加功能性新的页面或修
改网站风格和版式时,需另外支付费用。
顶目4、乙方为甲方网站的数据库进行维护和管理,包括:对网站数据库进行整理和优化、定期检测数据库的信息的准确性、清除多余数据、定期备份数据库等服务。保证网站数据库数据的合法、完整以及难确。
-
-
-
+
+
+
+
diff --git a/TextLocator/MainWindow.xaml.cs b/TextLocator/MainWindow.xaml.cs
index 1d1e354bc5fd9614da107d818e35ab1f6ed6b21f..f8d3c62f852937247e0b900f5ccef7b7e7f1cbf9 100644
--- a/TextLocator/MainWindow.xaml.cs
+++ b/TextLocator/MainWindow.xaml.cs
@@ -11,7 +11,9 @@ using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
+using System.Windows.Media.Imaging;
using TextLocator.Consts;
+using TextLocator.Core;
using TextLocator.Enums;
using TextLocator.Factory;
using TextLocator.Index;
@@ -159,9 +161,9 @@ namespace TextLocator
///
private void ShowStatus(string text)
{
- this.Dispatcher.InvokeAsync(() => {
+ this.Dispatcher.BeginInvoke(new Action(() => {
this.WorkStatus.Text = text;
- });
+ }));
}
///
@@ -288,7 +290,7 @@ namespace TextLocator
FileInfoItem infoItem = new FileInfoItem(fileInfo);
// 增加点击事件
- infoItem.MouseUp += InfoItem_MouseUp;
+ infoItem.MouseDown += InfoItem_MouseDown;
infoItem.Tag = fileInfo;
this.SearchResultList.Items.Add(infoItem);
@@ -318,13 +320,17 @@ namespace TextLocator
///
///
///
- private void InfoItem_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
+ private void InfoItem_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
+ // 手动GC
+ GC.Collect();
+ GC.WaitForPendingFinalizers();
+
FileInfoItem infoItem = sender as FileInfoItem;
Entity.FileInfo fileInfo = infoItem.Tag as Entity.FileInfo;
// 根据文件类型显示图标
- this.PreviewFileTypeIcon.Source = FileUtil.GetFileTypeIcon(fileInfo.FileType);
+ this.PreviewFileTypeIcon.Source = FileUtil.GetFileIcon(fileInfo.FileType);
this.PreviewFileName.Text = fileInfo.FileName;
this.PreviewFileContent.Document.Blocks.Clear();
@@ -332,24 +338,56 @@ namespace TextLocator
this.OpenFile.Tag = fileInfo.FilePath;
this.OpenFolder.Tag = fileInfo.FilePath.Replace(fileInfo.FileName, "");
- // 文件内容预览
- ThreadPool.QueueUserWorkItem(_ => {
- // 文件内容
- string content = FileInfoServiceFactory.GetFileInfoService(fileInfo.FileType).GetFileContent(fileInfo.FilePath);
+ // 判断文件大小,超过2m的文件不预览
+ if (FileUtil.OutOfRange(fileInfo.FileSize))
+ {
+ Message.ShowInfo("MessageContainer", "只能预览小于2MB的文档");
+ return;
+ }
+
+ // 获取扩展名
+ string fileExt = Path.GetExtension(fileInfo.FilePath).Replace(".", "");
+
+ // 图片文件
+ if ("png,jpg,gif".Contains(fileExt))
+ {
+ this.PreviewFileContent.Visibility = Visibility.Hidden;
+ this.PreviewImage.Visibility = Visibility.Visible;
+ BitmapImage bi = new BitmapImage();
+ bi.BeginInit();
+ bi.CacheOption = BitmapCacheOption.OnLoad;
+ bi.StreamSource = new MemoryStream(File.ReadAllBytes(fileInfo.FilePath));
+ bi.EndInit();
+ bi.Freeze();
- this.Dispatcher.InvokeAsync(()=> {
- // Paragraph 类似于 html 的 P 标签
- Paragraph p = new Paragraph();
- // Run 是一个 Inline 的标签
- Run r = new Run(content);
- p.Inlines.Add(r);
+ this.PreviewImage.Source = bi;
- this.PreviewFileContent.Document.Blocks.Add(p);
+ }
+ else
+ {
+ this.PreviewImage.Visibility = Visibility.Hidden;
+ this.PreviewFileContent.Visibility = Visibility.Visible;
+ // 文件内容预览
+ ThreadPool.QueueUserWorkItem(_ =>
+ {
+ // 文件内容
+ string content = FileInfoServiceFactory.GetFileInfoService(fileInfo.FileType).GetFileContent(fileInfo.FilePath);
- // 关键词高亮
- RichTextBoxUtil.Highlighted(this.PreviewFileContent, Colors.Red, fileInfo.Keywords);
+ this.Dispatcher.BeginInvoke(new Action(() =>
+ {
+ // Paragraph 类似于 html 的 P 标签
+ Paragraph p = new Paragraph();
+ // Run 是一个 Inline 的标签
+ Run r = new Run(content);
+ p.Inlines.Add(r);
+
+ this.PreviewFileContent.Document.Blocks.Add(p);
+
+ // 关键词高亮
+ RichTextBoxUtil.Highlighted(this.PreviewFileContent, Colors.Red, fileInfo.Keywords);
+ }));
});
- });
+ }
}
diff --git a/TextLocator/Properties/AssemblyInfo.cs b/TextLocator/Properties/AssemblyInfo.cs
index 2d55d9899aa6e10fb6379103da7fda770ee7eccc..4e3011f83c45fedd3d88d581212ef1194ba840d3 100644
--- a/TextLocator/Properties/AssemblyInfo.cs
+++ b/TextLocator/Properties/AssemblyInfo.cs
@@ -1,6 +1,4 @@
using System.Reflection;
-using System.Resources;
-using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;
@@ -8,11 +6,11 @@ using System.Windows;
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("TextLocator")]
-[assembly: AssemblyDescription("本地全文索引搜索定位工具")]
+[assembly: AssemblyDescription("本地文档全文索引搜索定位工具")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("刘磊")]
-[assembly: AssemblyProduct("TextLocator")]
-[assembly: AssemblyCopyright("Copyright © 2021")]
+[assembly: AssemblyProduct("文本定位器")]
+[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@@ -51,8 +49,8 @@ using System.Windows;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
+[assembly: AssemblyVersion("1.0.0.3")]
+[assembly: AssemblyFileVersion("1.0.0.3")]
// log4net
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
\ No newline at end of file
diff --git a/TextLocator/Properties/Resources.Designer.cs b/TextLocator/Properties/Resources.Designer.cs
index c54c4f338deb6134e78a9f76d8c56d18f8bbc334..4e784911a2e9a005a5de233fc33750eab4e184b7 100644
--- a/TextLocator/Properties/Resources.Designer.cs
+++ b/TextLocator/Properties/Resources.Designer.cs
@@ -1,70 +1,143 @@
//------------------------------------------------------------------------------
//
// 此代码由工具生成。
-// 运行时版本: 4.0.30319.42000
+// 运行时版本:4.0.30319.42000
//
-// 对此文件的更改可能导致不正确的行为,如果
-// 重新生成代码,则所做更改将丢失。
+// 对此文件的更改可能会导致不正确的行为,并且如果
+// 重新生成代码,这些更改将会丢失。
//
//------------------------------------------------------------------------------
-
-namespace TextLocator.Properties
-{
+namespace TextLocator.Properties {
+ using System;
+
+
///
- /// 强类型资源类,用于查找本地化字符串等。
+ /// 一个强类型的资源类,用于查找本地化的字符串等。
///
// 此类是由 StronglyTypedResourceBuilder
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
- // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen
+ // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
// (以 /str 作为命令选项),或重新生成 VS 项目。
- [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
- internal class Resources
- {
-
+ internal class Resources {
+
private static global::System.Resources.ResourceManager resourceMan;
-
+
private static global::System.Globalization.CultureInfo resourceCulture;
-
+
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
- internal Resources()
- {
+ internal Resources() {
}
-
+
///
- /// 返回此类使用的缓存 ResourceManager 实例。
+ /// 返回此类使用的缓存的 ResourceManager 实例。
///
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
- internal static global::System.Resources.ResourceManager ResourceManager
- {
- get
- {
- if ((resourceMan == null))
- {
+ internal static global::System.Resources.ResourceManager ResourceManager {
+ get {
+ if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TextLocator.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
-
+
///
/// 重写当前线程的 CurrentUICulture 属性,对
/// 使用此强类型资源类的所有资源查找执行重写。
///
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
- internal static global::System.Globalization.CultureInfo Culture
- {
- get
- {
+ internal static global::System.Globalization.CultureInfo Culture {
+ get {
return resourceCulture;
}
- set
- {
+ set {
resourceCulture = value;
}
}
+
+ ///
+ /// 查找 System.Drawing.Bitmap 类型的本地化资源。
+ ///
+ internal static System.Drawing.Bitmap eml {
+ get {
+ object obj = ResourceManager.GetObject("eml", resourceCulture);
+ return ((System.Drawing.Bitmap)(obj));
+ }
+ }
+
+ ///
+ /// 查找 System.Drawing.Bitmap 类型的本地化资源。
+ ///
+ internal static System.Drawing.Bitmap excel {
+ get {
+ object obj = ResourceManager.GetObject("excel", resourceCulture);
+ return ((System.Drawing.Bitmap)(obj));
+ }
+ }
+
+ ///
+ /// 查找 System.Drawing.Bitmap 类型的本地化资源。
+ ///
+ internal static System.Drawing.Bitmap html {
+ get {
+ object obj = ResourceManager.GetObject("html", resourceCulture);
+ return ((System.Drawing.Bitmap)(obj));
+ }
+ }
+
+ ///
+ /// 查找 System.Drawing.Bitmap 类型的本地化资源。
+ ///
+ internal static System.Drawing.Bitmap pdf {
+ get {
+ object obj = ResourceManager.GetObject("pdf", resourceCulture);
+ return ((System.Drawing.Bitmap)(obj));
+ }
+ }
+
+ ///
+ /// 查找 System.Drawing.Bitmap 类型的本地化资源。
+ ///
+ internal static System.Drawing.Bitmap ppt {
+ get {
+ object obj = ResourceManager.GetObject("ppt", resourceCulture);
+ return ((System.Drawing.Bitmap)(obj));
+ }
+ }
+
+ ///
+ /// 查找 System.Drawing.Bitmap 类型的本地化资源。
+ ///
+ internal static System.Drawing.Bitmap rtf {
+ get {
+ object obj = ResourceManager.GetObject("rtf", resourceCulture);
+ return ((System.Drawing.Bitmap)(obj));
+ }
+ }
+
+ ///
+ /// 查找 System.Drawing.Bitmap 类型的本地化资源。
+ ///
+ internal static System.Drawing.Bitmap txt {
+ get {
+ object obj = ResourceManager.GetObject("txt", resourceCulture);
+ return ((System.Drawing.Bitmap)(obj));
+ }
+ }
+
+ ///
+ /// 查找 System.Drawing.Bitmap 类型的本地化资源。
+ ///
+ internal static System.Drawing.Bitmap word {
+ get {
+ object obj = ResourceManager.GetObject("word", resourceCulture);
+ return ((System.Drawing.Bitmap)(obj));
+ }
+ }
}
}
diff --git a/TextLocator/Properties/Resources.resx b/TextLocator/Properties/Resources.resx
index af7dbebbacef595e3089c01c05671016c21a8304..90492469a8368d33fbe099a65f6b0b7fab2e3265 100644
--- a/TextLocator/Properties/Resources.resx
+++ b/TextLocator/Properties/Resources.resx
@@ -46,7 +46,7 @@
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
- : System.Serialization.Formatters.Binary.BinaryFormatter
+ : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
@@ -60,6 +60,7 @@
: and then encoded with base64 encoding.
-->
+
@@ -68,9 +69,10 @@
-
+
+
@@ -85,9 +87,10 @@
-
+
+
@@ -109,9 +112,34 @@
2.0
- System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
- System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ ..\Resource\ext\eml.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
+
+
+ ..\Resource\ext\excel.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
+
+
+ ..\Resource\ext\html.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
+
+
+ ..\Resource\ext\pdf.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
+
+
+ ..\Resource\ext\ppt.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
+
+
+ ..\Resource\ext\rtf.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
+
+
+ ..\Resource\ext\txt.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
+
+
+ ..\Resource\ext\word.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
+
\ No newline at end of file
diff --git a/TextLocator/Service/ExcelFileService.cs b/TextLocator/Service/ExcelFileService.cs
index e6ac2b8821abf3d2ae35c0e086111d1d7bfb9e01..e1a25bc0ffe248fd8f8c0f170d43bc0b42c22ef4 100644
--- a/TextLocator/Service/ExcelFileService.cs
+++ b/TextLocator/Service/ExcelFileService.cs
@@ -29,92 +29,101 @@ namespace TextLocator.Service
// 获取扩展名
string extName = Path.GetExtension(filePath);
// 文件流
- FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
- // 读取IWorkbook
- IWorkbook readWorkbook = null;
- switch (extName)
+ using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite))
{
- // 把xls写入workbook中 2003版本
- case ".xls":
- readWorkbook = new HSSFWorkbook(fileStream);
- break;
- // 把xlsx 写入workbook中 2007版本
- case ".xlsx":
- readWorkbook = new XSSFWorkbook(fileStream);
- break;
- default:
- break;
- }
- // 关闭文件流
- fileStream.Close();
+ // 读取IWorkbook
+ IWorkbook readWorkbook = null;
+ switch (extName)
+ {
+ // 把xls写入workbook中 2003版本
+ case ".xls":
+ readWorkbook = new HSSFWorkbook(fileStream);
+ break;
+ // 把xlsx 写入workbook中 2007版本
+ case ".xlsx":
+ readWorkbook = new XSSFWorkbook(fileStream);
+ break;
+ default:
+ break;
+ }
+ // 关闭文件流
+ fileStream.Close();
- if (readWorkbook != null)
- {
- StringBuilder builder = new StringBuilder();
- // 获取表
- var sheetCount = readWorkbook.NumberOfSheets;
- for (int i = 0; i < sheetCount; i++)
+ if (readWorkbook != null)
{
- // 获取sheet表数据
- ISheet sheet = readWorkbook.GetSheetAt(i);
+ StringBuilder builder = new StringBuilder();
+ // 获取表
+ var sheetCount = readWorkbook.NumberOfSheets;
+ for (int i = 0; i < sheetCount; i++)
+ {
+ // 获取sheet表数据
+ ISheet sheet = readWorkbook.GetSheetAt(i);
- // 获取行数
- var rowCount = sheet.LastRowNum;
+ // 获取行数
+ var rowCount = sheet.LastRowNum;
- // 从第四行(下标为3)开始获取数据,前三行是表头
- // 如果从第一行开始,则i=0就可以了
- for (int j = 0; j <= rowCount; j++)
- {
- // 获取具体行
- IRow row = sheet.GetRow(j);
- if (row != null)
+ // 从第四行(下标为3)开始获取数据,前三行是表头
+ // 如果从第一行开始,则i=0就可以了
+ for (int j = 0; j <= rowCount; j++)
{
- // 获取行对应的列数
- for (int k = 0; k < row.LastCellNum; k++)
+ // 获取具体行
+ IRow row = sheet.GetRow(j);
+ if (row != null)
{
- // 获取某行某列对应的单元格数据
- builder.Append(row.GetCell(k) + ";");
+ // 获取行对应的列数
+ for (int k = 0; k < row.LastCellNum; k++)
+ {
+ // 获取某行某列对应的单元格数据
+ builder.Append(row.GetCell(k) + ";");
+ }
+ // 换行
+ builder.AppendLine();
}
- // 换行
- builder.AppendLine();
}
+
+ readWorkbook.Close();
}
- }
-
- content = builder.ToString();
+ content = builder.ToString();
+ }
}
}
catch
{
// =========== Spire.XLS ===========
// 创建Workbook对象
- Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
-
- // 加载Excel文档
- workbook.LoadFromFile(filePath);
+ using (Spire.Xls.Workbook workbook = new Spire.Xls.Workbook())
+ {
- StringBuilder builder = new StringBuilder();
+ // 加载Excel文档
+ workbook.LoadFromFile(filePath);
- // 获取工作表
- for (int i = 0; i < workbook.Worksheets.Count; i++)
- {
- Spire.Xls.Worksheet sheet = workbook.Worksheets[i];
+ StringBuilder builder = new StringBuilder();
- // 行
- for(int j = sheet.FirstRow; j < sheet.LastRow; j++)
+ // 获取工作表
+ for (int i = 0; i < workbook.Worksheets.Count; i++)
{
- Spire.Xls.CellRange row = sheet.Rows[j];
- // 列
- for (int k = 0; k < row.Columns.Length; k++)
+ Spire.Xls.Worksheet sheet = workbook.Worksheets[i];
+
+ // 行
+ for (int j = sheet.FirstRow; j < sheet.LastRow; j++)
{
- builder.Append(row.Columns[k].Value2.ToString());
+ Spire.Xls.CellRange row = sheet.Rows[j];
+ // 列
+ for (int k = 0; k < row.Columns.Length; k++)
+ {
+ builder.Append(row.Columns[k].Value2.ToString());
+ }
+ row.Dispose();
+ builder.AppendLine();
}
- builder.AppendLine();
+ sheet.Dispose();
}
- }
- content = builder.ToString();
+ workbook.Dispose();
+
+ content = builder.ToString();
+ }
}
}
catch (Exception ex)
diff --git a/TextLocator/Service/PdfFileService.cs b/TextLocator/Service/PdfFileService.cs
index 068b30f996dc597912ff60674a1f850ed8dd576e..d9cbd12ccabb214ec272e4478417e07ba3ea8739 100644
--- a/TextLocator/Service/PdfFileService.cs
+++ b/TextLocator/Service/PdfFileService.cs
@@ -20,21 +20,25 @@ namespace TextLocator.Service
try
{
// 实例化一个PdfDocument对象
- PdfDocument doc = new PdfDocument();
+ using (PdfDocument doc = new PdfDocument())
+ {
+ //实例化一个StringBuilder 对象
+ StringBuilder builder = new StringBuilder();
+ // 加载Pdf文档
+ doc.LoadFromFile(filePath);
- // 加载Pdf文档
- doc.LoadFromFile(filePath);
+ //提取PDF所有页面的文本
+ foreach (PdfPageBase page in doc.Pages)
+ {
+ builder.Append(page.ExtractText());
+ }
- //实例化一个StringBuilder 对象
- StringBuilder builder = new StringBuilder();
+ doc.Dispose();
- //提取PDF所有页面的文本
- foreach (PdfPageBase page in doc.Pages)
- {
- builder.Append(page.ExtractText());
+ content = builder.ToString();
}
- content = builder.ToString();
+
}
catch (Exception ex)
{
diff --git a/TextLocator/Service/PowerPointFileService.cs b/TextLocator/Service/PowerPointFileService.cs
index f8ec8c7b0ede7cf5797c60509fb78984549e0dbe..e6d7423766fe0653548c9f29889b4d16794bbfbe 100644
--- a/TextLocator/Service/PowerPointFileService.cs
+++ b/TextLocator/Service/PowerPointFileService.cs
@@ -18,23 +18,28 @@ namespace TextLocator.Service
string content = "";
try
{
- Presentation presentation = new Presentation(filePath, FileFormat.Auto);
- StringBuilder builder = new StringBuilder();
- foreach (ISlide slide in presentation.Slides)
- {
- foreach (IShape shape in slide.Shapes)
+ using (Presentation presentation = new Presentation(filePath, FileFormat.Auto)) {
+ StringBuilder builder = new StringBuilder();
+ foreach (ISlide slide in presentation.Slides)
{
- if (shape is IAutoShape)
+ foreach (IShape shape in slide.Shapes)
{
- foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs)
+ if (shape is IAutoShape)
{
- builder.Append(tp.Text + Environment.NewLine);
+ if ((shape as IAutoShape).TextFrame != null)
+ {
+ foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs)
+ {
+ builder.Append(tp.Text + Environment.NewLine);
+ }
+ }
}
+ shape.Dispose();
}
+ slide.Dispose();
}
+ content = builder.ToString();
}
-
- content = builder.ToString();
}
catch (Exception ex)
{
diff --git a/TextLocator/Service/TxtFileService.cs b/TextLocator/Service/TxtFileService.cs
index bfcebdd5bc847d4ba55ecb5d27d0f63aad59db9f..5ecbbef1da335717cdd2bd2e4b3b8f2ef67122b6 100644
--- a/TextLocator/Service/TxtFileService.cs
+++ b/TextLocator/Service/TxtFileService.cs
@@ -18,15 +18,17 @@ namespace TextLocator.Service
string content = "";
try
{
- StreamReader reader = new StreamReader(filePath, Encoding.UTF8);
- StringBuilder builder = new StringBuilder();
- string line;
- while ((line = reader.ReadLine()) != null)
+ using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8))
{
- builder.Append(line);
- }
+ StringBuilder builder = new StringBuilder();
+ string line;
+ while ((line = reader.ReadLine()) != null)
+ {
+ builder.Append(line);
+ }
- content = builder.ToString();
+ content = builder.ToString();
+ }
}
catch (Exception ex)
{
diff --git a/TextLocator/Service/WordFileService.cs b/TextLocator/Service/WordFileService.cs
index ea99865dbf969b4d3c27b79ba8c67737cc898145..b3edd4afa9923f14f40f00e1d60b8a181d3c32d3 100644
--- a/TextLocator/Service/WordFileService.cs
+++ b/TextLocator/Service/WordFileService.cs
@@ -22,15 +22,15 @@ namespace TextLocator.Service
using (var document = new Document(new FileStream(filePath, FileMode.Open)))
{
// 提取每个段落的文本
- var sb = new StringBuilder();
+ StringBuilder builder = new StringBuilder();
foreach (Section section in document.Sections)
{
foreach (Spire.Doc.Documents.Paragraph paragraph in section.Paragraphs)
{
- sb.AppendLine(paragraph.Text);
+ builder.AppendLine(paragraph.Text);
}
}
- content = sb.ToString();
+ content = builder.ToString();
}
}
catch (Exception ex)
diff --git a/TextLocator/TextLocator.csproj b/TextLocator/TextLocator.csproj
index 30ae3b6b6dc328bc09e833c95da7825e700e7eaa..de11f59fde8aa3641dfa87ec779acca4d9bcd6bc 100644
--- a/TextLocator/TextLocator.csproj
+++ b/TextLocator/TextLocator.csproj
@@ -132,6 +132,7 @@
+
@@ -153,6 +154,7 @@
Designer
+
diff --git a/TextLocator/Util/FileUtil.cs b/TextLocator/Util/FileUtil.cs
index 9229b2ed056a8f9eb86b16a9a53e46f93f978251..e5b6579f9d26b0c4b13c6be3aa7bb99856c5ba34 100644
--- a/TextLocator/Util/FileUtil.cs
+++ b/TextLocator/Util/FileUtil.cs
@@ -1,6 +1,7 @@
using log4net;
using System;
using System.Collections.Generic;
+using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
@@ -18,23 +19,43 @@ namespace TextLocator.Util
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
///
- /// 图标集合
+ /// 根据文件类型获取文件图标
///
- private static readonly Dictionary icons = new Dictionary();
-
- static FileUtil()
+ ///
+ ///
+ public static BitmapImage GetFileIcon(FileType fileType)
{
- icons.Add("word", new BitmapImage(new Uri(@"/Resource/ext/word.png", UriKind.Relative)));
- icons.Add("excel", new BitmapImage(new Uri(@"/Resource/ext/excel.png", UriKind.Relative)));
- icons.Add("ppt", new BitmapImage(new Uri(@"/Resource/ext/rtf.png", UriKind.Relative)));
-
- icons.Add("pdf", new BitmapImage(new Uri(@"/Resource/ext/pdf.png", UriKind.Relative)));
-
- icons.Add("txt", new BitmapImage(new Uri(@"/Resource/ext/txt.png", UriKind.Relative)));
- icons.Add("html", new BitmapImage(new Uri(@"/Resource/ext/html.png", UriKind.Relative)));
-
- icons.Add("eml", new BitmapImage(new Uri(@"/Resource/ext/eml.png", UriKind.Relative)));
- icons.Add("rtf", new BitmapImage(new Uri(@"/Resource/ext/rtf.png", UriKind.Relative)));
+ Bitmap bitmap = null;
+ switch (fileType)
+ {
+ case FileType.Word类型:
+ bitmap = Properties.Resources.word;
+ break;
+ case FileType.Excel类型:
+ bitmap = Properties.Resources.excel;
+ break;
+ case FileType.PowerPoint类型:
+ bitmap = Properties.Resources.ppt;
+ break;
+ case FileType.PDF类型:
+ bitmap = Properties.Resources.pdf;
+ break;
+ default:
+ bitmap = Properties.Resources.txt;
+ break;
+ }
+ BitmapImage bi = new BitmapImage();
+ using (MemoryStream ms = new MemoryStream())
+ {
+ bitmap.Save(ms, bitmap.RawFormat);
+ bi.BeginInit();
+ bi.CacheOption = BitmapCacheOption.OnLoad;
+ bi.StreamSource = ms;
+ bi.EndInit();
+ bi.Freeze();
+ }
+ bitmap.Dispose();
+ return bi;
}
///
@@ -54,6 +75,48 @@ namespace TextLocator.Util
return filePaths;
}
+ ///
+ /// 获取文件大小友好显示
+ ///
+ ///
+ ///
+ public static string GetFileSizeFriendly(long fileSize)
+ {
+ string fileSizeUnit = "b";
+ if (fileSize > 1024)
+ {
+ fileSize = fileSize / 1024;
+ fileSizeUnit = "KB";
+ }
+ if (fileSize > 1024)
+ {
+ fileSize = fileSize / 1024;
+ fileSizeUnit = "MB";
+ }
+ if (fileSize > 1024)
+ {
+ fileSize = fileSize / 1024;
+ fileSizeUnit = "GB";
+ }
+ return fileSize + "" + fileSizeUnit;
+ }
+
+ ///
+ /// 超出范围
+ ///
+ ///
+ ///
+ ///
+ public static bool OutOfRange(long fileSize, int range = 10)
+ {
+ return false;
+ /*if (fileSize <= 0)
+ {
+ return false;
+ }
+ return fileSize / 1024 / 1024 > range;*/
+ }
+
///
/// 获取指定根目录下的子目录及其文档
///
@@ -99,31 +162,5 @@ namespace TextLocator.Util
log.Error(ex.Message, ex);
}
}
-
- ///
- /// 根据文件类型获取图标
- ///
- ///
- public static BitmapImage GetFileTypeIcon(FileType fileType)
- {
- switch (fileType)
- {
- case FileType.Word类型:
- return icons["word"];
- case FileType.Excel类型:
- return icons["excel"];
- case FileType.PowerPoint类型:
- return icons["ppt"];
- case FileType.PDF类型:
- return icons["pdf"];
- case FileType.HTML或XML类型:
- return icons["html"];
- case FileType.纯文本:
- return icons["txt"];
- case FileType.其他类型:
- return icons["txt"];
- }
- return null;
- }
}
}