Problem Load Data Excel with Microsoft.Office.Interop.Excel for .NET 4.8

Problem Load Data Excel with Microsoft.Office.Interop.Excel for .NET 4.8

08130182
Contributor Contributor
722 Views
3 Replies
Message 1 of 4

Problem Load Data Excel with Microsoft.Office.Interop.Excel for .NET 4.8

08130182
Contributor
Contributor

Hola a todos:

 

En este día quiero ejecutar una parcela en AutoCAD 2024 a través de .NET (Biblioteca de clases - Framework 4.8), en la parcela un botón al hacer 'clic' debería abrir un 'filedialog' y cargar los datos de excel y renderizar en un DataGridView.

 

En el caso de AutoCAD no funciona el clic

08130182_1-1719726644246.png

Figura 01: Clic en 'Cargar archivo' y no funciona el botón.

 

A continuación, intento ejecutar un ejemplo mientras se ve un tutorial

(https://www.youtube.com/watch?v=LDq4C_wF0fs) , pero aparece otro error, para este caso el problema es diferente, i cuando se crea una nueva configuración de solución como (Aplicación de Windows Forms - .NET Framework 4.8), pero no se pueden cargar los datos de Excel. Reviso una pregunta en stackOverFlow, pero cambio en la compilación para 32 bits y el problema continúa. Ayúdame por favor

 

 

08130182_0-1719726299851.png

Figura 02: Ejecución del código replicado del tutorial

0 Likes
723 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

You should post your code (using the "</>" button above the message window), instead of showing a picture with only partial code barely readable).

 

Do you have to actually make Excel running as separate application in parallel along with AutoCAD in the workflow? Or, you actually ONLY need read data from the sheet(s) in a *.xlsx file? If it is the latter, automating Excel app via COM is a bad solution: the data in *xlsx file can be easily read (or even saved back) without having to make Excel app runs, or installed at all. There are many free/open source .NET libraries one can use to access data in *.xlsx file. 

 

Here is one of the tutorial in youtube:

https://www.youtube.com/watch?v=5U2qf52PTTY 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

08130182
Contributor
Contributor

dear friend, in the next code i have a problem with the word 'DataTable', is this beacause DataTable appear content in 'using System.Data;' and 'using Autodesk.AutoCAD.DatabaseServices;':

 

 

help  me please, the code is:

 

using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
// HOW TO IMPORT EXCEL FILE TO DATAGRIDVIEW AND SAVE TO DATABASE USING C#
// using Microsoft.Office.Interop.Excel;
// C# Tutorial - Open and Read Excel Files (*.xls/*.xlsx) | FoxLearn
using ExcelDataReader;
using System.IO;
//using System.Data;


namespace DisanV01.Forms
//namespace DisanV01
{
public partial class UI_Inicio02_MetodoDeHunter : UserControl
{
public UI_Inicio02_MetodoDeHunter()
{
InitializeComponent();
}

private void label1_Click(object sender, EventArgs e)
{

}
private void cboSheet_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = tableCollection[cboSheet.SelectedItem.ToString()];
// dataGridView1.datasource
// dataGridView1.DataSource = dt;
dataGridView1.DataSource = dt;
}
private void button3_Click(object sender, EventArgs e)
{

}

DataTableCollection tableCollection;

private void inicio_metododehunter_004_btnImport_Click(object sender, EventArgs e)
{
//HOW TO IMPORT EXCEL FILE TO DATAGRIDVIEW AND SAVE TO DATABASE USING C#
//Microsoft.Office.Interop.Excel.Application xlApp;
//Microsoft.Office.Interop.Excel.Workbook xlWorkbook;
//Microsoft.Office.Interop.Excel.Worksheet xlWorksheet;
//Microsoft.Office.Interop.Excel.Range xlRange;

//int xlRow;
//string strFileName;

//openFD.Filter = "Excel Office |*.xls; *.xlsx";
//openFD.ShowDialog();
//strFileName = openFD.FileName;

//if(strFileName != "")
//{
// xlApp = new Microsoft.Office.Interop.Excel.Application();
// xlWorkbook = xlApp.Workbooks.Open(strFileName);
// xlWorksheet = xlWorkbook.Worksheets["Hoja1"];
// xlRange = xlWorksheet.UsedRange;

// int i = 0;

// for (xlRow = 2; xlRow <= xlRange.Rows.Count; xlRow++)
// {
// i++;
// inicio_metododehunter_006.Rows.Add(i, xlRange.Cells[xlRow, 1].Text, xlRange.Cells[xlRow, 2].Text);
// }
// xlWorkbook.Close();
// xlApp.Quit();
//}

// C# Tutorial - Open and Read Excel Files (*.xls/*.xlsx) | FoxLearn
{
using(OpenFileDialog openFD=new OpenFileDialog() { Filter= "Excel 97-2003 Workbook|*.xls|Excel Workbook|*.xlsx" })
{
if (openFD.ShowDialog() == DialogResult.OK)
{
txtFilename.Text = openFD.FileName;
using(var stream=File.Open(openFD.FileName, FileMode.Open, FileAccess.Read))
{
using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
{
DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
});
tableCollection = result.Tables;
cboSheet.Items.Clear();
foreach (DataTable table in tableCollection)
cboSheet.Items.Add(table.TableName);

}
}
}
}

}


}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{

}

private void label2_Click(object sender, EventArgs e)
{

}

private void label3_Click(object sender, EventArgs e)
{

}


}
}

0 Likes
Message 4 of 4

_gile
Consultant
Consultant

To avoid conflict between Autodesk.AutoCAD.DatabaseServices.DataTable and System.Data.DataTable, you can simply use the full name (as upper) or use a using alias directive.

 

Please, use the 'Insert/Edit code sample' button [</>] when you paste code.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes