I want to read data from a DWG excel file using C#.

I want to read data from a DWG excel file using C#.

Anonymous
Not applicable
8,949 Views
12 Replies
Message 1 of 13

I want to read data from a DWG excel file using C#.

Anonymous
Not applicable

this dwg file data reading with c#?

0 Likes
8,950 Views
12 Replies
Replies (12)
Message 2 of 13

Balaji_Ram
Alumni
Alumni

Hello,

 

For an embedded Ole object as in your drawing, you can cast the OleObject as "Microsoft.Office.Interop.Excel.Workbook" and then you can use it appropriately using the Excel API to retrieve the data.

 

Here is a sample code snippet :

 

Ole2Frame oleFrame = tr.GetObject(id, OpenMode.ForRead) as Ole2Frame;

Microsoft.Office.Interop.Excel.Workbook wb = oleFrame.OleObject as Microsoft.Office.Interop.Excel.Workbook;

Microsoft.Office.Interop.Excel.Worksheet ws = wb.ActiveSheet;

Microsoft.Office.Interop.Excel.Range range = ws.UsedRange;

 

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 13

Anonymous
Not applicable

Hello thank you,

but I could not

can you help me please

 

0 Likes
Message 4 of 13

Balaji_Ram
Alumni
Alumni

Here is a sample code that displays all the values read from the embedded Oleobject.

I have attached a screenshot image to show the output.

 

[CommandMethod("Test")]
public static void TestMethod()
{
    Document doc = Application.DocumentManager.MdiActiveDocument;
    Editor ed = doc.Editor;
    Database db = doc.Database;

    using (Transaction Tx = db.TransactionManager.StartTransaction())
    {
        BlockTableRecord bt = Tx.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;

        foreach (ObjectId id in bt)
        {
            if (id.ObjectClass.Name != "AcDbOle2Frame")
                continue;

            Ole2Frame oleFrame = Tx.GetObject(id, OpenMode.ForRead) as Ole2Frame;

            if (! oleFrame.IsLinked)
            {
                Microsoft.Office.Interop.Excel.Workbook wb = oleFrame.OleObject as Microsoft.Office.Interop.Excel.Workbook;

                Microsoft.Office.Interop.Excel.Worksheet ws = wb.ActiveSheet;

                Microsoft.Office.Interop.Excel.Range range = ws.UsedRange;
                for (int row = 1; row <= 15; row++)
                {
                    for (int col = 1; col <= range.Columns.Count; col++)
                    {
                        ed.WriteMessage(String.Format("{0}{1}{2}-{3}", Environment.NewLine, row, col, Convert.ToString((range.Cells[row, col] as Microsoft.Office.Interop.Excel.Range).Value2)));
                    }
                }
            }
        }
    }
}

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 5 of 13

Anonymous
Not applicable

thank you very much 

source code was useful 🙂

0 Likes
Message 6 of 13

Anonymous
Not applicable

Thanks for the source code but the transaction did not complete.

I want to do the program .

I have attached a screenshot image to show the output.

 

thank you very much.

0 Likes
Message 7 of 13

Anonymous
Not applicable

hi Balaji Thank you for the source code, but unable to set winform can you help?

0 Likes
Message 8 of 13

Balaji_Ram
Alumni
Alumni

Hello,

 

Can you please explain what you mean by "unable to set winform" ?

 

You can try having a datagrid in a winform and then provide a data table as its source. The data read from the excel can be used to create a data table.

 

Sample code to for this can be found in MSDN help :

http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagrid.datasource.aspx

 

 

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 9 of 13

Anonymous
Not applicable

hello balaji

 

application on a Windows form application will be.(C# .NET 4.0)

DO YOU WANT TO PROGRAM

 

source code is being sent to you in advance .

 

[CommandMethod("Test")]

 

public static void TestMethod()

 

{

 

    Document doc = Application.DocumentManager.MdiActiveDocument;

 

    Editor ed = doc.Editor;

 

    Database db = doc.Database;

 

 

 

    using (Transaction Tx = db.TransactionManager.StartTransaction())

 

    {

 

        BlockTableRecord bt = Tx.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;

 

 

 

        foreach (ObjectId id in bt)

 

        {

 

            if (id.ObjectClass.Name != "AcDbOle2Frame")

 

                continue;

 

 

 

            Ole2Frame oleFrame = Tx.GetObject(id, OpenMode.ForRead) as Ole2Frame;

 

 

 

            if (! oleFrame.IsLinked)

 

            {

 

                Microsoft.Office.Interop.Excel.Workbook wb = oleFrame.OleObject as Microsoft.Office.Interop.Excel.Workbook;

 

 

 

                Microsoft.Office.Interop.Excel.Worksheet ws = wb.ActiveSheet;

 

 

 

                Microsoft.Office.Interop.Excel.Range range = ws.UsedRange;

 

                for (int row = 1; row <= 15; row++)

 

                {

 

                    for (int col = 1; col <= range.Columns.Count; col++)

 

                    {

 

                        ed.WriteMessage(String.Format("{0}{1}{2}-{3}", Environment.NewLine, row, col, Convert.ToString((range.Cells[row, col] as Microsoft.Office.Interop.Excel.Range).Value2)));

 

                    }

 

                }

 

            }

 

        }

 

    }

I can not run this code in windows form application.
I have attached a screenshot image to show the output.

 

0 Likes
Message 10 of 13

norman.yuan
Mentor
Mentor

Of course you CANNOT run the code in Win Form EXE app: the code uses AutoCAD managed (.NET) API, which CAN ONLY be used to run inside AutoCAD.

 

If you HAVE to do a stand-alone EXE app, then you have to use AutoCAD COM API to automate AutoCAD. Or, if you do not mind making things more complicated, you could expose the managed code as COM. But either way, AutoCAD must run.

 

Now, if AutoCAD must run, why do you need the trouble to have an external EXE app? You can just build the same Win form into AutoCAD with the very minor change to the code you showed here. Simply start AutoCAD, enter the command defined in the CommandMethod, off you go.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 11 of 13

Anonymous
Not applicable

I have to exe because

information that is contained in the excel file dwg

I need to MSSQL database record.

 

0 Likes
Message 12 of 13

norman.yuan
Mentor
Mentor

Well, I am not sure what do you mean by "excel file dwg", never heard of.

 

If you mean a *.dwg file with an Excel worksheet embedded (as your attached "drawing.jpg" shows), the Baliji's code shoud run INSIDE AutoCAD just fine, because it uses AutoCAD .NET API. If your goal is to read data in the worksheet embedded in AutoCAD drawing file, then that code shows how to do it and proves that you DO NOT have to do stand-alone exe app (but you do need Excel installed).

 

As for accessing data in SQL Server database, of course you can access in the same froom inside AutoCAD as you would do in EXE app. Again, it not limit you do use external app.

 

If you insist to use external app, then forget AutoCAD .NET API for now (unless you know it well and know how to expose it to COM), and look into AutoCAD COM API.

 

Without other compelling reasons, I'd use AutoCAD .NET API and go based in Balaji's code and run from inside AutoCAD.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 13 of 13

Anonymous
Not applicable

Check this...Excel Tutorial

 

Lee

0 Likes