using Autodesk. AutoCAD. ApplicationServices;
Using Autodesk. AutoCAD. DatabaseServices;
Using Autodesk. AutoCAD. Geometry;
Using Autodesk. AutoCAD. EditorInput;
Using Autodesk. AutoCAD. The Runtime;
Using Excel=NetOffice. ExcelApi;
using System;
Using System. Collections. Generic;
Using DotNetARX;
The namespace NTTC
{
Public class ExcelCAD
{
[CommandMethod (" ExcelToCAD ")]
Public void ExcelToCAD ()
{
Document doc=Application. The DocumentManager. MdiActiveDocument;
The Database db=doc. Database;
The Editor Ed=doc. Editor;
LateBindingApi. Core. Factory. The Initialize ();//initialize the COM object support
Excel. Application excelapp=new Excel. Application ();//that a Excel object
PromptOpenFileOptions opt=new PromptOpenFileOptions (" please select Excel file ");//define the open file dialog box options
Opt. DialogCaption="choose Excel file";//the title of the file dialog
//file dialog box filter list, can be various versions of Excel
Opt. Filter="Excel workbook, 97-2010 (*. XLS) | *. XLS | Excel workbook (*. XLSX) | *. XLSX";
Opt. FilterIndex=0;//file filter list file extensions by default
//according to user selection, access to the file name
PromptFileNameResult fileResult=Ed GetFileNameForOpen (opt);
If (fileResult Status!=PromptStatus. OK) return;
The string filename=fileResult. StringResult;//set file name variable
Using (Transaction trans=db. The TransactionManager. StartTransaction ())
{//open the specified Excel file, obtain the specified page Sheet1
Excel. The Workbook book=excelapp. Workbooks. Open (filename);
Excel. The Worksheet sheet=(. Excel Worksheet) book. Worksheets (" Sheet1 ");
ListTXTS=new List (a);//used to keep a list of points in the name of the
ListPoints=new List (a);//point to hold the position of the list
Application. SetSystemVariable (" PDMODE ", 35).//the shape of the set point
Application. SetSystemVariable (" PDSIZE ", 2);//the size of the set point
int i=1;
//on the cell cycle, if is empty or the blank space the end loop
While (sheet) Cells [I + 1, 1]. The Value!=null & amp; & Sheet. The Cells [I + 1, 1]. The Value. The ToString (). The Trim ()!="")
{
DBPoint pt=new DBPoint ();//the location of the temporary variable is used to store some
DBText TXT=new DBText ();//temporary variables are used to store some name
//the name of the access point, located in the first column
TXT. TextString=sheet. Cells [r]. I + 1, I Value. The ToString ();
//access point x, y, z values at 2 and 4 column
Double x=the Convert. ToDouble (sheet) Cells [I + 1, 2]. The Value).
Double y=the Convert. ToDouble (sheet) Cells [I + 1, 3]. The Value).
Double z=the Convert. ToDouble (sheet) Cells [I + 1, 4] Value);
Pt. Position=new Point3d (x, y, z);//setting the location of the
//set the position of the text, located right on the point of the
TXT. Position=pt. Position. The Add (new Vector3d (3, 0, 0));
Points. Add (pt);//add some position to the corresponding list
TXTS. Add (TXT);//add some names to the corresponding list
i++;
}
Db. AddToModelSpace (TXTS. ToArray ());//add points to the name of the current model space
Db. AddToModelSpace (points. The ToArray ());//add points to the position of the current model space
Trans.Com MIT ();
}
Excelapp. Quit ();
Excelapp. The Dispose ();
}
}
}
Prompt error
Cad is a 2010 edition, net4.0 version
CodePudding user response:
Wrong,, 2012 version, cad, net4.0