-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoExcel.cs
40 lines (38 loc) · 1.38 KB
/
toExcel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.IO;
using OfficeOpenXml;
namespace Registro_Alumnos_profesores
{
class toExcel
{
//Exportar a Excel
public static void ExportarAExcel(DataTable datos, string nombreArchivo)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage())
{
var worksheet = package.Workbook.Worksheets.Add("Datos");
worksheet.Cells["A1"].LoadFromDataTable(datos, true);
var stream = new MemoryStream(package.GetAsByteArray());
var saveFileDialog = new Microsoft.Win32.SaveFileDialog
{
Filter = "Archivo de Excel (*.xlsx)|*.xlsx",
FileName = nombreArchivo
};
if (saveFileDialog.ShowDialog() == true)
{
using (var fileStream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write))
{
stream.WriteTo(fileStream);
}
}
}
}
//------------------------------------------------------------------------------------------------------------------------
}
}