Working with tables in MultiCAD.NET. Part 3. External table files and data exchange with Microsoft Excel

In previous articles about tables in MultiCAD.NET, we talked about the programmatic creation and formatting of tables in a drawing, the use of various data types as content, and the use of table templates . In this article, we will continue the story about the use of templates and look at the API in more detail, which allows you to save the table to an external file as a template and load it into a drawing to form typical tables. In the second half of the article, the process of exchanging data with Microsoft Excel will be considered.
Source table
Let the source table contain the characteristics of the metal profile — name, grade, thickness of steel, purpose — the structure of which can be used to describe various types of profiles: guide, rack, etc.:

We will not stop at the procedure for creating such a table: how to create and we edited the tables earlier in the previous article .
Saving a table as a template
To save the table to a separate file, use the method
McTable.SaveToFile()
. The method allows you to write a table to files in the following formats:- table files (.dat),
- Tab delimited text (.txt)
- semicolon-delimited text (.csv),
- XML
- Microsoft Excel workbook (.xls),
- OpenOffice.org tables (.ods).
The table that you want to save as a template can be selected in the drawing. The following command saves the selected table in one of the available formats:
[CommandMethod("smpl_SaveTable", CommandFlags.NoCheck | CommandFlags.NoPrefix)]
public void smpl_SaveTable()
{
McObjectId idSelected = McObjectManager.SelectObject("Укажите таблицу для записи:");
if (idSelected.IsNull)
{
return;
}
McTable outObj = McObjectManager.GetObject(idSelected);
if (outObj == null)
{
return;
}
// Без указания параметров метод SaveToFile() вызовет диалог,
// в котором пользователю будет предложено выбрать имя и формат файла.
outObj.SaveToFile();
}
Loading a table from a file
The table is loaded from an external file using the method
McTable.LoadFromFile()
. As for SaveToFile()
, the file name can be specified explicitly or selected from the dialog. The following downloadable file formats are supported:- Table Files (.tbl, .dat),
- text formats (.txt, .csv, .xml),
- Access files (.mdb, .accdb)
- Microsoft Excel workbook (.xls, .xlsx),
- OpenOffice.org tables (.ods),
- StarOffice tables (.sxc).
The following command adds a table from the file selected by the user to the drawing:
[CommandMethod("smpl_LoadTable", CommandFlags.NoCheck | CommandFlags.NoPrefix)]
public void smpl_LoadTable()
{
McTable Table2 = new McTable();
// Без указания параметров метод LoadFromFile() вызовет диалог,
// в котором пользователю будет предложено выбрать имя и формат файла.
if (Table2.LoadFromFile())
{
Table2.PlaceObject(McEntity.PlaceFlags.Silent);
}
}
Now we can use the saved table as a template to add typical tables to the drawing:

Exchange data with Microsoft Excel
In addition to saving tables to files of various formats, MultiCAD.NET contains the ability to quickly transfer tabular data to a Microsoft Excel worksheet and vice versa.
When the method is
ExportToExcel()
called, a new Excel workbook is opened and all tabular data is transferred to it, while the formatting of the cells set in the table is preserved. Add a command that allows you to select a table in the drawing and export it to an Excel sheet:
[CommandMethod("smpl_ExportTableToExcel", CommandFlags.NoCheck | CommandFlags.NoPrefix)]
public void smpl_ExportTableToExcel()
{
McObjectId idSelected = McObjectManager.SelectObject("Укажите таблицу для экспорта:");
if (idSelected.IsNull)
{
return;
}
McTable outObj = McObjectManager.GetObject(idSelected);
if (outObj == null)
{
return;
}
outObj.ExportToExcel();
}
MultiCAD.NET also supports importing a selected range of table cells from an open Excel worksheet, for which you need to call the function
ImportFromExcel()
. The following command inserts a selected range of cells from an Excel sheet into a drawing:
[CommandMethod("smpl_ImportTableFromExcel", CommandFlags.NoCheck | CommandFlags.NoPrefix)]
public void smpl_ImportTableFromExcel()
{
McTable Table2 = new McTable();
if (Table2.ImportFromExcel())
{
Table2.PlaceObject(McEntity.PlaceFlags.Silent);
}
}
With this publication, we conclude our overview of the basic tools for working with tables in the MultiCAD.NET API. Of course, this is far from all the possibilities that table-based functionality provides, all the more so as work is currently underway to expand and improve it.
If some or other aspects of working with tables have remained insufficiently covered, write comments, suggest topics for future articles.
Discussion of the article is also available on our forum: forum.nanocad.ru/index.php?showtopic=6512 .
Translation of the article into English: Tables in MultiCAD.NET. Part 3: Saving and loading table from an external file