It will work if we use UpdateOption.UpdateColumnWidth and UpdateOption.UpdateRowWidth on update datalink. The code from "Through the Interface" (http://through-the-interface.typepad.com/through_the_interface/2007/08/creating-an-aut.html) can be rewritten as:
[CommandMethod("TFS")]
public static void TableFromSpreadsheet()
{
// Hardcoding the string
// Could also select for it
const string dlName = "Import table from Excel demo";
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
OpenFileDialog ofd =
new OpenFileDialog(
"Select Excel spreadsheet to link",
null,
"xls; xlsx",
"ExcelFileToLink",
OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles
);
DialogResult dr = ofd.ShowDialog();
if (dr != System.Windows.Forms.DialogResult.OK)
return;
ed.WriteMessage("\nFile selected was \"{0}\".", ofd.Filename);
PromptPointResult ppr = ed.GetPoint("\nEnter table insertion point: ");
if (ppr.Status != PromptStatus.OK)
return;
// Remove the Data Link, if it exists already
DataLinkManager dlm = db.DataLinkManager;
ObjectId dlId = dlm.GetDataLink(dlName);
if (dlId != ObjectId.Null)
{
dlm.RemoveDataLink(dlId);
}
// Create and add the Data Link
DataLink dl = new DataLink
{
DataAdapterId = "AcExcel",
Name = dlName,
Description = "Excel fun with Through the Interface",
ConnectionString = ofd.Filename,
DataLinkOption = DataLinkOption.PersistCache
};
dl.UpdateOption |= (int)UpdateOption.AllowSourceUpdate;
dlId = dlm.AddDataLink(dl);
Transaction tr = doc.TransactionManager.StartTransaction();
using (tr)
{
tr.AddNewlyCreatedDBObject(dl, true);
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
Table tb = new Table { TableStyle = db.Tablestyle, Position = ppr.Value };
tb.SetDataLink(0, 0, dlId, true);
tb.UpdateDataLink(UpdateDirection.SourceToData, UpdateOption.UpdateColumnWidth | UpdateOption.UpdateRowHeight);
tb.GenerateLayout();
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(tb);
tr.AddNewlyCreatedDBObject(tb, true);
tr.Commit();
}
// Force a regen to display the table
ed.Regen();
}
-Khoa