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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值