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.
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.
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.
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());
}
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.
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.
And here's a complete guide to how to create excel spreadsheets using Interop
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.