Importing an excel sheet using c#

Importing an excel sheet using c#

Anonymous
Not applicable
4,949 Views
6 Replies
Message 1 of 7

Importing an excel sheet using c#

Anonymous
Not applicable

How can one import an excel file to revit and create a schedule from it. Please include source code.

0 Likes
4,950 Views
6 Replies
Replies (6)
Message 2 of 7

jeremytammik
Autodesk
Autodesk

Look at the Revit SDK samples and search globally for 'excel'. Some of them import data from excel spreadsheets. I don't know whether you can create a schedule that way, though. Can you do that manually through the user interface at all?

 

Cheers,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 3 of 7

Anonymous
Not applicable
So as you have suggested, I tried one of the samples in SDK, "PointsFromExcel - create reference points based on XYZ data in an Excel file". to see how does it work.
I attached the screenshots of what I got in VS and Revit, after trying to implement the code. My question is when I was showing and reading an excel file in C# earlier, other than my main Cs file, I had to create a Form File ( graphics ) and in that file, I entered the path of my excel file. But for Revit, I don't know where is the exact location that I can enter the path of my excel file location. Could you please look at the attached pictures.
0 Likes
Message 4 of 7

Anonymous
Not applicable

Another screenshot

0 Likes
Message 5 of 7

aignatovich
Advisor
Advisor

Hi!

 

I didn't find PointsFromExcel in samples folder in Revit SDK

 

However, exception message it is clear: all classes, that implement IExternalCommand interface must have Transaction attribute, for example, I opened the first sdk sample:

 

Revit SDK 2017\Software Development Kit\Samples\AddSpaceAndZone\CS\Command.cs

 

The firs attribute of Command class is:

 

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]

two others (Regeneration and Journaling) are optional

 

Sincerely, Alexander

0 Likes
Message 6 of 7

Anonymous
Not applicable


for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
    for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
    {
        data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
        xlWorkSheet.Cells[i + 1, j + 1] = data;
    }
}

Full source...import to excel file

0 Likes
Message 7 of 7

Anonymous
Not applicable

Use this code 

using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var dt = new DataTable();

            var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "File1.xlsx");
            var query = "SELECT F1 As A, F2 As B, F3 As C FROM [Sheet1$]";
            using (OleDbConnection cn = new OleDbConnection { ConnectionString = ConnectionString(fileName, "No") })
            {
                using (OleDbCommand cmd = new OleDbCommand { CommandText = query, Connection = cn })
                {
                    cn.Open();

                    OleDbDataReader dr = cmd.ExecuteReader();
                    dt.Load(dr);
                }

            }
            if (dt.Rows.Count >1)
            {
                // remove header
                dt.Rows[0].Delete();
            }
            dt.AcceptChanges();
            Console.WriteLine();
        }
        public string ConnectionString(string FileName, string Header)
        {
            OleDbConnectionStringBuilder Builder = new OleDbConnectionStringBuilder();
            if (Path.GetExtension(FileName).ToUpper() == ".XLS")
            {
                Builder.Provider = "Microsoft.Jet.OLEDB.4.0";
                Builder.Add("Extended Properties", string.Format("Excel 8.0;IMEX=1;HDR={0};", Header));
            }
            else
            {
                Builder.Provider = "Microsoft.ACE.OLEDB.12.0";
                Builder.Add("Extended Properties", string.Format("Excel 12.0;IMEX=1;HDR={0};", Header));
            }

            Builder.DataSource = FileName;

            return Builder.ConnectionString;
        }
    }
}


or try to ZetExcel.com this would help you.
Thanks

 

0 Likes