Create Excel file with C # [closed]

0

I have a problem: I have to generate a class in C # to create an Excel file, but I'm not getting it.

Can anyone help me?

The idea is to generate an Excel file with a menu and a line with values.

    
asked by anonymous 02.03.2017 / 19:58

2 answers

6

The best tool I know to do what you need is EPPlus . There are several answers from me on the site .

I'll give you a little tutorial on how to use it.

The minimum of the minimum

using (var excelPackage = new ExcelPackage())
{
    excelPackage.Workbook.Properties.Author = "Jaderson Pessoa";
    excelPackage.Workbook.Properties.Title = "Meu Excel";

    // Aqui você coloca a lógica que precisa escrever nas planilhas.

    string path = @"C:\teste.xlsx";
    File.WriteAllBytes(path, excelPackage.GetAsByteArray());
}

Adding a spreadsheet and writing to it

using (var excelPackage = new ExcelPackage())
{
    excelPackage.Workbook.Properties.Author = "Jaderson Pessoa";
    excelPackage.Workbook.Properties.Title = "Meu Excel";

    // Aqui simplesmente adiciono a planilha inicial
    var sheet = excelPackage.Workbook.Worksheets.Add("Planilha 1");
    sheet.Name = "Planilha 1";

    // Títulos
    var i = 1;
    var titulos = new String[] { "Título Um", "Título Dois", "Título Três" };
    foreach (var titulo in titulos)
    {
        sheet.Cells[1, i++].Value = titulo;
    }

    // Valores
    i = 1;
    var valores = new String[] { "1", "2", "3" };
    foreach (var valor in valores)
    {
        // Aqui escrevo a segunda linha do arquivo com alguns valores.
        sheet.Cells[2, i++].Value = valor;
    }

    string path = @"C:\teste.xlsx";
    File.WriteAllBytes(path, excelPackage.GetAsByteArray());
}

I think this is a good start for your C # class.

    
03.03.2017 / 18:01
0

I think that to have a control at this level of an excel without having to buy a third party lib you can use the Interop of the same .net framework.

link

And here's a complete guide to how to create excel spreadsheets using Interop

link

Try to follow the content of these links and complement your question with questions about the implementation itself, so I can either complement the answer in order to help you as other people can also help you better.

    
03.03.2017 / 13:23