Skip to content

Commit

Permalink
Support Xlsm AutoCheck #227
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed May 10, 2021
1 parent c7e4230 commit c0799b9
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ No, the image test has 1 million rows*10 columns of data, the maximum memory usa
### Limitations and caveats

- Not support xls and encrypted file now
- xlsm only support Query

### Reference

Expand Down
3 changes: 2 additions & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,8 @@ A. 名称一样,系统会自动映射(注意:大小写不敏感)

### 局限与警告

- 目前不支援 xls (97-2003) 或是加密文件。
- 目前不支援 xls (97-2003) 或是加密文件
- xlsm 只支持查询



Expand Down
3 changes: 2 additions & 1 deletion README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,8 @@ A. 名稱一樣,系統會自動映射(注意:大小寫不敏感)

### 侷限與警告

- 目前不支援 xls (97-2003) 或是加密檔案。
- 目前不支援 xls (97-2003) 或是加密檔案
- xlsm 只支持查詢



Expand Down
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

---

### 0.14.3
- [Opt] Support Xlsm AutoCheck #227
- [Bug] Fix SaveAsByTemplate single column demension index error #226

### 0.14.2
- [Bug] Fix asp.net webform gridview datasource can't use QueryAsDataTable #223

Expand Down
5 changes: 5 additions & 0 deletions docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

---


### 0.14.3
- [Opt] 支持 Xlsm 自动判断 #227
- [Bug] 修正 SaveAsByTemplate 单列 demension 索引错误 #226

### 0.14.2
- [Bug] 修正 asp.net webform gridview datasource 不能使用 QueryAsDataTable #223

Expand Down
4 changes: 4 additions & 0 deletions docs/README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

---

### 0.14.3
- [Opt] 支持 Xlsm 自動判斷 #227
- [Bug] 修正 SaveAsByTemplate 單列 demension 索引錯誤 #226

### 0.14.2
- [Bug] 修正 asp.net webform gridview datasource 不能使用 QueryAsDataTable #223

Expand Down
Binary file added samples/xlsx/TestIssue227.xlsm
Binary file not shown.
1 change: 1 addition & 0 deletions src/MiniExcel/MiniExcel.TypeCheker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal static ExcelType GetExcelType(string filePath, ExcelType excelType)
case ".csv":
return ExcelType.CSV;
case ".xlsx":
case ".xlsm":
return ExcelType.XLSX;
//case ".xls":
// return ExcelType.XLS;
Expand Down
3 changes: 3 additions & 0 deletions src/MiniExcel/MiniExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MiniExcelLibs.OpenXml;
using MiniExcelLibs.Utils;
using MiniExcelLibs.Zip;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
Expand All @@ -12,6 +13,8 @@ public static partial class MiniExcel
{
public static void SaveAs(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null)
{
if (Path.GetExtension(path).ToLowerInvariant() == ".xlsm")
throw new NotSupportedException("MiniExcel SaveAs not support xlsm");
using (FileStream stream = new FileStream(path, FileMode.CreateNew))
SaveAs(stream, value, printHeader, sheetName, GetExcelType(path, excelType), configuration);
}
Expand Down
2 changes: 1 addition & 1 deletion src/MiniExcel/MiniExcelLibs.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
<Version>0.14.1</Version>
<Version>0.14.3</Version>
</PropertyGroup>
<PropertyGroup>
<AssemblyName>MiniExcel</AssemblyName>
Expand Down
73 changes: 60 additions & 13 deletions tests/MiniExcelTests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,53 @@ public MiniExcelIssueTests(ITestOutputHelper output)
this.output = output;
}

/// <summary>
/// [Support Xlsm AutoCheck · Issue #227 · shps951023/MiniExcel]
/// (https://github.com/shps951023/MiniExcel/issues/227)
/// </summary>
[Fact]
public void Issue227()
{
{
var path = PathHelper.GetNewTemplateFilePath("xlsm");
Assert.Throws<NotSupportedException>(() => MiniExcel.SaveAs(path, new[] { new { V = "A1" }, new { V = "A2" } }));
}

{
var path = PathHelper.GetSamplePath("xlsx/TestIssue227.xlsm");
{
var rows = MiniExcel.Query<UserAccount>(path).ToList();

Assert.Equal(100, rows.Count());

Assert.Equal(Guid.Parse("78DE23D2-DCB6-BD3D-EC67-C112BBC322A2"), rows[0].ID);
Assert.Equal("Wade", rows[0].Name);
Assert.Equal(DateTime.ParseExact("27/09/2020", "dd/MM/yyyy", CultureInfo.InvariantCulture), rows[0].BoD);
Assert.Equal(36, rows[0].Age);
Assert.False(rows[0].VIP);
Assert.Equal(decimal.Parse("5019.12"), rows[0].Points);
Assert.Equal(1, rows[0].IgnoredProperty);
}
{
using (var stream = File.OpenRead(path))
{
var rows = stream.Query<UserAccount>().ToList();

Assert.Equal(100, rows.Count());

Assert.Equal(Guid.Parse("78DE23D2-DCB6-BD3D-EC67-C112BBC322A2"), rows[0].ID);
Assert.Equal("Wade", rows[0].Name);
Assert.Equal(DateTime.ParseExact("27/09/2020", "dd/MM/yyyy", CultureInfo.InvariantCulture), rows[0].BoD);
Assert.Equal(36, rows[0].Age);
Assert.False(rows[0].VIP);
Assert.Equal(decimal.Parse("5019.12"), rows[0].Points);
Assert.Equal(1, rows[0].IgnoredProperty);
}
}
}


}

/// <summary>
/// https://github.com/shps951023/MiniExcel/issues/226
Expand Down Expand Up @@ -53,7 +100,7 @@ public void Issue223()
new Dictionary<string, object>(){{"A",Guid.NewGuid()},{"B","HelloWorld"}},
};
var path = PathHelper.GetNewTemplateFilePath();
MiniExcel.SaveAs(path,value);
MiniExcel.SaveAs(path, value);

var dt = MiniExcel.QueryAsDataTable(path);
var columns = dt.Columns;
Expand All @@ -74,7 +121,7 @@ public void Issue222()
var path = PathHelper.GetSamplePath("xlsx/TestIssue222.xlsx");
var rows = MiniExcel.Query(path).ToList();
Assert.Equal(typeof(DateTime), rows[1].A.GetType());
Assert.Equal(new DateTime(2021,4,29), rows[1].A);
Assert.Equal(new DateTime(2021, 4, 29), rows[1].A);
}

/// <summary>
Expand Down Expand Up @@ -142,7 +189,7 @@ public void Issue211()

MiniExcel.SaveAs(path, reader);

var rows = MiniExcel.Query(path,true).ToList();
var rows = MiniExcel.Query(path, true).ToList();
Assert.Equal((double)1, rows[0].Test1);
Assert.Equal((double)2, rows[0].Test2);
Assert.Equal((double)3, rows[1].Test1);
Expand All @@ -157,7 +204,7 @@ public void Issue211()
public void Issue216()
{
var path = PathHelper.GetNewTemplateFilePath();
var value = new[] { new { Test1="1",Test2=2 }, new { Test1 = "3", Test2 = 4 } };
var value = new[] { new { Test1 = "1", Test2 = 2 }, new { Test1 = "3", Test2 = 4 } };
MiniExcel.SaveAs(path, value);

{
Expand Down Expand Up @@ -205,7 +252,7 @@ public void IssueI3OSKV()
var value = new[] { new { Test = 123456.789 } };
MiniExcel.SaveAs(path, value);

var A2 = MiniExcel.Query(path,true).First().Test ;
var A2 = MiniExcel.Query(path, true).First().Test;
Assert.Equal(123456.789, A2);

File.Delete(path);
Expand All @@ -223,14 +270,14 @@ public void Issue220()
var path = PathHelper.GetSamplePath("xlsx/TestIssue220.xlsx");
var rows = MiniExcel.Query(path, useHeaderRow: true);
var result = (from s in rows
group s by s.PRT_ID into g
select new
{
PRT_ID = g.Key,
Apr = g.Sum(_ => (double?)_.Apr),
May = g.Sum(_ => (double?)_.May),
Jun = g.Sum(_ => (double?)_.Jun),
}
group s by s.PRT_ID into g
select new
{
PRT_ID = g.Key,
Apr = g.Sum(_ => (double?)_.Apr),
May = g.Sum(_ => (double?)_.May),
Jun = g.Sum(_ => (double?)_.Jun),
}
).ToList();
Assert.Equal(91843.25, result[0].Jun);
Assert.Equal(50000.99, result[1].Jun);
Expand Down

0 comments on commit c0799b9

Please sign in to comment.