GithubHelp home page GithubHelp logo

ccexcelwriter's Introduction

CcExcelWriter - Clean Code Excel Writer

Easiest library written in C# to write excel files (2007 and later). With this library you can write excel files with extremely low amount of code and configure fancy excel styles with no knowledge of how to do it in C#.

Introduction

DocumentFormat.OpenXML is one of the best libraries to work with excel files. But if you already use it or is learning how to do it, you will notice that this library are very complex and requires you to have a extensive knowledge about excel files structure.

CcExcelWriter is a extension of the library DocumentFormat.OpenXML, that fix all common problems of write excel files. So you have all the power of OpenXML but don't need complex code to do it.

Get and Create sheet, lines and cells become more easy, the only thing you do is specified sheet name, and cells id (column and line like A1, A2, B1, B2, ...)

Write data of different types, is simple now. This library resolve all problems to you, you can write all primitive types and other like String, DateTime, TimeSpan, without thinking of how they will be placed in excel file.

Creating styles in cells, it's a big complex thing to do in OpenXML, but with this library you don't need to think in nothing of that. You can simple create an excel file and copy the cell styles from it.

So check it out the code below to get a glimpse of what CcExcelWrite can do.

Finding the binaries

It is recommended to use Nuget

Install-Package CcExcelWriter

How to Use - Sample 01

Let's do a simple example of how to use CcExcelWriter, this exemple it isn't functional (don't have a real application) but represents well the basic resources of this library.

For start let's make a excel file like that:

Nothing special, the cells B2, C2 and D2 has font-family, font-size, background-color, font-color, text-aling, and border defined.

Now let's do some C# code:

// Get a stream of the existing excel file
// You can use any kind of stream, don't need to be FileStream.
using (var fs = new FileStream("fileName", FileMode.Open))
{
    // Open the excel to work.
    var excel = new Excel(fs);

    // Get the sheet (Plan1 is the name of the excel existing sheet,
    // to more information see comment below.
    var sheet = excel.GetSheet("Plan1");

    // Copy the styles of the existing cells to use later.
    var styleR = sheet.GetCellStyle("B", 2);
    var styleG = sheet.GetCellStyle("C", 2);
    var styleB = sheet.GetCellStyle("D", 2);

    // Clean all that exists in Plan1 to make something new.
    sheet.ClearAllSheetData();

    // Write information in some cells using the styles
    // that was copy before.
    sheet.SetCell("B", 2, styleR, "Some");
    sheet.SetCell("D", 2, styleG, "Simple");
    sheet.SetCell("F", 2, styleB, "Text");
    sheet.SetCell("B", 4, styleR, 10);
    sheet.SetCell("D", 4, styleG, 12.3);
    sheet.SetCell("F", 4, styleB, '-');

    // Save all updates.
    excel.Save();
}

*The Plan1 name used in GetSheet is the same of the excel, see the image below:

The image below is the result of the code:

Sample 02

Let's do something more interesting and functional, an employee data sheet. So first we make a excel like:

Then we need get data from some database. I won't specify how this can be done it's not the point here. Instead know there's a previous code that load data in the employees variable. Assume that variable employees implements IEnumerable<Employee> Where:

public class Employee
{
    public long ID { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public DateTime Birthday { get; set; }
    public decimal Salary { get; set; }
    public DateTime StartDate { get; set; }
}

So the code will be like:

using (var fs = new FileStream(fileOut, FileMode.Open))
{
    var excel = new Excel(fs);
    var sheet = excel.GetSheet("Plan1");

    // Copy the styles of the existing cells to use later.
    var styleInt = sheet.GetCellStyle("A", 2);
    var styleFloat = sheet.GetCellStyle("E", 2);
    var styleString = sheet.GetCellStyle("B", 2);
    var styleDate = sheet.GetCellStyle("D", 2);

    // Clean all data of line 2 and below. Leave the header intact.
    sheet.ClearAllSheetDataBelow(2);

    uint line = 2;

    foreach (var employee in employees)
    {
        sheet.SetCell("A", line, styleInt, employee.ID);
        sheet.SetCell("B", line, styleString, employee.Name);
        sheet.SetCell("C", line, styleString, employee.Email);
        sheet.SetCell("D", line, styleDate, employee.Birthday);
        sheet.SetCell("E", line, styleFloat, employee.Salary);
        sheet.SetCell("F", line, styleDate, employee.StartDate);

        line++;
    }

    // Save all updates.
    excel.Save();
}

*Notice that you don't need to copy every column style, do column D and F the style of date is the same so you can use it to both.

The result of this code (Depending on the data you get from your database) will be something like that:

ccexcelwriter's People

Contributors

jonataspiazzi avatar

Watchers

James Cloos avatar  avatar

ccexcelwriter's Issues

SetCell overload conflicts

When using SetCell method with 4 parameters (column, line, style, value) if the value used is a string the overload selected is the overload with 5 parameters (column, line, style, format, args). Depending on the scenario is possible to get a null exception.

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.