How to Load family from excel spreadsheet

How to Load family from excel spreadsheet

Anonymous
Not applicable
914 Views
3 Replies
Message 1 of 4

How to Load family from excel spreadsheet

Anonymous
Not applicable

Hi

I just learned how to read a excel file and retrieve all data into a single string.

           string Data = "";
           using(ExcelPackage package = new ExcelPackage(new FileInfo(filename)))
           {
               ExcelWorksheet sheet = package.Workbook.Worksheets[1];
               for (int row = 3; row < 9999; row++)
               {
                   var thisValue = sheet.Cells[row, 1].Value;
                   if (thisValue == null || thisValue.ToString() == "")
                       break;
                   for (int col = 1; col < 3; col++)
                   {
                       thisValue = sheet.Cells[row, col].Value;
                       if (thisValue == null || thisValue.ToString() == "")
                           break;
 
                       Data += thisValue.ToString() + ",";
                   }
 
                   Data += Environment.NewLine;
               }
           }
           TaskDialog.Show("Excel Data&colon; ", Data);

readresuld.JPGEXCELLOAD.JPG

but How can I get a single string from each cell individuality by loop over each row?

for example I need string “Section” for the family symbol, and string “Type” for the category,

and use these 2 strings to load the family and its symbols. Like:

if (Type == "Beam")
{
FamilySymbol gotSymbol = null;
String fileName = "Families path for structural framing family";
String name = Section;
 using (Transaction t = new Transaction(doc, "Load"))
 {
 t.Start();
 Family family = null;
 if (doc.LoadFamilySymbol(fileName, name, out gotSymbol))
 TaskDialog.Show("Status", name + " is loaded");
 t.Commit();
 }
}
 

any suggestion or tips?

 

Thanks

 

0 Likes
915 Views
3 Replies
Replies (3)
Message 2 of 4

jeremytammik
Autodesk
Autodesk

Dear Sarkate,

 

From the way you phrase your question, I would suggest that you work through some basic .NET and C# tutorials before doing any more advanced work.

 

The question you ask is too basic and has nothing to do with the Revit API.

 

I'll take a stab at answering anyway:

 

The variable

 

thisValue

 

is assigned values from individual table cells, one at a time.

 

You could grab all the individual cell values from there.

 

Instead, the two lines

 

Data += thisValue.ToString() + ",";

 

and

 

Data += Environment.NewLine;

 

concatenate all these values into one single long string.

 

As said, if you do not see this for yourself, I would strongly advise you to proceed no futher with this project but play around with some basic .NET, C# and string manipulation tutorials first.

 

I hope this helps.

 

Best regards,

 

Jeremy

 

 



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

0 Likes
Message 3 of 4

Anonymous
Not applicable

Hi,Jeremy

Thanks for your reply and advice. 

I've actually solved this problem by reading some C# thread over last weekend, It is much easier than i thought, this is the working code:

                for (int row = 1; row < 9999; row++)
                {
                    var SectionVar = sheet.Cells[row, 1].Value;
                    var EltypeVar = sheet.Cells[row, 2].Value;
                    if (SectionVar == null || SectionVar.ToString() == "")
                        break;

 

Thanks

0 Likes
Message 4 of 4

jeremytammik
Autodesk
Autodesk

Well done!

 

Thank you for letting us know.

 

Cheers,

 

Jeremy



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

0 Likes