Ver Mensaje Individual
  #1 (permalink)  
Antiguo 26/03/2008, 16:40
cjcesar
 
Fecha de Ingreso: febrero-2008
Mensajes: 13
Antigüedad: 16 años, 2 meses
Puntos: 0
exportar datos a excel

hola, tengo una consulta ojala puedan ayudarme: tengo que guardar datos de un archivo en excel (que tiene como 15 columnas y muchas filas) en un nuevo excel; cuando intento pasar datos al excel no se guarda nada en el archivo que se crea, no me sale ningun error, nada.
Cuando hago el debug, en la variable objQryTables (que es de tipo Excel.QueryTables) dice que se produjo la excepcion: System.Runtime.InteropServices.COMException;

ojala alguien tenga la solucion, gracias; mi codigo:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace escritura
{
public partial class Escritura : Form
{
private Excel.Application objExcel = null;
private Excel.Workbooks objBooks = null;
private Excel._Workbook objBook = null;
private Excel.Sheets objSheets = null;
private Excel._Worksheet objSheet = null;
private Excel.Range objRange = null;
private Excel.Font objFont = null;
private Excel.QueryTables objQryTables = null;
private Excel._QueryTable objQryTable = null;
private object objOpt = System.Reflection.Missing.Value;

private object strRutaAGuardar = "C:\\Documents and Settings\\user\\Escritorio\\lecturaCampos\\";
private string strRuta = "C:\\Documents and Settings\\user\\Escritorio\\lecturaCampos\\Registr os.xls";

public Escritura()
{
InitializeComponent();
}
private void btGuardar_Click(object sender, EventArgs e)
{
guardandoEnExcel();
}
public void guardandoEnExcel()
{
objExcel = new Excel.Application();
objBooks = (Excel.Workbooks)objExcel.Workbooks;
objBook = (Excel._Workbook)objBooks.Add(objOpt);

objSheets = (Excel.Sheets)objBook.Worksheets;
objSheet = (Excel._Worksheet)(objSheets.get_Item(1));
objRange = objSheet.get_Range("A1", objOpt);
objQryTables = objSheet.QueryTables;

objQryTable = (Excel._QueryTable)objQryTables.Add(
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
strRuta + ";", objRange, "select Entidad from [Hoja1$]");

objQryTable.RefreshStyle = Excel.XlCellInsertionMode.xlInsertEntireRows;

objBook.SaveAs(strRutaAGuardar + "libro2.xls", objOpt, objOpt,
objOpt, objOpt, objOpt, Excel.XlSaveAsAccessMode.xlNoChange, objOpt, objOpt,
objOpt, objOpt, objOpt);
objBook.Close(false, objOpt, objOpt);
objExcel.Quit();
}
}
}