Skip to content

Commit

Permalink
[New] OpenXmlConfiguration add AutoFilter property.
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed Oct 21, 2021
1 parent c415e34 commit f6d0d7c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,17 @@ MiniExcel.SaveAs(path, value,configuration:config);
![image](https://user-images.githubusercontent.com/12729184/118784917-f3e57700-b8c2-11eb-8718-8d955b1bc197.png)


#### 9. AutoFilter

Since v0.19.0 `OpenXmlConfiguration.AutoFilter` can en/unable AutoFilter , default value is `true`, and setting AutoFilter way:

```csharp
MiniExcel.SaveAs(path, value, configuration: new OpenXmlConfiguration() { AutoFilter = false });
```





### Fill Data To Excel Template <a name="getstart3"></a>

Expand Down
8 changes: 8 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,14 @@ MiniExcel.SaveAs(path, value,configuration:config);

![image](https://user-images.githubusercontent.com/12729184/118784917-f3e57700-b8c2-11eb-8718-8d955b1bc197.png)

#### 9. AutoFilter 筛选

从 0.19.0 支持,可藉由 OpenXmlConfiguration.AutoFilter 设定,预设为True。关闭 AutoFilter 方式 :

```csharp
MiniExcel.SaveAs(path, value, configuration: new OpenXmlConfiguration() { AutoFilter = false });
```




Expand Down
6 changes: 6 additions & 0 deletions README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,13 @@ MiniExcel.SaveAs(path, value,configuration:config);



#### 9. AutoFilter 篩選

從 0.19.0 支持,可藉由 OpenXmlConfiguration.AutoFilter 設定,預設為True。關閉 AutoFilter 方式 :

```csharp
MiniExcel.SaveAs(path, value, configuration: new OpenXmlConfiguration() { AutoFilter = false });
```



Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
### 0.19.0
- [New] SaveAs default style with autoFilter mode. #190
- [New] Add ConvertCsvToXlsx、ConvertXlsxToCsv method. #292
- [New] OpenXmlConfiguration add AutoFilter property.
- [Bug] Fix after CSV Query then SaveAs system will throw "Stream was not readable." exception. #293

### 0.18.0
Expand Down
1 change: 1 addition & 0 deletions docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
### 0.19.0
- [New] SaveAs 预设样式增加筛选功能. #190
- [New] 新增 ConvertCsvToXlsx、ConvertXlsxToCsv 方法. #292
- [New] OpenXmlConfiguration 新增 AutoFilter 属性.
- [Bug] 修正 CSV 读取后 SaveAs 会抛出 "Stream was not readable." 错误. #293

### 0.18.0
Expand Down
1 change: 1 addition & 0 deletions docs/README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
### 0.19.0
- [New] SaveAs 預設樣式增加篩選功能. #190
- [New] 新增 ConvertCsvToXlsx、ConvertXlsxToCsv 方法. #292
- [New] OpenXmlConfiguration 新增 AutoFilter 屬性.
- [Bug] 修正 CSV 讀取後 SaveAs 會拋出 "Stream was not readable." 錯誤. #293

### 0.18.0
Expand Down
11 changes: 6 additions & 5 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void SaveAs(object value, string sheetName, bool printHeader, IConfigurat
{
sheetId++;
var sheetPath = $"xl/worksheets/sheet{sheetId}.xml";
CreateSheetXml(sheet.Value, printHeader, archive, packages, sheetPath);
CreateSheetXml(sheet.Value, printHeader, archive, packages, sheetPath, config);
}
GenerateContentTypesXml(archive, packages);
}
Expand All @@ -54,21 +54,21 @@ public void SaveAs(object value, string sheetName, bool printHeader, IConfigurat
{
sheetId++;
var sheetPath = $"xl/worksheets/sheet{sheetId}.xml";
CreateSheetXml(dt, printHeader, archive, packages, sheetPath);
CreateSheetXml(dt, printHeader, archive, packages, sheetPath, config);
}
GenerateContentTypesXml(archive, packages);
}
else
{
var packages = DefualtOpenXml.GenerateDefaultOpenXml(archive, new[] { sheetName }, config);
var sheetPath = "xl/worksheets/sheet1.xml";
CreateSheetXml(value, printHeader, archive, packages, sheetPath);
CreateSheetXml(value, printHeader, archive, packages, sheetPath, config);
GenerateContentTypesXml(archive, packages);
}
}
}

private void CreateSheetXml(object value, bool printHeader, MiniExcelZipArchive archive, Dictionary<string, ZipPackageInfo> packages, string sheetPath)
private void CreateSheetXml(object value, bool printHeader, MiniExcelZipArchive archive, Dictionary<string, ZipPackageInfo> packages, string sheetPath, OpenXmlConfiguration configuration)
{
ZipArchiveEntry entry = archive.CreateEntry(sheetPath);
using (var zipStream = entry.Open())
Expand Down Expand Up @@ -231,7 +231,8 @@ private void CreateSheetXml(object value, bool printHeader, MiniExcelZipArchive
else
throw new NotImplementedException($"Type {type.Name} & genericType {genericType.Name} not Implemented. please issue for me.");
writer.Write("</x:sheetData>");
writer.Write($"<x:autoFilter ref=\"A1:{ExcelOpenXmlUtils.ConvertXyToCell(maxColumnIndex, maxRowIndex==0?1: maxRowIndex)}\" />");
if(configuration.AutoFilter)
writer.Write($"<x:autoFilter ref=\"A1:{ExcelOpenXmlUtils.ConvertXyToCell(maxColumnIndex, maxRowIndex==0?1: maxRowIndex)}\" />");
writer.Write("</x:worksheet>");
}
else if (value is DataTable)
Expand Down
1 change: 1 addition & 0 deletions src/MiniExcel/OpenXml/OpenXmlConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public class OpenXmlConfiguration : IConfiguration
internal static readonly OpenXmlConfiguration DefaultConfig = new OpenXmlConfiguration();
public bool FillMergedCells { get; set; }
public TableStyles TableStyles { get; set; } = TableStyles.Default;
public bool AutoFilter { get; set; } = true;
}
}
8 changes: 8 additions & 0 deletions tests/MiniExcelTests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public MiniExcelIssueTests(ITestOutputHelper output)
[Fact]
public void TestIssue190()
{
{
var path = PathHelper.GetTempPath();
var value = new TestIssue190Dto[] { };
MiniExcel.SaveAs(path, value, configuration: new OpenXmlConfiguration() { AutoFilter = false });

var sheetXml = Helpers.GetZipFileContent(path, "xl/worksheets/sheet1.xml");
Assert.DoesNotContain("<x:autoFilter ref=\"A1:C1\" />", sheetXml);
}
{
var path = PathHelper.GetTempPath();
var value = new TestIssue190Dto[] { };
Expand Down

0 comments on commit f6d0d7c

Please sign in to comment.