Skip to content

Commit d436511

Browse files
committed
[New] Support CSV Insert #I4X92G (via @shps951023)
1 parent 0996ab8 commit d436511

12 files changed

Lines changed: 239 additions & 21 deletions

File tree

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,46 @@ Since V1.26.0, we can set the attributes of Column dynamically
934934

935935

936936

937+
### Add, Delete, Update
938+
939+
#### Add
940+
941+
v1.28.0 support CSV insert N rows data after last row
942+
943+
```csharp
944+
// Origin
945+
{
946+
var value = new[] {
947+
new { ID=1,Name ="Jack",InDate=new DateTime(2021,01,03)},
948+
new { ID=2,Name ="Henry",InDate=new DateTime(2020,05,03)},
949+
};
950+
MiniExcel.SaveAs(path, value);
951+
}
952+
// Insert 1 rows after last
953+
{
954+
var value = new { ID=3,Name = "Mike", InDate = new DateTime(2021, 04, 23) };
955+
MiniExcel.Insert(path, value);
956+
}
957+
// Insert N rows after last
958+
{
959+
var value = new[] {
960+
new { ID=4,Name ="Frank",InDate=new DateTime(2021,06,07)},
961+
new { ID=5,Name ="Gloria",InDate=new DateTime(2022,05,03)},
962+
};
963+
MiniExcel.Insert(path, value);
964+
}
965+
```
966+
967+
![image](https://user-images.githubusercontent.com/12729184/191023733-1e2fa732-db5c-4a3a-9722-b891fe5aa069.png)
968+
969+
970+
971+
#### Delete(waiting)
972+
973+
#### Update(waiting)
974+
975+
976+
937977
### Excel Type Auto Check <a name="getstart5"></a>
938978

939979
- MiniExcel will check whether it is xlsx or csv based on the `file extension` by default, but there may be inaccuracy, please specify it manually.

README.zh-CN.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ MiniExcel.SaveAs(path, value);
534534

535535

536536

537+
538+
537539
### 模板填充 Excel <a name="getstart3"></a>
538540

539541
- 宣告方式类似 Vue 模板 `{{变量名称}}`, 或是集合渲染 `{{集合名称.栏位名称}}`
@@ -933,7 +935,43 @@ public class TestIssueI4TXGTDto
933935

934936

935937

938+
### 新增、删除、修改
939+
940+
#### 新增
941+
942+
v1.28.0 开始支持 CSV 插入新增,在最后一行新增N笔数据
943+
944+
```csharp
945+
// 原始数据
946+
{
947+
var value = new[] {
948+
new { ID=1,Name ="Jack",InDate=new DateTime(2021,01,03)},
949+
new { ID=2,Name ="Henry",InDate=new DateTime(2020,05,03)},
950+
};
951+
MiniExcel.SaveAs(path, value);
952+
}
953+
// 最后一行新增一行数据
954+
{
955+
var value = new { ID=3,Name = "Mike", InDate = new DateTime(2021, 04, 23) };
956+
MiniExcel.Insert(path, value);
957+
}
958+
// 最后一行新增N行数据
959+
{
960+
var value = new[] {
961+
new { ID=4,Name ="Frank",InDate=new DateTime(2021,06,07)},
962+
new { ID=5,Name ="Gloria",InDate=new DateTime(2022,05,03)},
963+
};
964+
MiniExcel.Insert(path, value);
965+
}
966+
```
967+
968+
![image](https://user-images.githubusercontent.com/12729184/191023733-1e2fa732-db5c-4a3a-9722-b891fe5aa069.png)
969+
970+
971+
972+
#### 删除(未完成)
936973

974+
#### 修改(未完成)
937975

938976
### Excel 类别自动判断 <a name="getstart5"></a>
939977

README.zh-Hant.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,46 @@ public class TestIssueI4TXGTDto
937937

938938

939939

940+
### 新增、刪除、修改
941+
942+
#### 新增
943+
944+
v1.28.0 開始支持 CSV 插入新增,在最後一行新增N筆數據
945+
946+
```csharp
947+
// 原始數據
948+
{
949+
var value = new[] {
950+
new { ID=1,Name ="Jack",InDate=new DateTime(2021,01,03)},
951+
new { ID=2,Name ="Henry",InDate=new DateTime(2020,05,03)},
952+
};
953+
MiniExcel.SaveAs(path, value);
954+
}
955+
// 最後一行新增一行數據
956+
{
957+
var value = new { ID=3,Name = "Mike", InDate = new DateTime(2021, 04, 23) };
958+
MiniExcel.Insert(path, value);
959+
}
960+
// 最後一行新增N行數據
961+
{
962+
var value = new[] {
963+
new { ID=4,Name ="Frank",InDate=new DateTime(2021,06,07)},
964+
new { ID=5,Name ="Gloria",InDate=new DateTime(2022,05,03)},
965+
};
966+
MiniExcel.Insert(path, value);
967+
}
968+
```
969+
970+
![image](https://user-images.githubusercontent.com/12729184/191023733-1e2fa732-db5c-4a3a-9722-b891fe5aa069.png)
971+
972+
973+
974+
#### 刪除(未完成)
975+
976+
#### 修改(未完成)
977+
978+
979+
940980
### Excel 類別自動判斷 <a name="getstart5"></a>
941981

942982
- MiniExcel 預設會根據`文件擴展名`判斷是 xlsx 還是 csv,但會有失準時候,請自行指定。

docs/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424

2525

2626

27+
## 1.28.0
28+
29+
- [New] Support CSV Insert #I4X92G (via @shps951023)
30+
2731
### 1.27.0
2832

2933
- [New] Support DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )

docs/README.zh-CN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727

2828

2929

30+
31+
32+
## 1.28.0
33+
34+
- [New] 支持 CSV Insert 方法 #I4X92G (via @shps951023)
35+
3036
### 1.27.0
3137

3238
- [New] 支持 DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )

docs/README.zh-Hant.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727

2828

29+
## 1.28.0
30+
31+
- [New] 支持 CSV Insert 方法 #I4X92G (via @shps951023)
32+
2933
### 1.27.0
3034

3135
- [New] 支持 DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )

src/MiniExcel/Csv/CsvWriter.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ internal class CsvWriter : IExcelWriter, IDisposable
1818
private readonly Stream _stream;
1919
private readonly CsvConfiguration _configuration;
2020
private readonly bool _printHeader;
21-
private readonly object _value;
21+
private object _value;
2222
private readonly StreamWriter _writer;
2323
private bool disposedValue;
24+
private object _insertValue;
2425

2526
public CsvWriter(Stream stream, object value, IConfiguration configuration, bool printHeader)
2627
{
@@ -81,7 +82,7 @@ public void SaveAs()
8182
{
8283
mode = "Properties";
8384
genericType = item.GetType();
84-
props = CustomPropertyHelper.GetSaveAsProperties(genericType,_configuration);
85+
props = CustomPropertyHelper.GetSaveAsProperties(genericType, _configuration);
8586
}
8687

8788
break;
@@ -138,6 +139,11 @@ public void SaveAs()
138139
}
139140
}
140141

142+
public void Insert()
143+
{
144+
SaveAs();
145+
}
146+
141147
public async Task SaveAsAsync(CancellationToken cancellationToken = default(CancellationToken))
142148
{
143149
await Task.Run(() => SaveAs(),cancellationToken).ConfigureAwait(false);

src/MiniExcel/IExcelWriter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ internal interface IExcelWriter
88
{
99
void SaveAs();
1010
Task SaveAsAsync(CancellationToken cancellationToken = default(CancellationToken));
11+
void Insert();
1112
}
1213
}

src/MiniExcel/MiniExcel.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using MiniExcelLibs.Utils;
55
using MiniExcelLibs.Zip;
66
using System;
7+
using System.Collections;
78
using System.Collections.Generic;
89
using System.Data;
910
using System.Dynamic;
@@ -24,6 +25,28 @@ public static MiniExcelDataReader GetReader(this Stream stream, bool useHeaderRo
2425
return new MiniExcelDataReader(stream, useHeaderRow, sheetName, excelType, startCell, configuration);
2526
}
2627

28+
public static void Insert(string path, object value, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null)
29+
{
30+
if (Path.GetExtension(path).ToLowerInvariant() != ".csv")
31+
throw new NotSupportedException("MiniExcel SaveAs only support csv insert now");
32+
33+
using (var stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.SequentialScan))
34+
Insert(stream, value, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration);
35+
}
36+
37+
public static void Insert(this Stream stream, object value, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null)
38+
{
39+
// reuse code
40+
object v = null;
41+
{
42+
if (!(value is IEnumerable) && !(value is IDataReader) && !(value is IDictionary<string, object>) && !(value is IDictionary))
43+
v = Enumerable.Range(0, 1).Select(s => value);
44+
else
45+
v = value;
46+
}
47+
ExcelWriterFactory.GetProvider(stream, v, sheetName, excelType, configuration, false).Insert();
48+
}
49+
2750
public static void SaveAs(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null,bool overwriteFile = false)
2851
{
2952
if (Path.GetExtension(path).ToLowerInvariant() == ".xlsm")

src/MiniExcel/MiniExcelLibs.csproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
4-
<Version>1.27.0</Version>
4+
<Version>1.28.0</Version>
55
</PropertyGroup>
66
<PropertyGroup>
77
<AssemblyName>MiniExcel</AssemblyName>
@@ -11,22 +11,22 @@
1111
<Description>
1212
Fast, Low-Memory, Easy Excel .NET helper to import/export/template spreadsheet
1313

14-
Github : https://github.com/MiniExcel/MiniExcel
14+
Github : https://github.com/mini-software/MiniExcel
1515
Gitee : https://gitee.com/dotnetchina/MiniExcel
16-
Issues : https://github.com/MiniExcel/MiniExcel/issues
17-
Todo : https://github.com/MiniExcel/MiniExcel/projects/1?fullscreen=true
16+
Issues : https://github.com/mini-software/MiniExcel/issues
17+
Todo : https://github.com/mini-software/MiniExcel/projects/1?fullscreen=true
1818
</Description>
1919
<Authors>LIN,WEI-HAN</Authors>
2020
<PackageId>MiniExcel</PackageId>
2121
<Copyright>LIN,WEI-HAN, 2021 onwards</Copyright>
2222
<NeutralLanguage>en</NeutralLanguage>
23-
<license>https://raw.githubusercontent.com/MiniExcel/MiniExcel/master/LICENSE</license>
23+
<license>https://raw.githubusercontent.com/mini-software/MiniExcel/master/LICENSE</license>
2424
<RootNamespace>MiniExcelLibs</RootNamespace>
2525
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
26-
<PackageProjectUrl>https://github.com/MiniExcel/MiniExcel</PackageProjectUrl>
27-
<RepositoryUrl>https://github.com/MiniExcel/MiniExcel</RepositoryUrl>
26+
<PackageProjectUrl>https://github.com/mini-software/MiniExcel</PackageProjectUrl>
27+
<RepositoryUrl>https://github.com/mini-software/MiniExcel</RepositoryUrl>
2828
<PackageIcon>icon.png</PackageIcon>
29-
<PackageReleaseNotes>Please Check [Release Notes](https://github.com/MiniExcel/MiniExcel/tree/master/docs)</PackageReleaseNotes>
29+
<PackageReleaseNotes>Please Check [Release Notes](https://github.com/mini-software/MiniExcel/tree/master/docs)</PackageReleaseNotes>
3030
<RepositoryType>Github</RepositoryType>
3131
</PropertyGroup>
3232
<ItemGroup Condition=" '$(TargetFramework)' == 'net461'">

0 commit comments

Comments
 (0)