.NET

Reply
Valued Contributor
86 Posts
0 Kudos
Registered: ‎08-31-2011
Post 1 of 3

an interlocking movement with Excel

6907 Views, 2 Replies
02-19-2013 05:12 PM

Thank you.
I perform shifts from VBA to VB.NET API now.
Therefore our use OFFICE is still 2003,
but processes it by an interlocking movement with Excel by same commands in VBA. 
Is it possible to shift to VB.NET API in this?
If, for example, is version up or possibility of OFFICE
what is necessary if is impossible; introduction Same Sample cord Please.
Thanking you in advance.

Distinguished Mentor
582 Posts
91 Kudos
Registered: ‎04-11-2010
Post 2 of 3

Re: an interlocking movement with Excel

02-20-2013 08:00 AM in reply to: NKCAUTOCAD01

Hi,

 

Is this related to AutoCAD .NET development?, If so, what version of AutoCAD and Operating System are yo working on?

 

Also it's very, very dificult to understand your post, please try using Google Translate or other service to translate from your native language to English, or post in your native language and may be someone here can understand your problem.

 

Gaston Nunez

 

*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 3 of 3

Re: an interlocking movement with Excel

02-20-2013 08:22 AM in reply to: gasty1001

Just for example the code I've used to write some data

to Excel sheet, not sure about if it will be work in 2003 release,

anyway you can try itYou have to create form with button Button1 and listview - ListView1,

then complile and runcode  on your end

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
//using System.Data;
//using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using System.Globalization;
using System.Threading;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;

// Working example tested on Excel 2007, Acad 2009-2010
// References -> COM -> Microsoft.Excel XX.0 Object Library and Microsoft.Office XX.0 Object Library
// Create form and drop Button1 and ListView1 on this form
namespace ExcelForumCS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {

            // This lines are very important!
            CultureInfo oldcult = Thread.CurrentThread.CurrentCulture;
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-EN"); //<-- change culture on whatever you need
            
            object miss = System.Reflection.Missing.Value;
            int i = 0;
            int j = 0;

            // Open Excel
            Excel.Application m_objExcel = new Excel.ApplicationClass();
            m_objExcel.Visible = true;
            m_objExcel.UserControl = true;
            m_objExcel.DisplayAlerts = false;
            try
            {
            Excel.Workbooks m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
            
            m_objBooks.Open("C:\\Test\\Points.xls", miss, false);
                Excel.Workbook m_objBook = (Excel.Workbook)m_objBooks.get_Item(1);

                
                Excel.Sheets m_objSheets = (Excel.Sheets)m_objBook.Worksheets as Excel.Sheets;

                Excel.Worksheet m_objSheet = (Excel.Worksheet)m_objSheets.get_Item("Sheet1");
                //optional
                m_objSheet.Select(miss);
    
                Excel.Range m_objCells = null;
                m_objCells = (Excel.Range)m_objSheet.get_Range("A1", "A1");

                // write data starting from cell "A1"
                Range m_objRange = null;
                Random rand = new Random();
                for (i = 1; i <= 25; i++)// 25 - dummy number of rows
                {
                    for (j = 1; j <= 3; j++)//  3 - number of point coordinates
                    {
                        // get cell range by row and column
                         m_objRange =  m_objSheet.get_Range(m_objCells[i, j], m_objCells[i, j]);
                        // set value to a cell
                        m_objRange.set_Value(miss, (i*0.12345*rand.Next(1,10)).ToString());
                    }
                }

                // fill the listview with data
                ListView1.Clear();
                string[] columns = new string[] { "X coordinate", "Y coordinate", "Z coordinate" };

                foreach (string column in columns)
                {
                    ListView1.Columns.Add(column, 96, HorizontalAlignment.Left);
                }

                ListView1.View = View.Details;
                ListView1.GridLines = true;
                ListView1.FullRowSelect = true;

                for (int row = 1; row <= i - 1; row++)
                {

                    ListViewItem lvi = new ListViewItem(m_objSheet.get_Range(m_objCells[row, 1], m_objCells[row, 1]).Value2.ToString(), 0);
                    for (int col = 2; col <= j - 1; col++)
                    {

                        lvi.SubItems.Add(m_objSheet.get_Range(m_objCells[row, 1], m_objCells[row, 1]).Value2.ToString());
                    }
                    this.ListView1.Items.Add(lvi);
                }

                // Save the file in the typical workbook format (if newly created file)
                //m_objBook.SaveAs("C:\\Points.xls", Excel.XlFileFormat.xlWorkbookNormal, "", false, false, false, XlSaveAsAccessMode.xlShared, miss, miss, miss,
                //miss, miss);
               // save workbook
                m_objBook.Save();
                // close workbook and quit Excel
                if (m_objBook.Saved)
                {
                    m_objBook.Close();
                }
                else
                {
                    m_objBook.Close(true, miss, miss);
                }
                m_objExcel.Quit();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                // release process if it's still active
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(m_objExcel);
                Thread.CurrentThread.CurrentCulture = oldcult;
            }
        }
}

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!