Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkPflug committed Sep 26, 2023
1 parent aa001a2 commit 3db441d
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 26 deletions.
6 changes: 4 additions & 2 deletions docs/Async.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ buffered in memory, and all IO will be handled asynchronously.
The CreateAsync methods are only supported on .NET Core versions.

Reading:

```
// this line will buffer the entire file into memory.
await using var edr = ExcelDataReader.CreateAsync("jumbo.xlsx");
await using var edr = ExcelDataReader.CreateAsync("data.xlsx");
while(await edr.ReadAsync())
{
Expand All @@ -20,9 +21,10 @@ while(await edr.ReadAsync())
```

Writing:

```
// must use async disposal
await using var edw = ExcelDataWriter.CreateAsync("jumbo.xlsx");
await using var edw = ExcelDataWriter.CreateAsync("data.xlsx");
edw.WriteAsync(myDataReader, "MyData");
edw.WriteAsync(myOtherDataReader, "MoreData");
Expand Down
2 changes: 1 addition & 1 deletion docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ _0.1.5_
- Add `RowFieldCount` property to determine number of fields in the current row.

_0.1.4_
- Fix behavior of GetValue to honor the data type of the schema instead of the excel type of the column.
- Fix behavior of GetValue to honor the data type of the schema instead of the Excel type of the column.
- Implement `GetSchemaTable()` so DataTable.Load functions correctly.
- Skip rows containing empty data in xlsx files.
- Fix a NotImplementedException in netstandard 2.0.
4 changes: 2 additions & 2 deletions docs/Schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ It is often useful to apply a strict schema to the data in an Excel file when th
to be constrained to values of a uniform type. This allows the data to be read in a strongly-typed way
when loading into a `DataTable` or being processed by certain schema-aware tools like `SqlBulkCopy`.

Sylvan.Data.Excel allows applying a schema to excel data via the `IExcelSchemaProvider` interface.
Sylvan.Data.Excel allows applying a schema to Excel data via the `IExcelSchemaProvider` interface.
This interface allows providing a different schema per-sheet in a workbook.

The `ExcelSchema` class provides a concrete implementation of `IExcelSchemaProvider`, and is the easiet
Expand All @@ -52,7 +52,7 @@ using System.Data;
// uses Sylvan.Data package to create a schema definition.
var schema = Schema.Parse("Id:int,Name:string,Value:decimal,ReleaseDate:date,Notes:string?");

// creates an excel schema that can apply the above schema to an Excel worksheet.
// creates an Excel schema that can apply the above schema to an Excel worksheet.
var excelSchema = new ExcelSchema(hasHeaders: true, schema);

using (var data = ExcelDataReader.Create("data.xlsx", new ExcelDataReaderOptions { Schema = excelSchema }))
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The `GetExcelDataType` method allows inspecting the native Excel data type of a

### Reading Raw Data

The ExcelDataReader provides a forward only, row by row access to the data in a worksheet. It allows iterating over sheets using the `NextResult()` method, and iterating over rows using the `Read()` method. Fields are accessed using standard accessors, most commonly `GetString()`. `GetString()` is designed to not throw an exception, except in the case that a cell contains a formula error.
The ExcelDataReader provides a forward only, row by row access to the data in a worksheet. It allows iterating over sheets using the `NextResult()` method, and iterating over rows using the `Read()` method. Fields are accessed using standard accessors, most commonly `GetString()`. `GetString()` is designed to not throw an exception, except in the case that a cell contains a formula error. The `TryOpenWorksheet(string name)` method can be used to open a specific, known worksheet.

```C#
using Sylvan.Data.Excel;
Expand Down
2 changes: 1 addition & 1 deletion source/Sylvan.Data.Excel.Tests/CustomTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void MissingStyle()
[Fact]
public void EmptySharedStringValue()
{
// Test a degenerate case produced by AG grid export to excel.
// Test a degenerate case produced by AG grid export to Excel.
var reader = XlsxBuilder.Create(TestData.EmptyValue, TestData.SharedStringSimple);
Assert.True(reader.Read());
// <v>0</v>
Expand Down
2 changes: 1 addition & 1 deletion source/Sylvan.Data.Excel.Tests/ExcelDataReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ public void Date1900()
Assert.Equal(-0.5d, edr.GetDouble(0));
Assert.Throws<InvalidCastException>(() => edr.GetDateTime(0));

// The range [0, 1) renders in excel as 1900-01-00, which is nonsense.
// The range [0, 1) renders in Excel as 1900-01-00, which is nonsense.
Assert.True(edr.Read());
Assert.Equal(ExcelDataType.Numeric, edr.GetExcelDataType(0));
Assert.Equal(0, edr.GetDouble(0));
Expand Down
2 changes: 1 addition & 1 deletion source/Sylvan.Data.Excel.Tests/ExcelDataWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public void Boolean()
[Fact]
public void JaggedData()
{
// tests writing jagged data to excel.
// tests writing jagged data to Excel.
var data = "a,b,c\n1,2,3\n1,2,3,4\n,1,2,3,4,5\n";
var r = new StringReader(data);
var csv = CsvDataReader.Create(r);
Expand Down
23 changes: 14 additions & 9 deletions source/Sylvan.Data.Excel/ExcelDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@

namespace Sylvan.Data.Excel;

internal enum DateMode
{
Mode1900,
Mode1904,
}

/// <summary>
/// A DbDataReader implementation that reads data from an Excel file.
/// </summary>
public abstract partial class ExcelDataReader : DbDataReader, IDisposable, IDbColumnSchemaGenerator
{
// Excel supports two different date representations.
internal enum DateMode
{
Mode1900,
Mode1904,
}

int fieldCount;
bool isClosed;
Stream stream;
Expand Down Expand Up @@ -403,7 +404,7 @@ void ValidateSheetRange(int ordinal)
}

/// <summary>
/// Gets the value as represented in excel.
/// Gets the value as represented in Excel.
/// </summary>
/// <remarks>
/// Formula errors are returned as ExcelErrorCode values, rather than throwing an exception.
Expand Down Expand Up @@ -434,7 +435,7 @@ public object GetExcelValue(int ordinal)
}

/// <summary>
/// Gets the column schema
/// Gets the column schema of the current worksheet.
/// </summary>
public ReadOnlyCollection<DbColumn> GetColumnSchema()
{
Expand All @@ -444,6 +445,10 @@ public ReadOnlyCollection<DbColumn> GetColumnSchema()
/// <summary>
/// Initializes the schema starting with the current row.
/// </summary>
/// <remarks>
/// This can be used when a worksheet has "header" rows with non-data content.
/// Read past the header, and call Initialize when the row of tabular data is found.
/// </remarks>
public void Initialize()
{
var sheet = this.WorksheetName;
Expand Down Expand Up @@ -571,7 +576,7 @@ public sealed override object GetValue(int ordinal)
var doubleValue = GetDouble(ordinal);
unchecked
{
// excel stores all values as double
// Excel stores all values as double
// but we'll try to return it as the
// most "intuitive" type.
var int32Value = (int)doubleValue;
Expand Down
2 changes: 1 addition & 1 deletion source/Sylvan.Data.Excel/ExcelDataWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Sylvan.Data.Excel;

/// <summary>
/// Writes data to excel files.
/// Writes data to Excel files.
/// </summary>
public abstract class ExcelDataWriter :
IDisposable
Expand Down
9 changes: 2 additions & 7 deletions source/Sylvan.Data.Excel/ExcelFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;

namespace Sylvan.Data.Excel;

/// <summary>
/// Indicates the kind of data the format string represents.
/// </summary>
Expand Down Expand Up @@ -226,12 +227,6 @@ internal ExcelFormat(string spec)
{
this.Format = spec;
this.Kind = DetermineKind(spec);
//this.format = FormatKind switch
//{
// FormatKind.Date => "o",
// FormatKind.Time => "HH:mm:ss.FFFFFFF",
// _ => "G",
//};
}

internal ExcelFormat(string spec, FormatKind kind, string? format = null)
Expand All @@ -250,7 +245,7 @@ internal ExcelFormat(string spec, FormatKind kind, string? format = null)
/// </summary>
public FormatKind Kind { get; private set; }

internal string FormatValue(double value, DateMode mode)
internal string FormatValue(double value, ExcelDataReader.DateMode mode)
{
var kind = this.Kind;
switch (kind)
Expand Down

0 comments on commit 3db441d

Please sign in to comment.