Tengo una tabla de datos y quiero que la exporte a archivo Excel, es una aplicación wpf y todas las soluciones que he encontrado son para la aplicación web asp.net por favor ayuda ...Cómo exportar de tabla de datos a archivo excel en wpf C#
6
A
Respuesta
8
sólo para que sea más visible, para todos
Microsoft.Office.Interop.Excel.Application excel = null;
Microsoft.Office.Interop.Excel.Workbook wb = null;
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.Worksheet ws = null;
Microsoft.Office.Interop.Excel.Range rng = null;
try
{
excel = new Microsoft.Office.Interop.Excel.Application();
wb = excel.Workbooks.Add();
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
for (int Idx = 0; Idx < dt.Columns.Count; Idx++)
{
ws.Range["A1"].Offset[0, Idx].Value = dt.Columns[Idx].ColumnName;
}
for (int Idx = 0; Idx < dt.Rows.Count; Idx++)
{ // <small>hey! I did not invent this line of code,
// I found it somewhere on CodeProject.</small>
// <small>It works to add the whole row at once, pretty cool huh?</small>
ws.Range["A2"].Offset[Idx].Resize[1, dt.Columns.Count].Value =
dt.Rows[Idx].ItemArray;
}
excel.Visible = true;
wb.Activate();
}
catch (COMException ex)
{
MessageBox.Show("Error accessing Excel: " + ex.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.ToString());
}
4
Puede guardar un .csv (archivo de valores separados por comas) de su tabla de datos. Este archivo se puede abrir en Excel.
Además: Ya sea WPF o Winforms, la conversión es igual en ambos porque su código de conversión está escrito en su idioma, es decir, C# y no es específico de WPF o Winforms.
2
Para mí trabajar muy bien gracias ... alguien como vb.net?
Dim excel As Microsoft.Office.Interop.Excel.Application = Nothing
Dim wb As Microsoft.Office.Interop.Excel.Workbook = Nothing
Dim missing As Object = Type.Missing
Dim ws As Microsoft.Office.Interop.Excel.Worksheet = Nothing
Dim rng As Microsoft.Office.Interop.Excel.Range = Nothing
Sub ExcelFile (ByVal dt Como DataTable)
Try
excel = New Microsoft.Office.Interop.Excel.Application()
wb = excel.Workbooks.Add()
ws = DirectCast(wb.ActiveSheet, Microsoft.Office.Interop.Excel.Worksheet)
For Idx As Integer = 0 To dt.Columns.Count - 1
ws.Range("A1").Offset(0, Idx).Value = dt.Columns(Idx).ColumnName
Next
For Idx As Integer = 0 To dt.Rows.Count - 1
' <small>hey! I did not invent this line of code,
' I found it somewhere on CodeProject.</small>
' <small>It works to add the whole row at once, pretty cool huh?</small>
' YES IT'S COOL Brother ...
ws.Range("A2").Offset(Idx).Resize(1, dt.Columns.Count).Value = dt.Rows(Idx).ItemArray
Next
excel.Visible = True
wb.Activate()
Catch ex As Exception
MessageBox.Show("Error accessing Excel: " & ex.ToString())
End Try
End Sub
1
una manera
ArrayList arr = (ArrayList)dataGridView.DataSource;
dt = ArrayListToDataTable(arr);
dataTable2Excel(dt, dataGridView, pFullPath_toExport, nameSheet);
http://www.codeproject.com/Articles/30169/Excel-export-from-DatagridView
http://support.microsoft.com/default.aspx?scid=kb;en-us;317719
Cuestiones relacionadas
- 1. Exportar a Excel C#
- 2. Exportar tabla HTML a Excel
- 3. ASP.Net Exportar datos a Excel
- 4. Cómo exportar datos de tabla al archivo
- 5. Exportar tabla HTML a Excel utilizando ASP.NET
- 6. Exportar datos de consulta SQL a Excel
- 7. Cómo exportar datos a un archivo Excel utilizando PHPExcel
- 8. Exportar tabla HTML a Excel caracteres especiales de JavaScript cambiaron
- 9. C# (WinForms-App) exportar DataSet a Excel
- 10. Cómo exportar los datos de hoja de cálculo Excel a SQL Server 2008 tabla
- 11. Exportar datos de SQLite a Excel en iOS programáticamente
- 12. Exportar SQL a Excel
- 13. T-SQL: Exportar a un nuevo archivo de Excel
- 14. Cómo exportar datos a Excel usando LINQ to Entity?
- 15. datagridview exportar a excel
- 16. Exportar de PHP a Excel
- 17. ¿Cómo exportar una cuadrícula de datos a un archivo de Excel en Flex?
- 18. Cómo exportar datos de C++ a MatLab
- 19. Cómo convierte el archivo de Excel a la tabla de datos C#?
- 20. Exportar a archivo de Excel en MVC3 ASP.net
- 21. php + jqgrid + exportar a excel
- 22. Exportar tabla HTML a excel - usando jQuery o Java
- 23. Exportar a Excel en Asp.net MVC
- 24. Cómo exportar una tabla al formato csv o excel
- 25. Consulta de MY-SQL para exportar datos en el archivo de Excel
- 26. Exportar registros de Datagridview a Excel
- 27. Exportar mi Google Chart (tabla) de datos a excel o csv
- 28. ¿Cómo puedo exportar datos repetidos simples de excel como xml?
- 29. jQuery DataTables: exportar columnas individuales a Excel
- 30. exportar a Excel en jquery o jqGrid
gracias! gran ejemplo. :) – asuciu