Revit API 里目前好像还没有明细表导出Excel的API
所以导出Excel的策略是,先读取明细表,然后再
写到Excel中,读取可以用
ViewSchedule.GetCellText(SectionType pType, int row, int column);
写入Excel方法就很多了,读者可以随意选
下面是一个简单的例子,写入Excel用的是NPOI
关键代码如下:
Document doc = commandData.Application.ActiveUIDocument.Document;
ViewSchedule v = doc.ActiveView as ViewSchedule;
TableData td = v.GetTableData();
TableSectionData tdb = td.GetSectionData(SectionType.Header);
string head = v.GetCellText(SectionType.Header, 0, 0);
TableSectionData tdd = td.GetSectionData(SectionType.Body);
int c = tdd.NumberOfColumns;
int r = tdd.NumberOfRows;
HSSFWorkbook work = new HSSFWorkbook();
ISheet sheet = work.CreateSheet("mysheet");
for (int i = 0; i < r; i++)
{
IRow row = sheet.CreateRow(i);
for (int j = 0; j < c; j++)
{
Autodesk.Revit.DB.CellType ctype = tdd.GetCellType(i, j);
ICell cell = row.CreateCell(j);
string str = v.GetCellText(SectionType.Body, i, j);
cell.SetCellValue(str);
}
}
using (FileStream fs = File.Create("d:\\excel.xls"))
{
work.Write(fs);
fs.Close();
}
转载请注明来源本文地址:https://www.tuituisoft/blog/3488.html