Revit開發之匯出明細表到Excel
Revit API 裡目前好像還沒有明細表匯出Excel的API
所以匯出Excel的策略是,先讀取明細表,然後再
寫到Excel中,讀取可以用
ViewSchedule.GetCellText(SectionType sectionType, 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();
}
博主會經常更新一些技術文章,請大家多多關注,
原始碼下載請加qq群480950299