
18/01/2011, 15:02
|
 | | | Fecha de Ingreso: noviembre-2008 Ubicación: Santiago
Mensajes: 172
Antigüedad: 16 años, 3 meses Puntos: 5 | |
Respuesta: Insertar datos en un excel ???? aca te dejo un ejemplo de como escribir en un excel usando interop
Código:
Excel.Application excelApp = new Excel.ApplicationClass();
Excel._Worksheet worksheet = null;
Excel._Workbook workbook = null;
SaveFileDialog dlgs = new SaveFileDialog();
string filename = "";
try
{
if (excelApp != null)
{
FileInfo sPathApp = new FileInfo(dlg.FileName);
string sPathAppFile = Convert.ToString(sPathApp.Name);
workbook = (Excel._Workbook)excelApp.Workbooks.Open(dlg.FileName, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
workbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);//crea nueva hoja
worksheet = (Excel._Worksheet)workbook.ActiveSheet; // activa la Hoja
worksheet.Name = "Hojatest";// cambia nombre a hoja
int totalSheets = workbook.Sheets.Count;
worksheet.Move(Type.Missing, workbook.Worksheets[totalSheets]);
worksheet.Cells[4, 1] = "texto";
worksheet.Cells[4, 2] = "De";
worksheet.Cells[4, 3] = "Pruebas";
worksheet.Cells[4, 4] = "-------";
}
}
workbook.Close(null, null, null);
excelApp.Workbooks.Close();
excelApp.Quit();
MessageBox.Show("Exportacion a Excel Finalizada");
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
workbook = null;
excelApp = null;
GC.Collect();
en todo caso, debes tomar en cuenta que este metodo, a veces tiene el inconveniente que no cierra los procesos excel en la memporia, por lo qu ea la larga, puede provocar mas problemas.
existen otras herramientas como aspose que facilitan aun mas este procedimiento.
saludos
plch
__________________ Nada mas patetico que "detestar" a Intel o Microsoft o Windows o Apple ... apuesto que eres el tipico teton espinilluo y pajero que usa Linux para sentirse capo ... |