Thanks Gile for the update.
Here's an example that I wrote using the ExcelReader.RangetoDataTable() method to import data from an Excel Workbook in an AutoCAD table:
/// <summary>
/// Opens an Excel file using the ExcelReader class
/// reads the values (from a specific sheet if necessary)
/// creates an AutoCAD Table with the values
/// Originally copied from here: http://bit.ly/wtBLVZ
/// </summary>
[CommandMethod("test4")]
public void test4()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Stopwatch sw = Stopwatch.StartNew();
//object[][] values;
System.Data.DataTable table;
using (ExcelReader xlr = new ExcelReader(@"C:\temp\test1.xls"))
{
table= xlr.RangeToDataTable(1,1);
}
if (table == null)
return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord btrMs = (BlockTableRecord)bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForRead);
Table bomTable = new Table();
bomTable.TableStyle = db.Tablestyle;
bomTable.Position = Point3d.Origin;
TableStyle ts = (TableStyle)tr.GetObject(bomTable.TableStyle, OpenMode.ForRead);
double txtHeight = ts.TextHeight(RowType.DataRow);
//add the correct number of rows & columns
bomTable.InsertColumns(1,1,table.Columns.Count);
bomTable.InsertRows(1,1,table.Rows.Count);
//write the header row.
DataRow row = table.Rows[1];
for (int i = 0; i < table.Columns.Count; i++)
{
bomTable.Cells[0, i].TextString = table.Rows[0][i].ToString();
}
// then ALL the data rows
for (int i = 1; i < table.Rows.Count; i++)
{
for (int j = 1; j < table.Columns.Count; j++)
{
bomTable.Cells[i, j].TextString = table.Rows[i][j].ToString();
}
}
bomTable.GenerateLayout();
btrMs.UpgradeOpen();
btrMs.AppendEntity(bomTable);
tr.AddNewlyCreatedDBObject(bomTable, true);
tr.Commit();
}
sw.Stop();
AcadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage(
"\nPasteFromClipboard: {0}", sw.Elapsed.TotalMilliseconds);
}
It has an issue to do with the column heads and rows within the AutoCAD table itself, but I think that's due to my not having a style setup in the .dwg file already?