SoloCodigo

CLR: .Net / Mono / Boo / Otros CLR => C# => Mensaje iniciado por: cabromex en Martes 3 de Diciembre de 2013, 03:21

Título: PROBLEMAS CON EXCEL CONNECTION STRING
Publicado por: cabromex en Martes 3 de Diciembre de 2013, 03:21
SALUDOS.

TENGO UN PROBLEMA CON UN CONNECTION STRING A UNA BASE DE DATOS DE EXCEL, LO QUE PRETENDO ES QUE ESTA SE ABRA PARA QUE EL PROGRAMA LE DE LECTURA. YA LA EH TRATADO MIL VECES PERO ME SIGUE APARECIENDO UN ERROR QUE DICE: An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in ADMBD2.exe

Additional information: No se encontró "Provider=microsoft.ACE.oledb.4.0;data source=C:\Users\erik\Documents\LP_01-nov.xls;extended properties="excel 12.0;hdr=No;IMEX=1;"". Compruebe la ortografía del nombre del archivo y si su ubicación es correcta


namespace ADMBD2
{
    public partial class Form1 : Form
    {
        public string FilePath;
        public string xlsFilePath;
        public Form1()
        {
            InitializeComponent();
        }

        public void btn_buscar_Click(object sender, EventArgs e)
        {
            //BUESQUEDA DE TABAL DE EXCEL
           
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Busqueda de Tablas";
            fdlg.InitialDirectory = @"c:\";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;

            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                txt_buscar.Text = fdlg.FileName;
                FilePath = fdlg.FileName;
               
            }

            try
            {
           
                MessageBox.Show("Se ha seleccionado tabla de excel ! "); //ESTE ES EL PATH QUE SUPUESTAMENTE NO ES VALIDO.
                xlsFilePath = @"Provider=microsoft.ACE.oledb.4.0;data source=" + FilePath + ";extended properties=" + "\"excel 12.0;hdr=No;IMEX=1;\"";
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
           
        }


        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void btn_cargar_Click(object sender, EventArgs e)
        {
           
            Excel.Application xlApp;
            Excel.Workbook xlWorkbook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;
            var misValue = Type.Missing;
           
            //abrir el documento
           
            xlApp = new Excel.Application();
            //error en el path al archivo excel
            xlWorkbook = xlApp.Workbooks.Open(xlsFilePath,misValue,misValue,misValue,misValue,misValue,misValue,misValue,misValue,misValue,misValue,misValue,misValue);

            //seleccion de la hoja de calculo
            xlWorkSheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1);
            //seleccion de rango
            range = xlWorkSheet.UsedRange;

            //leer las celdas
            int rows = range.Rows.Count;
            int cols = range.Columns.Count;

            for (int row = 1; row <= rows; row++)
            {
                for (int col = 1; col <= cols; col++)
                {
                    //lectura como cadena
                    string str_value = (range.Cells[row, col] as Excel.Range).Value2.ToString();

                    //conversion
                    int int_value = Convert.ToInt32(str_value, 10);

                    Console.WriteLine("string:{0}", str_value);
                    Console.WriteLine("int:{0}", int_value);
                }
            }


        }

        private void btn_salir_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


AQUI LES DEJO EL CODIGO
Título: Re:PROBLEMAS CON EXCEL CONNECTION STRING
Publicado por: .net en Martes 7 de Enero de 2014, 16:34
cambia excel 12.0 por excel 8.0 y luego mira revisa esto

http://www.connectionstrings.com/excel/ (http://www.connectionstrings.com/excel/) aqui te da los connectionstring correctos para establecer una conexion con excel


espero te ayude

aparte debes de tener instalado en el servidor Access DataBaseEngine para establecer conexiones con excel, access,etc.

mira el link http://www.microsoft.com/es-mx/download/details.aspx?id=13255 (http://www.microsoft.com/es-mx/download/details.aspx?id=13255)