.NET

Reply
Contributor
ugur35
Posts: 17
Registered: ‎04-20-2013
Message 1 of 12 (1,097 Views)

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

1097 Views, 11 Replies
05-18-2013 01:10 AM

this dwg file data reading with c#?

ADN Support Specialist
Balaji_Ram
Posts: 663
Registered: ‎03-21-2011
Message 2 of 12 (1,038 Views)

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

05-21-2013 06:18 AM in reply to: ugur35

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

Contributor
ugur35
Posts: 17
Registered: ‎04-20-2013
Message 3 of 12 (1,013 Views)

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

05-22-2013 12:26 AM in reply to: Balaji_Ram

Hello thank you,

but I could not

can you help me please

 

ADN Support Specialist
Balaji_Ram
Posts: 663
Registered: ‎03-21-2011
Message 4 of 12 (976 Views)

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

05-23-2013 04:29 AM in reply to: ugur35

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

Contributor
ugur35
Posts: 17
Registered: ‎04-20-2013
Message 5 of 12 (935 Views)

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

05-25-2013 12:18 AM in reply to: Balaji_Ram

thank you very much 

source code was useful :smileyhappy:

Contributor
ugur35
Posts: 17
Registered: ‎04-20-2013
Message 6 of 12 (707 Views)

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

07-03-2013 07:00 AM in reply to: ugur35

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.

Contributor
ugur35
Posts: 17
Registered: ‎04-20-2013
Message 7 of 12 (636 Views)

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

07-05-2013 05:25 AM in reply to: ugur35

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

ADN Support Specialist
Balaji_Ram
Posts: 663
Registered: ‎03-21-2011
Message 8 of 12 (631 Views)

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

07-05-2013 05:52 AM in reply to: ugur35

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

Contributor
ugur35
Posts: 17
Registered: ‎04-20-2013
Message 9 of 12 (625 Views)

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

07-05-2013 06:02 AM in reply to: Balaji_Ram

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.

 

*Expert Elite*
norman.yuan
Posts: 959
Registered: ‎04-27-2009
Message 10 of 12 (614 Views)

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

07-05-2013 06:34 AM in reply to: ugur35

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.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community