Skip to content

Commit

Permalink
[New] Support CSV Insert #I4X92G (via @shps951023)
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed Sep 19, 2022
1 parent 0996ab8 commit d436511
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 21 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,46 @@ Since V1.26.0, we can set the attributes of Column dynamically



### Add, Delete, Update

#### Add

v1.28.0 support CSV insert N rows data after last row

```csharp
// Origin
{
var value = new[] {
new { ID=1,Name ="Jack",InDate=new DateTime(2021,01,03)},
new { ID=2,Name ="Henry",InDate=new DateTime(2020,05,03)},
};
MiniExcel.SaveAs(path, value);
}
// Insert 1 rows after last
{
var value = new { ID=3,Name = "Mike", InDate = new DateTime(2021, 04, 23) };
MiniExcel.Insert(path, value);
}
// Insert N rows after last
{
var value = new[] {
new { ID=4,Name ="Frank",InDate=new DateTime(2021,06,07)},
new { ID=5,Name ="Gloria",InDate=new DateTime(2022,05,03)},
};
MiniExcel.Insert(path, value);
}
```

![image](https://user-images.githubusercontent.com/12729184/191023733-1e2fa732-db5c-4a3a-9722-b891fe5aa069.png)



#### Delete(waiting)

#### Update(waiting)



### Excel Type Auto Check <a name="getstart5"></a>

- 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.
Expand Down
38 changes: 38 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,8 @@ MiniExcel.SaveAs(path, value);





### 模板填充 Excel <a name="getstart3"></a>

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



### 新增、删除、修改

#### 新增

v1.28.0 开始支持 CSV 插入新增,在最后一行新增N笔数据

```csharp
// 原始数据
{
var value = new[] {
new { ID=1,Name ="Jack",InDate=new DateTime(2021,01,03)},
new { ID=2,Name ="Henry",InDate=new DateTime(2020,05,03)},
};
MiniExcel.SaveAs(path, value);
}
// 最后一行新增一行数据
{
var value = new { ID=3,Name = "Mike", InDate = new DateTime(2021, 04, 23) };
MiniExcel.Insert(path, value);
}
// 最后一行新增N行数据
{
var value = new[] {
new { ID=4,Name ="Frank",InDate=new DateTime(2021,06,07)},
new { ID=5,Name ="Gloria",InDate=new DateTime(2022,05,03)},
};
MiniExcel.Insert(path, value);
}
```

![image](https://user-images.githubusercontent.com/12729184/191023733-1e2fa732-db5c-4a3a-9722-b891fe5aa069.png)



#### 删除(未完成)

#### 修改(未完成)

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

Expand Down
40 changes: 40 additions & 0 deletions README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,46 @@ public class TestIssueI4TXGTDto



### 新增、刪除、修改

#### 新增

v1.28.0 開始支持 CSV 插入新增,在最後一行新增N筆數據

```csharp
// 原始數據
{
var value = new[] {
new { ID=1,Name ="Jack",InDate=new DateTime(2021,01,03)},
new { ID=2,Name ="Henry",InDate=new DateTime(2020,05,03)},
};
MiniExcel.SaveAs(path, value);
}
// 最後一行新增一行數據
{
var value = new { ID=3,Name = "Mike", InDate = new DateTime(2021, 04, 23) };
MiniExcel.Insert(path, value);
}
// 最後一行新增N行數據
{
var value = new[] {
new { ID=4,Name ="Frank",InDate=new DateTime(2021,06,07)},
new { ID=5,Name ="Gloria",InDate=new DateTime(2022,05,03)},
};
MiniExcel.Insert(path, value);
}
```

![image](https://user-images.githubusercontent.com/12729184/191023733-1e2fa732-db5c-4a3a-9722-b891fe5aa069.png)



#### 刪除(未完成)

#### 修改(未完成)



### Excel 類別自動判斷 <a name="getstart5"></a>

- MiniExcel 預設會根據`文件擴展名`判斷是 xlsx 還是 csv,但會有失準時候,請自行指定。
Expand Down
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@



## 1.28.0

- [New] Support CSV Insert #I4X92G (via @shps951023)

### 1.27.0

- [New] Support DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )
Expand Down
6 changes: 6 additions & 0 deletions docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@





## 1.28.0

- [New] 支持 CSV Insert 方法 #I4X92G (via @shps951023)

### 1.27.0

- [New] 支持 DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )
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 @@ -26,6 +26,10 @@



## 1.28.0

- [New] 支持 CSV Insert 方法 #I4X92G (via @shps951023)

### 1.27.0

- [New] 支持 DateTimeOffset and ExcelFormat #430 (via @Lightczx , @shps951023 )
Expand Down
10 changes: 8 additions & 2 deletions src/MiniExcel/Csv/CsvWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ internal class CsvWriter : IExcelWriter, IDisposable
private readonly Stream _stream;
private readonly CsvConfiguration _configuration;
private readonly bool _printHeader;
private readonly object _value;
private object _value;
private readonly StreamWriter _writer;
private bool disposedValue;
private object _insertValue;

public CsvWriter(Stream stream, object value, IConfiguration configuration, bool printHeader)
{
Expand Down Expand Up @@ -81,7 +82,7 @@ public void SaveAs()
{
mode = "Properties";
genericType = item.GetType();
props = CustomPropertyHelper.GetSaveAsProperties(genericType,_configuration);
props = CustomPropertyHelper.GetSaveAsProperties(genericType, _configuration);
}

break;
Expand Down Expand Up @@ -138,6 +139,11 @@ public void SaveAs()
}
}

public void Insert()
{
SaveAs();
}

public async Task SaveAsAsync(CancellationToken cancellationToken = default(CancellationToken))
{
await Task.Run(() => SaveAs(),cancellationToken).ConfigureAwait(false);
Expand Down
1 change: 1 addition & 0 deletions src/MiniExcel/IExcelWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ internal interface IExcelWriter
{
void SaveAs();
Task SaveAsAsync(CancellationToken cancellationToken = default(CancellationToken));
void Insert();
}
}
23 changes: 23 additions & 0 deletions src/MiniExcel/MiniExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using MiniExcelLibs.Utils;
using MiniExcelLibs.Zip;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
Expand All @@ -24,6 +25,28 @@ public static MiniExcelDataReader GetReader(this Stream stream, bool useHeaderRo
return new MiniExcelDataReader(stream, useHeaderRow, sheetName, excelType, startCell, configuration);
}

public static void Insert(string path, object value, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null)
{
if (Path.GetExtension(path).ToLowerInvariant() != ".csv")
throw new NotSupportedException("MiniExcel SaveAs only support csv insert now");

using (var stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Read, 4096, FileOptions.SequentialScan))
Insert(stream, value, sheetName, ExcelTypeHelper.GetExcelType(path, excelType), configuration);
}

public static void Insert(this Stream stream, object value, string sheetName = "Sheet1", ExcelType excelType = ExcelType.XLSX, IConfiguration configuration = null)
{
// reuse code
object v = null;
{
if (!(value is IEnumerable) && !(value is IDataReader) && !(value is IDictionary<string, object>) && !(value is IDictionary))
v = Enumerable.Range(0, 1).Select(s => value);
else
v = value;
}
ExcelWriterFactory.GetProvider(stream, v, sheetName, excelType, configuration, false).Insert();
}

public static void SaveAs(string path, object value, bool printHeader = true, string sheetName = "Sheet1", ExcelType excelType = ExcelType.UNKNOWN, IConfiguration configuration = null,bool overwriteFile = false)
{
if (Path.GetExtension(path).ToLowerInvariant() == ".xlsm")
Expand Down
16 changes: 8 additions & 8 deletions 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>1.27.0</Version>
<Version>1.28.0</Version>
</PropertyGroup>
<PropertyGroup>
<AssemblyName>MiniExcel</AssemblyName>
Expand All @@ -11,22 +11,22 @@
<Description>
Fast, Low-Memory, Easy Excel .NET helper to import/export/template spreadsheet

Github : https://github.com/MiniExcel/MiniExcel
Github : https://github.com/mini-software/MiniExcel
Gitee : https://gitee.com/dotnetchina/MiniExcel
Issues : https://github.com/MiniExcel/MiniExcel/issues
Todo : https://github.com/MiniExcel/MiniExcel/projects/1?fullscreen=true
Issues : https://github.com/mini-software/MiniExcel/issues
Todo : https://github.com/mini-software/MiniExcel/projects/1?fullscreen=true
</Description>
<Authors>LIN,WEI-HAN</Authors>
<PackageId>MiniExcel</PackageId>
<Copyright>LIN,WEI-HAN, 2021 onwards</Copyright>
<NeutralLanguage>en</NeutralLanguage>
<license>https://raw.githubusercontent.com/MiniExcel/MiniExcel/master/LICENSE</license>
<license>https://raw.githubusercontent.com/mini-software/MiniExcel/master/LICENSE</license>
<RootNamespace>MiniExcelLibs</RootNamespace>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/MiniExcel/MiniExcel</PackageProjectUrl>
<RepositoryUrl>https://github.com/MiniExcel/MiniExcel</RepositoryUrl>
<PackageProjectUrl>https://github.com/mini-software/MiniExcel</PackageProjectUrl>
<RepositoryUrl>https://github.com/mini-software/MiniExcel</RepositoryUrl>
<PackageIcon>icon.png</PackageIcon>
<PackageReleaseNotes>Please Check [Release Notes](https://github.com/MiniExcel/MiniExcel/tree/master/docs)</PackageReleaseNotes>
<PackageReleaseNotes>Please Check [Release Notes](https://github.com/mini-software/MiniExcel/tree/master/docs)</PackageReleaseNotes>
<RepositoryType>Github</RepositoryType>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net461'">
Expand Down
9 changes: 9 additions & 0 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public ExcelOpenXmlSheetWriter(Stream stream, object value, string sheetName, IC
_sheets.Add(new SheetDto { Name = sheetName, SheetIdx = 1 }); //TODO:remove
}

public ExcelOpenXmlSheetWriter()
{
}

public void SaveAs()
{
GenerateDefaultOpenXml();
Expand Down Expand Up @@ -777,5 +781,10 @@ private string GetDimensionRef(int maxRowIndex, int maxColumnIndex)
{
await Task.Run(() => SaveAs(),cancellationToken).ConfigureAwait(false);
}

public void Insert()
{
throw new NotImplementedException();
}
}
}
Loading

0 comments on commit d436511

Please sign in to comment.