//计算单元格高度 private int GetHeight(Cell cell) { int result = 0; for (int i = 0; i < cell.RowSpan; i++) { result += this.rows[i + cell.Row].Height; } return result; }
//从文件中加载数据,文件格式与Save函数相同 public void Load(string fileName) { //清除原来的数据 this.cells.Clear(); this.columns.Clear(); this.rows.Clear(); this.selectedCells.Clear(); this.sqls.Clear(); if (fileName == "" || fileName == null) return; StreamReader sw = new StreamReader(fileName, Encoding.Default, false); string jsonText = sw.ReadToEnd(); // JsonObject json; var json = (JObject)JsonConvert.DeserializeObject(jsonText); //读出所有sql语句 JArray array = (JArray)json["sqls"]; foreach (JObject obj in array) { Sql sql = new Sql() { Name = (string)obj["name"], Content = (string)obj["sql"] }; this.sqls.Add(sql); } //左表头内容表达式 LeftValue = (string)json["leftsql"]; //左表头内容表达式 HeadValue = (string)json["headsql"]; //读出所有列 int i = 0; array = (JArray)json["columns"]; foreach (JObject obj in array) { Column column = new Column() { Width = (int)obj["width"], Number = i++ }; this.columns.Add(column); } //读出所有行 i = 0; array = (JArray)json["rows"]; foreach (JObject obj in array) { Row row = new Row() { Height = (int)obj["height"], Type = (string)obj["type"], Number = i++ }; this.rows.Add(row); } this.headCells.Clear(); this.leftCells.Clear(); this.mainCells.Clear(); //读出所有单元格 array = (JArray)json["cells"]; foreach (JObject obj in array) { Cell cell = new Cell() { Row = (int)obj["row"], RowSpan = (int)obj["rowspan"], ColumnSpan = (int)obj["columnspan"], Column = (int)obj["column"], Content = (string)obj["content"], location = (string)obj["location"], Height = (int)obj["height"], }; this.cells.Add(cell); //根据单元格类型,把单元格添加到对应队列中 string type = (string)obj["type"]; if (type == "head") { headCells.Add(cell); } else if (type == "left") { leftCells.Add(cell); } else if (type == "main") { mainCells.Add(cell); } else if (type == "bottom") { bottomCells.Add(cell); } if (type == "headchange") { headchangeCells.Add(cell); } } this.Layout(); }
//计算单元格宽带 private int GetWidth(Cell cell) { int result = 0; for (int i = 0; i < cell.ColumnSpan; i++) { result += this.columns[i + cell.Column].Width; } return result; }
//前插一列 public void InsertColumn() { //当前列之后所有列,列号+1 for (int i = this.editCell.Column; i < this.columns.Count; i++) { this.columns[i].Number++; } //所有当前列之后的单元格,column+1 foreach (Cell cell in this.cells) { if (cell.Column >= this.editCell.Column && cell != this.editCell) { cell.Column++; } } //生成新单元格 for (int i = 0; i < this.rows.Count; i++) { Cell cell = new Cell() { Row = i, Column = this.editCell.Column }; this.cells.Add(cell); } //插入新列 Column column = new Column() { Number = this.editCell.Column, Width = 100 }; this.columns.Add(column); this.editCell.Column++; this.columns.Sort(); Layout(); }
//前插一行 public void InsertRow() { //当前行之后所有行,行号+1 for (int i = this.editCell.Row; i < this.rows.Count; i++) { this.rows[i].Number++; } //所有当前行之后的单元格,row+1 foreach (Cell cell in this.cells) { if (cell.Row >= this.editCell.Row && cell != this.editCell) { cell.Row++; } } //生成新单元格 for (int i = 0; i < this.columns.Count; i++) { Cell cell = new Cell() { Row = this.editCell.Row, Column = i }; this.cells.Add(cell); } //插入新行 Row row = new Row() { Number = this.editCell.Row, Height = 100 }; rows.Add(row); this.editCell.Row++; rows.Sort(); Layout(); }
//取消合并 public void DecomposeCell() { //针对当前单元格,按行列重新产生单元格 for (int i = 0; i < this.editCell.RowSpan; i++) { for (int j = 0; j < this.editCell.ColumnSpan; j++) { //不是原来cell if(!(i == 0 && j == 0)) { Cell cell = new Cell() { Row = this.editCell.Row + i, Column = this.editCell.Column + j }; this.cells.Add(cell); } } } this.editCell.ColumnSpan = 1; this.editCell.RowSpan = 1; this.Layout(); }
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) { base.OnMouseLeftButtonDown(e); //获得选中的对象 Point point = e.GetPosition(this); this.selected = this.GetPosition(point) as IMove; if (this.selected is Cell) { //把输入内容放到原单元格中 this.editCell.Content = this.editTextBox.Text; //选中新单元格 Cell cell = this.selected as Cell; this.editCell = cell; //清空所有选中单元格 this.selectedCells.Clear(); this.Layout(); } }
public Table() { //默认5行5列 for (int i = 0; i < 5; i++) { Row row = new Row() { Number = i, Height = 100 }; rows.Add(row); } for (int i = 0; i < 5; i++) { Column column = new Column() { Number = i, Width = 100 }; columns.Add(column); } for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { Cell cell = new Cell() { Row = i, Column = j }; cells.Add(cell); } } //选中第一个单元格进行编辑 this.editCell = this.cells[0]; }