Create excel file with c#

Asked

Viewed 6,462 times

0

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

Someone can help me?

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

  • Take a look at NPOI: https://npoi.codeplex.com/

  • 1

    Create the class to generate Excel? Generate Excel with the data of a class? What exactly would it be?

  • http://csharp.net-informations.com/excel/csharp-create-excel.htm

  • Good afternoon friend, the idea and generate an excel file with a menu and a line with values .

  • I believe this will help you, the excel file will be created from a datatable, take a look at Link

  • 3

    I didn’t understand the menu part. What would it have. And the values? Where do they come from? Is it one line or several? Edit your question and ask for more details, please.

  • If it’s something simple, just with numbers, there are various ways of doing this programmatically, manipulating elements. But if it is something more complex, with formatting, and if you already know the report tool of visual studio (Crystal should also allow this, I do not know), a good option is to create a report and export to xls. If you need maintenance, it’ll be a lot easier. Here is a good tutorial: https://www.codeproject.com/Tips/888937/RDLC-Export-Directly-to-Word-Excel-PDF-from-Cod

  • Leo Longhi. what I was referring to menu and values would be the same thing as: column name, example: "NAME" column information in the bottom row, example: "Joao"

  • I’ll answer your question. I think it’s nice you take a look at this, to make the most of the site.

Show 4 more comments

2 answers

7

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

I’ll make a small tutorial for you on how to use.

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 Class C#.

0

I believe that to have a control at this level of an excel without buying a third lib you can use the Interop of the. net framework itself.

https://msdn.microsoft.com/pt-BR/library/dd264733.aspx

And here is a complete guide on how to create excel spreadsheets using Interop

https://msdn.microsoft.com/pt-br/library/ms173186(v=vs.80). aspx

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

  • 1

    That answer is of extremely low quality because it essentially only contains links. You could have suggested the links by comment. To improve the answer, I suggest editing it and adding an example of code, or better yet, a summary of what is described in the link.

  • @Luizvieira I agree with you that responses with only links are usually considered bad. But for this question I think appropriate because there are several tutorials on the internet on how to do this and I don’t think there is value in playing them here. Maybe the problem is the question itself.

  • @Alexdev The question certainly has problems. I voted to close it as not clear why the author says "I’m not getting it", but does not explain what the difficulty is (if it is a compilation error, if it does not generate the expected result, etc). That does not validate the answer, that as you yourself agree has its own problems. The content of the answer is not bad or useless, and I agree with you: it is appropriate... but for a comment. :)

  • @Alexdev Ah, and about "there is no value in playing (content) here", this is quite debatable. Any content exists out there, so that same line of reasoning would lead you to believe that there is also no value in ask this way. If the (a) AP came here to ask is because he(a) has difficulties, and the idea of this site is to collaboratively build a knowledge base. Links additional are welcome, but responses only with links are highly unwanted.

  • @Luizvieira my intention was that the person who asked the question followed the links to start trying to do what she wanted and only then she would have real doubts (putting her hand in the dough) that she could post here and then would complement the answer according to the questions that arise. But as I wrote in haste here I agree that the answer was vague.

  • 2

    It’s a great and noble intention, Erick. But it’s okay to comment on it. You, by the way, have enough reputation to comment on every publication. The comment serves that same purpose, and you avoid the risk of taking negativities (I’m not the one who negatively, okay? But unfortunately this happens very often in cases where a conversation like this would solve).

  • 1

    For this question the essential answer is only the name of the library or technique. This is enough for the OP to search how to do this. The link is just to save time. But as Luiz said, maybe the best place for this is in a comment.

  • I understand everyone’s point, although I don’t think the answer should be negative when it’s not wrong, it’s just not the best answer it could be. In any case, I understand everyone’s opinion. I will keep the answer here to keep track of the comments that I think is a very useful and valid discussion.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.