Author Topic: Can a table be exported to Excel?  (Read 2050 times)

0 Members and 1 Guest are viewing this topic.

waterharbin

  • Guest
Can a table be exported to Excel?
« on: February 08, 2012, 08:55:15 AM »
Hello,everyone. Can a table be exported to Excel?
we define a table in AutoCAD :
Code: [Select]
Dim TableEnt As New Autodesk.AutoCAD.DatabaseServices.Table()
Then, we can launch Excel :
Code: [Select]

Imports Microsoft.Office.Interop

Public Sub exportToExcel()
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        Try
            xlApp = New Excel.ApplicationClass
            xlWorkBook = xlApp.Workbooks.Add(misValue)
            'Fixed:(Microsoft.Office.Interop.Excel.Worksheet)
            xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), Excel.Worksheet)
            xlWorkSheet = DirectCast(xlWorkBook.ActiveSheet, Excel.Worksheet)
            'changing the name of active sheet
            xlWorkSheet.Name = "My Exported DataSet"

            xlApp.Visible = True
         
        Catch ex As Exception
            MessageBox.Show(ex.StackTrace() + ex.Message())           
        End Try

    End Sub
But how to select a table in AutoCAD, and then expoted the texts to Excel.
 Is this possible? I have no idea of this. Is there anyone who has done this before?

By the way, To lanuch Excel, we will need to  add a reference to the Microsoft Excel object library.Do it liks this:
Right click on your project and select Add Reference menu. After that go to COM tab and select and add Microsoft Excel 12.0 object library
« Last Edit: February 08, 2012, 09:06:16 AM by waterharbin »

kaefer

  • Guest
Re: Can a table be exported to Excel?
« Reply #1 on: February 08, 2012, 09:42:48 AM »
Is this possible? I have no idea of this. Is there anyone who has done this before?

Who haven't done this before? See e.g. this message by gile for a nice example, look for WriteXls().

Quote
After that go to COM tab and select and add Microsoft Excel 12.0 object library

You do not want to go down that route. To be independent of specific installed versions (here 12.0), use late binding instead.

waterharbin

  • Guest
Re: Can a table be exported to Excel?
« Reply #2 on: February 10, 2012, 02:22:14 AM »
Hello,Kaefer.I have heard late binding before.But I don't known how to do it. Can you teach me? Thanks.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Can a table be exported to Excel?
« Reply #3 on: February 10, 2012, 04:13:30 AM »
Hi,

You can see here an example of late binding using to read or write Excel files.
Speaking English as a French Frog