Skip to content

Commit

Permalink
- [New] Support GetColumns method #174
Browse files Browse the repository at this point in the history
- [Fix] dimension without x prefix #175
  • Loading branch information
shps951023 committed Apr 14, 2021
1 parent d11a3aa commit 1ea8957
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 7 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ foreach (var sheetName in sheetNames)
}
```

#### 7. Dynamic Query cast row to `IDictionary<string,object>`
#### 7. Get Columns

```C#
var columns = MiniExcel.GetColumns(path); // e.g result : ["A","B"...]
```

#### 8. Dynamic Query cast row to `IDictionary<string,object>`

```C#
foreach(IDictionary<string,object> row in MiniExcel.Query(path))
Expand Down
10 changes: 9 additions & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,15 @@ foreach (var sheetName in sheetNames)
}
```

#### 7. Dynamic Query 转成 `IDictionary<string,object>` 数据
#### 7. 查询所有栏(列)

```C#
var columns = MiniExcel.GetColumns(path); // e.g result : ["A","B"...]
var cnt = columns.Count; // get column count
```

#### 8. Dynamic Query 转成 `IDictionary<string,object>` 数据

```C#
foreach(IDictionary<string,object> row in MiniExcel.Query(path))
Expand Down
10 changes: 9 additions & 1 deletion README.zh-Hant.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,15 @@ foreach (var sheetName in sheetNames)
}
```

#### 7. Dynamic Query 轉成 `IDictionary<string,object>` 資料
#### 7. 查詢所有欄(列)

```C#
var columns = MiniExcel.GetColumns(path); // e.g result : ["A","B"...]
var cnt = columns.Count; // get column count
```

#### 8. Dynamic Query 轉成 `IDictionary<string,object>` 資料

```C#
foreach(IDictionary<string,object> row in MiniExcel.Query(path))
Expand Down
3 changes: 3 additions & 0 deletions drafts/【MiniExcel】Basic Demo.linq
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

void Main()
{
// ==== Excel Query ====
{
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString()}.xlsx");
Console.WriteLine("==== SaveAs by strongly type ====");
Expand Down Expand Up @@ -48,6 +49,8 @@ void Main()
Console.WriteLine(sheets);
}
}

// ==== Create/Save Excel ====
{
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid().ToString()}.xlsx");
Console.WriteLine("==== SaveAs by Anonymous ====");
Expand Down
Binary file added samples/xlsx/TestEmpty.xlsx
Binary file not shown.
Binary file removed samples/xlsx/c.xlsx
Binary file not shown.
11 changes: 11 additions & 0 deletions src/MiniExcel/MiniExcel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public static IEnumerable<string> GetSheetNames(this Stream stream)
yield return item.Name;
}

public static ICollection<string> GetColumns(string path)
{
using (var stream = File.OpenRead(path))
return (Query(stream).FirstOrDefault() as IDictionary<string, object>)?.Keys;
}

public static ICollection<string> GetColumns(this Stream stream)
{
return (Query(stream).FirstOrDefault() as IDictionary<string, object>)?.Keys;
}

public static void SaveAsByTemplate(string path, string templatePath, object value)
{
using(var stream = File.Create(path))
Expand Down
6 changes: 3 additions & 3 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void SaveAs(object value,bool printHeader, IConfiguration configuration)
// dimension

var maxRowIndex = rowCount + (printHeader && rowCount > 0 ? 1 : 0); //TODO:it can optimize
writer.Write($@"<dimension ref=""{GetDimension(maxRowIndex, maxColumnIndex)}""/><x:sheetData>");
writer.Write($@"<x:dimension ref=""{GetDimension(maxRowIndex, maxColumnIndex)}""/><x:sheetData>");

//header
var yIndex = 1;
Expand Down Expand Up @@ -180,7 +180,7 @@ public void SaveAs(object value,bool printHeader, IConfiguration configuration)

private void WriteEmptySheet(StreamWriter writer)
{
writer.Write($@"<?xml version=""1.0"" encoding=""utf-8""?><x:worksheet xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main""><dimension ref=""A1""/><x:sheetData></x:sheetData></x:worksheet>");
writer.Write($@"<?xml version=""1.0"" encoding=""utf-8""?><x:worksheet xmlns:x=""http://schemas.openxmlformats.org/spreadsheetml/2006/main""><x:dimension ref=""A1""/><x:sheetData></x:sheetData></x:worksheet>");
}

internal void GenerateSheetByDapperRow(StreamWriter writer, ZipArchive archive, IEnumerable value, int rowCount, List<string> keys, int xIndex = 1, int yIndex = 1)
Expand Down Expand Up @@ -314,7 +314,7 @@ internal void GenerateSheetByDataTable(StreamWriter writer, ZipArchive archive,
// dimension
var maxRowIndex = value.Rows.Count + (printHeader && value.Rows.Count > 0 ? 1 : 0);
var maxColumnIndex = value.Columns.Count;
writer.Write($@"<dimension ref=""{GetDimension(maxRowIndex, maxColumnIndex)}""/><x:sheetData>");
writer.Write($@"<x:dimension ref=""{GetDimension(maxRowIndex, maxColumnIndex)}""/><x:sheetData>");

if (printHeader)
{
Expand Down
23 changes: 23 additions & 0 deletions tests/MiniExcelTests/MiniExcelOpenXmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ namespace MiniExcelLibs.Tests
{
public partial class MiniExcelOpenXmlTests
{

[Fact]
public void GetColumnsTest()
{
{
var path = @"D:\git\MiniExcel\samples\xlsx\TestTypeMapping.xlsx";
var columns = MiniExcel.GetColumns(path);
Assert.Equal(new[] { "A", "B", "C", "D", "E", "F", "G", "H" }, columns);
}

{
var path = @"D:\git\MiniExcel\samples\xlsx\TestTypeMapping.xlsx";
var columns = MiniExcel.GetColumns(path);
Assert.Equal(8, columns.Count);
}

{
var path = @"D:\git\MiniExcel\samples\xlsx\TestEmpty.xlsx";
var columns = MiniExcel.GetColumns(path);
Assert.Null(columns);
}
}

[Fact]
public void SaveAsControlChracter()
{
Expand Down
4 changes: 3 additions & 1 deletion tests/MiniExcelTests/Utils/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ internal static string GetFirstSheetDimensionRefValue(string path)
using (var stream = File.OpenRead(path))
using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Read, false, Encoding.UTF8))
{
var sheet = archive.Entries.Single(w => w.FullName.StartsWith("xl/worksheets/sheet1", StringComparison.OrdinalIgnoreCase));
var sheet = archive.Entries.Single(w => w.FullName.StartsWith("xl/worksheets/sheet1", StringComparison.OrdinalIgnoreCase)
|| w.FullName.StartsWith("/xl/worksheets/sheet1", StringComparison.OrdinalIgnoreCase)
);
using (var sheetStream = sheet.Open())
{
var doc = XDocument.Load(sheetStream); ;
Expand Down

0 comments on commit 1ea8957

Please sign in to comment.