Solved! Go to Solution.
Solved by Keith.Brown. Go to Solution.
Solved by norman.yuan. Go to Solution.
@ilovejingle wrote:
I am trying to find a good approach to this so I can research more details myself
Getting data from Excel sheet for AutoCAD application is one of the most often asked questions. If you search this forum, or AutoCAD VBA (Visual Basic) forum, or the Internet, you would get tons of links. Since you'll do more detailed research, here are my suggestions (since you post in AutoCAD .NET API forum, I assume your are doing .NET API CAD app):
1. Basically, you have at least 3 usual options to get data for AutoCAD
Among these 3 of options, OpenXml would be my first choice, because it eliminates unnecessary dependencies to outside component installation (Excel, Access DB Engine) while OpenXml SDK is free and its DLL goes with your .NET DLL. Then COM automation would be the next, because most likely all your user has Excel installed.
2. If you are somewhat experienced in .NET programming, you would create an Interface to get external data for AutoCAD, so that the AutoCAD side code that uses the data would not care how the data is extracted from Excel sheet (COM automation, Access DB Engine or OpenXml). For example, you could define the interface like:
public interface IExcelSheetData
{
object GetData(string fileName);
}
Then you can implement at least 3 data accessing class for each options aforementioned (of course you only need one. But you may change your idea later and choose different option, so simply implement a different one and without need to change of code AutoCAD side at all).
Hope this points a few directions you could research towards deeper
Thanks, I will research more on the method you recommended to see whether I can figure it out.
I use Gembox.Spreadsheet to read/write excel files. No excel is required and it has a free version which is fully functional but limited to 150 rows per sheet and 3 sheets. Depending on your criteria, it might just work for you.
The full version is expensive but if you do alot of work with excel files then it is well worth it.
Another free alternative is SpreadsheetLite
Try OLEDB
System.Data.OleDb.OleDbConnection MyConnection ; System.Data.DataSet DtSet ; System.Data.OleDb.OleDbDataAdapter MyCommand ; MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;"); MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection); MyCommand.TableMappings.Add("Table", "TestTable"); DtSet = new System.Data.DataSet(); MyCommand.Fill(DtSet); dataGridView1.DataSource = DtSet.Tables[0]; MyConnection.Close();
More on...read excel in C#