Как создать Таблицу и заполнить её ячейки средствами .NET

Автор Тема: Как создать Таблицу и заполнить её ячейки средствами .NET  (Прочитано 5370 раз)

0 Пользователей и 1 Гость просматривают эту тему.

Онлайн Александр РивилисАвтор темы

  • Administrator
  • *****
  • Сообщений: 13829
  • Карма: 1784
  • Рыцарь ObjectARX
  • Skype: rivilis
Не забывайте про правильное Форматирование кода на форуме
Создание и добавление Autodesk Screencast видео в сообщение на форуме
Если Вы задали вопрос и на форуме появился правильный ответ, то не забудьте про кнопку Решение

Оффлайн fattyhallex

  • ADN Club
  • Сообщений: 16
  • Карма: 0
В добавление привожу отредактированный пример,
возможно кому-нибудь будет полезен,
код тестирован начиная с 2010 версии, в 2009-м
будет другой синтаксис.
Код - C# [Выбрать]
  1. #region "System References"
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Reflection;
  7. #endregion
  8.  
  9. #region "Acad References"
  10. using Autodesk.AutoCAD.ApplicationServices;
  11. using Autodesk.AutoCAD.DatabaseServices;
  12. using Autodesk.AutoCAD.Geometry;
  13. using Autodesk.AutoCAD.EditorInput;
  14. using Autodesk.AutoCAD.Runtime;
  15. using Autodesk.AutoCAD.Colors;
  16. #endregion
  17. //_____________________________//
  18.  
  19.    [CommandMethod("tabc")]
  20.  
  21.         public void testAddtableBC()
  22.         {
  23.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  24.  
  25.             Database db = doc.Database;
  26.  
  27.             Editor ed = doc.Editor;
  28.  
  29.             using (Transaction tr = db.TransactionManager.StartTransaction())
  30.             {
  31.  
  32.                 BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  33.  
  34.                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  35.  
  36.                 // создаем таблицу
  37.  
  38.                 Table tb = new Table();
  39.  
  40.                 tb.TableStyle = db.Tablestyle;
  41.  
  42.                 tb.SuppressRegenerateTable(true);
  43.  
  44.                 // число строк
  45.  
  46.                 int RowsNum = 10;// только количество строк для данных
  47.  
  48.                 // число столбцов
  49.  
  50.                 int ColumnsNum = 5;
  51.  
  52.                 // высота строки
  53.  
  54.                 double rowheight = 3;
  55.  
  56.                 // ширина столбца
  57.  
  58.                 double columnwidth = 30;
  59.  
  60.                 // вставка строк и столбцов
  61.  
  62.                 tb.InsertRows(0, rowheight, RowsNum + 1);// first one is already created
  63.  
  64.                 tb.Cells[0, 0].TextString = "Заголовок";
  65.  
  66.                 tb.Rows[0].IsMergeAllEnabled = true;
  67.  
  68.                 tb.InsertColumns(0, columnwidth, ColumnsNum);
  69.  
  70.                 tb.SetRowHeight(rowheight);
  71.  
  72.                 tb.SetColumnWidth(columnwidth);
  73.  
  74.                 tb.Position = ed.GetPoint("\nТочка вставки таблицы: ").Value;
  75.  
  76.                 // заполнение ячеек по-одной
  77.  
  78.                 for (int i = 1; i < RowsNum + 2; i++)
  79.                 {
  80.  
  81.                     for (int j = 0; j <= ColumnsNum; j++)
  82.                     {
  83.  
  84.                         tb.Cells[i, j].TextHeight = 1;
  85.  
  86.                         if (i == 1)
  87.  
  88.                             tb.Cells[i, j].TextString = "Столбец " + (j + 1).ToString();
  89.  
  90.                         else
  91.  
  92.                             tb.Cells[i, j].TextString = (i - 1).ToString() + "." + j.ToString();
  93.  
  94.                         tb.Cells[i, j].Alignment = CellAlignment.MiddleCenter;
  95.  
  96.                         tb.Cells[i, j].DataFormat = "Whole Number";// целочисленный, не знаю русского аналога
  97.  
  98.                     }
  99.  
  100.                 }
  101.                 int a = 0;
  102.  
  103.                 tb.Rows[a].Style = "Title";
  104.  
  105.                 tb.Rows[a + 1].Style = "Header";
  106.  
  107.                 for (a = 2; a < RowsNum + 2; a++)
  108.                 {
  109.                     tb.Rows[a].Style = "Data";
  110.                 }
  111.                 // добавление в отображение таблицы
  112.                 // различный цвет для строк
  113.                 for (a = 2; a < RowsNum + 2; a++)
  114.                 {
  115.                     tb.Rows[a].ContentColor = Color.FromColorIndex(ColorMethod.ByAci, 16);
  116.                     if (a % 2 == 0) tb.Rows[a].BackgroundColor = Color.FromRgb(245, 238, 224);
  117.  
  118.                     else tb.Rows[a].BackgroundColor = Color.FromRgb(227, 236, 243);
  119.                 }
  120.                 // титульная строка
  121.                 tb.Rows[0].BackgroundColor = Color.FromRgb(150, 121, 126);
  122.                 tb.Rows[0].ContentColor = Color.FromRgb(235, 176, 0);
  123.                 // строка заголовков
  124.                 tb.Rows[1].BackgroundColor = Color.FromRgb(169, 172, 193);
  125.                 tb.Rows[1].ContentColor = Color.FromRgb(102, 26, 0);
  126.  
  127.                 // добавление широкой рамки, значение по усмотрению
  128.                 tb.Columns[0].Borders.Left.LineWeight = LineWeight.LineWeight050;
  129.                 tb.Columns[tb.Columns.Count - 1].Borders.Right.LineWeight = LineWeight.LineWeight050;
  130.                 // получаем титульную строку
  131.                 Row rw = tb.Rows[0];
  132.  
  133.                 rw.Borders.Top.LineWeight = LineWeight.LineWeight050;
  134.                 // получаем строку заголовков
  135.                 rw = tb.Rows[1];
  136.  
  137.                 rw.Borders.Top.LineWeight = LineWeight.LineWeight050;
  138.                 rw.Borders.Bottom.LineWeight = LineWeight.LineWeight050;
  139.                 // делаем широкие линии в вертикалях ячеек заголовочной строки
  140.                 for (a = 1; a < ColumnsNum; a++)
  141.                 {
  142.                     tb.Cells[1, a].Borders.Left.LineWeight = LineWeight.LineWeight050;
  143.                     tb.Cells[1, a].Borders.Right.LineWeight = LineWeight.LineWeight050;
  144.                 }
  145.                 // получаем последнюю строку
  146.                 rw = tb.Rows[tb.Rows.Count - 1];
  147.  
  148.                 rw.Borders.Bottom.LineWeight = LineWeight.LineWeight050;
  149.  
  150.                 tb.SuppressRegenerateTable(false);
  151.  
  152.                 tb.GenerateLayout();
  153.  
  154.                 btr.AppendEntity(tb);
  155.  
  156.                 tr.AddNewlyCreatedDBObject(tb, true);
  157.  
  158.                 tr.Commit();
  159.  
  160.                 Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("lwdisplay", 1);
  161.  
  162.             }
  163.  
  164.         }
« Последнее редактирование: 29-07-2013, 00:10:43 от fattyhallex »