GithubHelp home page GithubHelp logo

csvhelper.excel's Introduction

Discord Chat NuGet Badge

Csv Helper for Excel

CsvHelper for Excel is an extension that links 2 excellent libraries, CsvHelper and ClosedXml. It provides an implementation of ICsvParser and ICsvSerializer from CsvHelper that read and write to Excel using ClosedXml.

ExcelParser

ExcelParser implements IParser and allows you to specify the path of the workbook or a stream.

When the path is passed to the constructor then the workbook loading and disposal is handled by the parser. By default the first worksheet is used as the data source.

using var reader = new CsvReader(new ExcelParser("path/to/file.xlsx"));
var people = reader.GetRecords<Person>();

When an instance of stream is passed to the constructor then disposal will not be handled by the parser. By default the first worksheet is used as the data source.

var bytes = File.ReadAllBytes("path/to/file.xlsx");

using var stream = new MemoryStream(bytes);
using var parser = new ExcelParser(stream);
using var reader = new CsvReader(parser);

var people = reader.GetRecords<Person>();
// do other stuff with workbook

All constructor options have overloads allowing you to specify your own CsvConfiguration, otherwise the default is used.

ExcelWriter

ExcelWriter inherits from CsvWriter and, like ExcelParser, allows you to specify the path to which to (eventually) save the workbook or a stream.

When the path is passed to the constructor the creation and disposal of both the workbook and worksheet (defaultly named "Export") as well as the saving of the workbook on dispose, is handled by the serialiser.

using (var writer = new ExcelWriter("path/to/file.xlsx"))
{
    writer.WriteRecords(people);
}

When an instance of stream is passed to the constructor the creation and disposal of a new worksheet (defaultly named "Export") is handled by the serialiser, but the workbook will not be saved.

using var stream = new MemoryStream();
using (var excelWriter = new ExcelWriter(stream, CultureInfo.InvariantCulture))
{
    excelWriter.WriteRecords(people);
}
//has to be disposed to write to the stream before accessing it.

//other stuff
var bytes = stream.ToArray();

All constructor options have overloads allowing you to specify your own CsvConfiguration, otherwise the default is used.

csvhelper.excel's People

Contributors

christophano avatar number1er avatar pedoc avatar youngcm2 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.