填充 <string> 列表或数组以将元素名称导出到 excel
Posted
技术标签:
【中文标题】填充 <string> 列表或数组以将元素名称导出到 excel【英文标题】:Filling <string> list or array to export element names to excel 【发布时间】:2021-11-30 19:40:26 【问题描述】:目标是用元素名称填充 excel 单元格。使用 EPPlus。
_elementsDB 仅提供“Autodesk.Revit.DB.Wall”。
int col = 1;
for ( int row=2; row < _elementsDB.Count; row++ )
ws.Cells[row, col].Value = _elementsDB;
绑定填充数组或列表。没有任何效果。
FilteredElementCollector collector = new FilteredElementCollector(doc);
IList<Element> _elementsDB = collector.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements();
List<Element> _elementsDB = collector.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements();
//First Option
string[] _elementsNameArr = new string[] ;
foreach (Element e in _elementsDB)
_elementsNameArr = e.Name;
//Second Option
List <string> _elementsNameList = new List<string>;
foreach (Element e in _elementsDB)
_elementsNameList = e.Name;
还尝试创建一个排序列表,也没有工作。显示一个 excxeption System.Argument.Exception "A record with such key already exists"。
SortedList<string, Element> _elementNameSorted = new SortedList<string, Element>();
foreach (Element e in _elementsDB)
_elementNameSorted.Add(e.Name,e);
【问题讨论】:
【参考方案1】:当你使用.ToElements()
方法时,它返回一个IList<Element>
,你可以稍后转换它,你不能直接将它的结果分配给List<Element>
,你必须使用LINQ来转换,使用.ToElements().ToList()
如果您还没有,请务必将using System.Linq;
添加到您的代码顶部。
反正这里不用转成List<Element>
,试试下面的代码:
FilteredElementCollector collector = new FilteredElementCollector(doc);
IList<Element> _elementsDB = collector.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements();
//List<Element> _elementsDB = collector.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType().ToElements(); // <---- IF YOU ALREADY DECLARED _elementsDB BEFORE, YOU CAN'T HAVE THIS HERE TOGETHER
List <string> _elementsNameList = new List<string>(); // <-- YOU'VE MISSED () HERE
foreach (Element e in _elementsDB)
_elementsNameList.Add(e.Name); // <-- HERE YOU HAVE TO ADD IT TO THE LIST, NOT ASSIGN TO THE LIST, YOU CANNOT ASSIGN A string TO A List<string>, YOU HAVE TO ADD
//Sorting your list would be
_elementsNameList = _elementsNameList.OrderBy(q => q).ToList();
//...... Write here if you have any code before writing the Excel
try
WriteXLS("YOU EXCEL FILE PATH HERE", "YOUR WORKSEET NAME HERE", _elementsNameList, 2); // <--- METHOD SOWN IN THE CODE BELOW
catch(Exception e)
TaskDialog.Show("Error", e.Message);
要写入现有的 Excel 文件,您可以使用以下方法:
private void WriteXLS(string filePath, string workSheetName, List<string> elementsNamesList, int startRow = 1, int col = 1)
FileInfo existingFile = new FileInfo(filePath);
using (ExcelPackage package = new ExcelPackage(existingFile))
ExcelWorksheet ws = GetWorkSheet(package, workSheetName);
int maxRows = elementsNamesList.Count;
for (int row = startRow; row <= maxRows; row++)
ws.Cells[row, col].Value = elementsNamesList[row];
并且,在运行它之前,一定要关闭你的Excel文件,如果文件打开它就不起作用。
【讨论】:
以上是关于填充 <string> 列表或数组以将元素名称导出到 excel的主要内容,如果未能解决你的问题,请参考以下文章