
In previous articles about tables in MultiCAD.NET, we talked about programmatically
creating and formatting tables in a drawing, using different types of data as content, and also using table
templates . In this article, we will continue the story of using templates and take a closer look at the API, which allows you to save a table to an external file as a template and load it into a drawing to form generic tables. In the second half of the article will be considered the process of data exchange with Microsoft Excel.
Source table
Let the source table contain the characteristics of a metal profile - the name, brand, thickness of steel, the purpose - the structure of which can be used to describe various types of profile: guide, rack, etc .:
')

We will not dwell on the procedure for creating such a table: we wrote about how to create and edit tables in the previous
article .
Save table as template
To save the table to a separate file, use the
McTable.SaveToFile()
method. The method allows you to write a table in the following file formats:
- table files (.dat),
- Tab-delimited text (.txt)
- semicolon-separated 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; }
Load table from file
The table is
McTable.LoadFromFile()
from an external file using the
McTable.LoadFromFile()
method. As with
SaveToFile()
, the file name can be specified explicitly or selected from the dialog. The following file formats are supported:
- Table files (.tbl, .dat),
- text formats (.txt, .csv, .xml),
- Microsoft Access files (.mdb, .accdb),
- Microsoft Excel workbook (.xls, .xlsx),
- OpenOffice.org tables (.ods),
- StarOffice tables (.sxc).
The following command adds a table to the drawing from the file selected by the user:
[CommandMethod("smpl_LoadTable", CommandFlags.NoCheck | CommandFlags.NoPrefix)] public void smpl_LoadTable() { McTable Table2 = new McTable();
Now we can use the saved table as a template for adding standard tables to the drawing:

Data Exchange 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 sheet and back.
When the
ExportToExcel()
method is
ExportToExcel()
a new Excel workbook is opened and all the table data is transferred to it with preservation of the cell formatting set in the table.
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 sheet, for which you need to call the
ImportFromExcel()
function.
The following command inserts the selected range of cells from the Excel sheet into the 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 complete the overview of fixed assets for working with tables in the MultiCAD.NET API. Of course, this is far from all the possibilities provided by the tabular functionality, especially since at present the work on its expansion and improvement continues.
If some aspects of working with the tables are not sufficiently illuminated, 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