Skip to content

Commit

Permalink
- [New] Support open with read only mode, avoid error of The process …
Browse files Browse the repository at this point in the history
…cannot access the file because it is being used by another process #87
  • Loading branch information
shps951023 committed Apr 20, 2021
1 parent 9b593cb commit 3aeae7d
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 12 deletions.
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

---

### 0.13.3
- [New] Support open with read only mode, avoid error of The process cannot access the file because it is being used by another process [#87](https://github.com/shps951023/MiniExcel/issues/#87)

### 0.13.2
- [Bug] Fix Column more than 255 rows cannot be read error [#208](https://github.com/shps951023/MiniExcel/issues/#208)

Expand Down
5 changes: 4 additions & 1 deletion docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

---

### 0.13.3
- [New] 支持 Excel 单纯读取模式,避免同时改模版又运行 MiniExcel 出现错误 "The process cannot access the file because it is being used by another process" [#87](https://github.com/shps951023/MiniExcel/issues/#87)

### 0.13.2
- [Bug] 列超过 255 行无法读取错误 [#208](https://github.com/shps951023/MiniExcel/issues/#208)
- [Bug] 超过 255 列无法读取错误 [#208](https://github.com/shps951023/MiniExcel/issues/#208)

### 0.13.1
- [New] SaveAsByTemplate 支持读取模板 byte[],方便缓存跟支持多用户同时读取同一个模板 #189
Expand Down
5 changes: 4 additions & 1 deletion docs/README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

---

### 0.13.3
- [New] 支持 Excel 單純讀取模式,避免同時改模版又運行 MiniExcel 出現錯誤 "The process cannot access the file because it is being used by another process" [#87](https://github.com/shps951023/MiniExcel/issues/#87)

### 0.13.2
- [Bug] 列超過 255 行無法讀取錯誤 [#208](https://github.com/shps951023/MiniExcel/issues/#208)
- [Bug] 超過 255 列無法讀取錯誤 [#208](https://github.com/shps951023/MiniExcel/issues/#208)

### 0.13.1
- [New] SaveAsByTemplate 支持讀取模板 byte[],方便緩存跟支持多用戶同時讀取同一個模板 [#189](https://github.com/shps951023/MiniExcel/issues/189)
Expand Down
13 changes: 5 additions & 8 deletions src/MiniExcel/MiniExcel.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
namespace MiniExcelLibs
{
using MiniExcelLibs.OpenXml;
using MiniExcelLibs.Utils;
using MiniExcelLibs.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;

public static partial class MiniExcel
{
Expand All @@ -30,7 +27,7 @@ public static void SaveAs(this Stream stream, object value, bool printHeader = t

public static IEnumerable<T> Query<T>(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null) where T : class, new()
{
using (var stream = File.OpenRead(path))
using (var stream = Helpers.OpenSharedRead(path))
foreach (var item in Query<T>(stream, sheetName, GetExcelType(path, excelType), configuration))
yield return item; //Foreach yield return twice reason : https://stackoverflow.com/questions/66791982/ienumerable-extract-code-lazy-loading-show-stream-was-not-readable
}
Expand All @@ -42,7 +39,7 @@ public static void SaveAs(this Stream stream, object value, bool printHeader = t

public static IEnumerable<dynamic> Query(string path, bool useHeaderRow = false, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null)
{
using (var stream = File.OpenRead(path))
using (var stream = Helpers.OpenSharedRead(path))
foreach (var item in Query(stream, useHeaderRow, sheetName, GetExcelType(path, excelType), configuration))
yield return item;
}
Expand All @@ -54,7 +51,7 @@ public static IEnumerable<dynamic> Query(this Stream stream, bool useHeaderRow =

public static IEnumerable<string> GetSheetNames(string path)
{
using (var stream = File.OpenRead(path))
using (var stream = Helpers.OpenSharedRead(path))
foreach (var item in GetSheetNames(stream))
yield return item;
}
Expand All @@ -68,7 +65,7 @@ public static IEnumerable<string> GetSheetNames(this Stream stream)

public static ICollection<string> GetColumns(string path)
{
using (var stream = File.OpenRead(path))
using (var stream = Helpers.OpenSharedRead(path))
return (Query(stream).FirstOrDefault() as IDictionary<string, object>)?.Keys;
}

Expand Down
2 changes: 1 addition & 1 deletion src/MiniExcel/MiniExcelLibs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

Github : https://github.com/shps951023/MiniExcel
Gitee : https://gitee.com/dotnetchina/MiniExcel
Issues : github https://github.com/shps951023/MiniExcel/issues or Gitee https://gitee.com/dotnetchina/MiniExcel/issues
Issues : https://github.com/shps951023/MiniExcel/issues
Todo : https://github.com/shps951023/MiniExcel/projects/1?fullscreen=true
Facebook : https://www.facebook.com/miniexcel
QQ : 813100564
Expand Down
2 changes: 1 addition & 1 deletion src/MiniExcel/OpenXml/ExcelOpenXmlTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ExcelOpenXmlTemplate(Stream _strem)

public void SaveAsByTemplate(string templatePath, object value)
{
using (var stream = File.Open(templatePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var stream = Helpers.OpenSharedRead(templatePath))
SaveAsByTemplateImpl(stream, value);
}
public void SaveAsByTemplate(byte[] templateBtyes, object value)
Expand Down
5 changes: 5 additions & 0 deletions src/MiniExcel/Utils/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
using System.Linq;
using System.Reflection;

internal static partial class Helpers
{
public static FileStream OpenSharedRead(string path) => File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}

internal static partial class Helpers
{
private const int GENERAL_COLUMN_INDEX = 255;
Expand Down
19 changes: 19 additions & 0 deletions tests/MiniExcelTests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ public MiniExcelIssueTests(ITestOutputHelper output)
this.output = output;
}

/// <summary>
/// https://github.com/shps951023/MiniExcel/issues/87
/// </summary>
[Fact]
public void Issue87()
{
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString()}.xlsx");
var templatePath = @"..\..\..\..\..\samples\xlsx\TestTemplateCenterEmpty.xlsx";
var value = new
{
Tests = Enumerable.Range(1, 5).Select((s, i) => new { test1 = i, test2 = i })
};
using (var stream = File.OpenRead(templatePath))
{
var rows = MiniExcel.Query(templatePath).ToList();
MiniExcel.SaveAsByTemplate(path, templatePath, value);
}
}

/// <summary>
/// https://github.com/shps951023/MiniExcel/issues/208
/// </summary>
Expand Down

0 comments on commit 3aeae7d

Please sign in to comment.