using Excel = Microsoft.Office.Interop.Excel; using System.Data.OleDb; using System.Data.SqlClient; namespace ADMBD2 { public partial class Form1 : Form { public string FilePath; 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; if (Path.GetExtension(FilePath) == ".xls") { try { btn_cargar.Enabled = true; MessageBox.Show("Se ha seleccionado tabla de excel ! "); } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show("No es una tabla de excel"); btn_cargar.Enabled = false; return; } } } private void Form1_Load(object sender, EventArgs e) { } private void btn_cargar_Click(object sender, EventArgs e) { if (Path.GetExtension(FilePath) == ".xls") { try { MessageBox.Show("Conexion Exitosa Tabla de Excel"); Excel.Application xlApp; Excel.Workbook xlWorkbook; Excel.Worksheet xlWorkSheet; Excel.Range range; var misValue = Type.Missing; //abrir el documento xlApp = new Excel.Application(); xlWorkbook = xlApp.Workbooks.Open(FilePath, 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; xlApp.Visible = true; //FALLA AL MOMENTO DE LLEGAR A ESTA PARTE for (int row = 6; row <= rows; row++)//ERROR HERE { for (int col = 1; col <= cols; cols++) { //lectura como cadena string str_value = (range.Cells[row, cols] as Excel.Range).Value2.ToString(); label1.Text = str_value; //conversion int int_value = Convert.ToInt32(str_value, 10); Console.WriteLine("string:{0}", str_value); Console.WriteLine("int:{0}", int_value); //I HAVE NO IDEA OF WHAT I WAS TRYING TO DO HERE /* string val1 = (string)xlWorkSheet.get_Range("string:{0}", misValue).Text; label1.Text = val1; string row; label1.Text = row;*/ } } // cerrar xlWorkbook.Close(false, misValue, misValue); xlApp.Quit(); // liberar releaseObject(xlWorkSheet); releaseObject(xlWorkbook); releaseObject(xlApp); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } } } public static void releaseObject(object obj) //JUST COPY THIS PART IM NOT SURE WHAT IT DOES { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { Console.WriteLine("Unable to release the object(object:{0})", obj.ToString()); } finally { obj = null; GC.Collect(); } } private void btn_salir_Click(object sender, EventArgs e) { this.Close(); } private void btn_limpiar_Click(object sender, EventArgs e) { txt_buscar.Clear(); } private void label1_Click(object sender, EventArgs e) { } } }
Hello.
Im tryingo to make a program that reads an excel table and migrated to sql for a college work. I almost complete all the code, based in some codes founded in the internet. But now I am having this error: Cannot make a link at runtime on a null reference poping out from a catch when I start reading the table. I need to star reading from row 6 column A. I make two for cicles where it should be reading first the rows and then the columns. Im a nooby on this if someone can give me a clue of how to solve that error, I pretty lost right now.
EXCUSE MI CONFUSING CODE, I TRIED MY BEST.