Skip to content

Commit

Permalink
CSV SaveAs support datareader
Browse files Browse the repository at this point in the history
  • Loading branch information
shps951023 committed Jun 8, 2021
1 parent c7658b2 commit 725317c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 9 deletions.
3 changes: 3 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

---

### 0.15.3
- [New] Csv SaveAs support datareader

### 0.15.2
- [New] Support Custom Datetime format #241
- [Bug] Csv type mapping Query error "cannot be converted to xxx type" #243
Expand Down
3 changes: 3 additions & 0 deletions docs/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

---

### 0.15.3
- [New] Csv SaveAs 支持 datareader

### 0.15.2
- [New] 支持自定义日期时间格式 #241
- [Bug] CSV类型映射查询错误 "cannot be converted to xxx type" #243
Expand Down
3 changes: 3 additions & 0 deletions docs/README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

---

### 0.15.3
- [New] Csv SaveAs 支持 datareader

### 0.15.2
- [New] 支持自定義日期時間格式 #241
- [Bug] CSV類型映射查詢錯誤 "cannot be converted to xxx type" #243
Expand Down
41 changes: 39 additions & 2 deletions src/MiniExcel/Csv/CsvWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ public void SaveAs(object value, string sheetName, bool printHeader, IConfigurat
var type = value.GetType();
Type genericType = null;

//DapperRow
if (value is IEnumerable)
if(value is IDataReader)
{
GenerateSheetByIDataReader(value, printHeader, seperator, newLine, writer);
}
else if (value is IEnumerable)
{
var values = value as IEnumerable;
List<object> keys = new List<object>();
Expand Down Expand Up @@ -122,6 +125,40 @@ public void SaveAs(object value, string sheetName, bool printHeader, IConfigurat
}
}

private static void GenerateSheetByIDataReader(object value, bool printHeader, string seperator, string newLine, StreamWriter writer)
{
var reader = (IDataReader)value;

int fieldCount = reader.FieldCount;
if (fieldCount == 0)
throw new InvalidDataException("fieldCount is 0");

if (printHeader)
{
for (int i = 0; i < fieldCount; i++)
{
var columnName = reader.GetName(i);

if (i != 0)
writer.Write(seperator);
writer.Write(CsvHelpers.ConvertToCsvValue(columnName?.ToCsvString(null)));
}
writer.Write(newLine);
}

while (reader.Read())
{
for (int i = 0; i < fieldCount; i++)
{
var cellValue = reader.GetValue(i);
if (i != 0)
writer.Write(seperator);
writer.Write(CsvHelpers.ConvertToCsvValue(cellValue?.ToCsvString(null)));
}
writer.Write(newLine);
}
}

private void GenerateSheetByDataTable(StreamWriter writer, DataTable dt, bool printHeader, string seperator, string newLine)
{
if (printHeader)
Expand Down
12 changes: 6 additions & 6 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>0.15.2</Version>
<Version>0.15.3</Version>
</PropertyGroup>
<PropertyGroup>
<AssemblyName>MiniExcel</AssemblyName>
Expand All @@ -10,11 +10,11 @@
<PackageTags>excel;xlsx;micro-helper;mini;openxml;helper;</PackageTags>
<Description>A high performance and easy Excel(xlsx,csv) Micro-Helper that avoids OOM and without third-party dependencies to create/query/template-fill-data.

Github : https://github.com/shps951023/MiniExcel
Gitee : https://gitee.com/dotnetchina/MiniExcel
Issues : https://github.com/shps951023/MiniExcel/issues
Todo : https://github.com/shps951023/MiniExcel/projects/1?fullscreen=true
</Description>
Github : https://github.com/shps951023/MiniExcel
Gitee : https://gitee.com/dotnetchina/MiniExcel
Issues : https://github.com/shps951023/MiniExcel/issues
Todo : https://github.com/shps951023/MiniExcel/projects/1?fullscreen=true
</Description>
<Authors>ITWeiHan</Authors>
<Copyright>©2021 WeiHan Lin</Copyright>
<license>https://raw.githubusercontent.com/shps951023/MiniExcel/master/LICENSE.md</license>
Expand Down
20 changes: 20 additions & 0 deletions tests/MiniExcelTests/MiniExcelIssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ public MiniExcelIssueTests(ITestOutputHelper output)
this.output = output;
}

/// <summary>
/// [CSV SaveAs support datareader · Issue #251 · shps951023/MiniExcel](https://github.com/shps951023/MiniExcel/issues/251)
/// </summary>
[Fact]
public void Issue251()
{
using (var cn = Db.GetConnection())
{
var reader = cn.ExecuteReader(@"select '""<>+-*//}{\\n' a,1234567890 b union all select '<test>Hello World</test>',-1234567890");
var path = PathHelper.GetTempPath(extension:"csv");
MiniExcel.SaveAs(path, reader);
Console.WriteLine(path);
var expected = @"a,b
""""""<>+-*//}{\\n"",1234567890
""<test>Hello World</test>"",-1234567890
";
Assert.Equal(expected, File.ReadAllText(path));
}
}

/// <summary>
/// No error exception throw when reading xls file #242
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion tests/MiniExcelTests/Utils/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MiniExcelLibs.Tests.Utils

internal static class Db
{
internal static SQLiteConnection GetConnection(string connectionString)
internal static SQLiteConnection GetConnection(string connectionString= "Data Source=:memory:")
{
return new SQLiteConnection(connectionString);
}
Expand Down

0 comments on commit 725317c

Please sign in to comment.