3
收藏0
分享

如果是CAD自身的Table对象,提取数据还是比较简单的,如果是伪表,则比较困难。标准表格的提取示例代码如下: #reg…

如果是CAD自身的Table对象,提取数据还是比较简单的,如果是伪表,则比较困难。标准表格的提取示例代码如下:

#region 导出标准表格
        internal void ExportStandardTable(mainForm mainForm)
        {
            mainForm.Hide();            
            //选择多个表格对象
            PromptSelectionOptions pso=new PromptSelectionOptions();
            pso.MessageForAdding = "请选择需要导出的表格对象";
            TypedValue[] acTypValAr = new TypedValue[1];
            acTypValAr[0] = new TypedValue((int)DxfCode.Start, "ACAD_Table");

            SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);
            PromptSelectionResult psr = ed.GetSelection(pso, acSelFtr);
            if (psr.Status==PromptStatus.OK)
            {
                List<Entity> ssEntList = psr.GetMulitEntityBySelect();
                List<List<List<string>>> tableList = new List<List<List<string>>>();
                //遍历每个表格对象
                for (int i = 0; i < ssEntList.Count; i++)
                {
                    Table table = ssEntList[i] as Table;
                    List<List<string>> rowList = new List<List<string>>();
                    //遍历每一行
                    for (int j = 0; j < table.Rows.Count; j++)
                    {
                        List<string> cellList = new List<string>();
                        //遍历每一列
                        for (int k = 0; k < table.Columns.Count; k++)
                        {
                            cellList.Add(table.Cells[j, k].TextString);
                        }
                        rowList.Add(cellList);
                    }
                    tableList.Add(rowList);
                }

                
                //弹窗选择Excel保存位置
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Excel文件|*.xlsx";
                sfd.Title = "请选择保存位置";
                sfd.ShowDialog();
                string path = sfd.FileName;
                if (path == "")
                {
                    mainForm.Show();
                    return;
                }
                //结果写入Excel文件中,用NPOI
                IWorkbook workbook = new XSSFWorkbook();
                for (int i = 0; i < tableList.Count; i++)
                {
                    ISheet sheet = workbook.CreateSheet($"表格{i}");
                    for (int j = 0; j < tableList[i].Count; j++)
                    {
                        IRow row = sheet.CreateRow(j);
                        for (int k = 0; k < tableList[i][j].Count; k++)
                        {
                            ICell cell = row.CreateCell(k);
                            cell.SetCellValue(tableList[i][j][k]);
                        }
                    }
                    //数据区添加边框线
                    ICellStyle cellStyle = workbook.CreateCellStyle();
                    cellStyle.BorderBottom = BorderStyle.Thin;
                    cellStyle.BorderLeft = BorderStyle.Thin;
                    cellStyle.BorderRight = BorderStyle.Thin;
                    cellStyle.BorderTop = BorderStyle.Thin;
                    for (int j = 0; j < tableList[i].Count; j++)
                    {
                        for (int k = 0; k < tableList[i][j].Count; k++)
                        {
                            sheet.GetRow(j).GetCell(k).CellStyle = cellStyle;
                        }
                    }
                }
                FileStream fs = new FileStream(path, FileMode.Create);
                workbook.Write(fs);
                fs.Close();
                workbook.Close();
                ed.WriteMessage($"\n已将{tableList.Count}个表格导出到Excel文件中!");
            }
            mainForm.Show();
        }
        #endregion
3 条评论 A文章作者 M管理员
  1. Alex

    是的,现在我那些DWG文档中的是OLE嵌入式Excel表格,比较麻烦。

    • 飞扬

      OLE表格不麻烦,甚至更简单,晚点我把代码贴出来

    • Alex

      顶礼膜拜大佬!!😲

购物车
优惠劵
今日签到
有新私信 私信列表
搜索
复制链接
微信扫码
已复制到剪贴板