Creating An AutoCAD To Excel Drafting Application Using C
Creating An AutoCAD To Excel Drafting Application Using C
extract drawing data and exporting it to an Excel spreadsheet. Below are the full steps and C#
code required for this process.
3. Implement C# Code
The following C# code extracts data (lines, circles, and text) from an AutoCAD drawing and
exports it to an Excel file.
[assembly: CommandClass(typeof(AutoCADToExcel.ExportAutoCADData))]
namespace AutoCADToExcel
{
public class ExportAutoCADData
{
[CommandMethod("ExportToExcel")]
public void ExportToExcel()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
try
{
// Start an Excel application
Application excelApp = new Application();
Workbook workbook = excelApp.Workbooks.Add();
Worksheet worksheet = (Worksheet)workbook.Sheets[1];
using (Transaction tr =
db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId,
OpenMode.ForRead);
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForRead);
string filePath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"AutoCADData.xlsx");
workbook.SaveAs(filePath);
workbook.Close();
excelApp.Quit();
How It Works
1. Open AutoCAD.
2. Load the DLL into AutoCAD using:
nginx
CopyEdit
NETLOAD
nginx
CopyEdit
ExportToExcel
Requirements