/// <summary> /// 获得模板 /// </summary> /// <param name="item"></param> private void InitTemplate(JsonObject item) { //取出表头行定义 JsonArray rows = item["rows"] as JsonArray; foreach (JsonObject obj in rows) { int height = obj["height"]; string type = obj["type"]; if (type == "head") { Row row = new Row() { Height = height }; this.headRowTemplate.Add(row); } if (type == "bottom") { Row row = new Row() { Height = height }; this.bottomRowTemplate.Add(row); } } //获得模板 JsonArray cells = item["cells"] as JsonArray; foreach (JsonObject obj in cells) { int row = obj["row"]; int column = obj["column"]; string content = obj["content"]; int rowspan = obj["rowspan"]; int columnspan = obj["columnspan"]; string location = obj["location"]; int height = obj["height"]; Cell cell = new Cell() { Row = row, Column = column, Content = content, RowSpan = rowspan, ColumnSpan = columnspan, Location = location, Height = height }; string type = obj["type"]; //表头 if (type == "head" || type == "headchange") { this.headCellTemplate.Add(cell); } //表头变化部分 if (type == "headchange") { this.headChangeCellTemplate.Add(cell); } //左侧 if (type == "left") { Program prog = new Program("data."+cell.Content, this, false); Delegate d = prog.Parse(prog.Exp); cell.Delegate = d; this.leftCellTemplate.Add(cell); } //主体 if (type == "main") { this.bodyCellTemplate.Add(cell); } //表底 if (type == "bottom") { this.bottomCellTemplate.Add(cell); } } //如果表头有变化部分,表头模板根据列号排序 if (HasHead) { List<Cell> headCellTemplate_temp = new List<Cell>(); //表头模板根据列号排序 for (int i = 0; i < this.headCellTemplate.Count; i++) { foreach (Cell cell in this.headCellTemplate) { if (cell.Column == i) { headCellTemplate_temp.Add(cell); break; } } } this.headCellTemplate.Clear(); this.headCellTemplate.AddRange(headCellTemplate_temp); } }
/// <summary> /// 复制单元格 /// </summary> /// <returns></returns> public Cell copyCell() { Cell cell = new Cell() { Row = Row, Column = Column, Content = Content, RowSpan = RowSpan, ColumnSpan = ColumnSpan,Location=Location, Width = Width, Height = Height, Delegate = Delegate }; return cell; }
//计算单元格高度 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; }
/// <summary> /// 把json转成报表对象 /// </summary> /// <param name="json"></param> private void jsonToTable(string json) { this.cells.Clear(); this.columns.Clear(); this.rows.Clear(); JsonObject item = JsonValue.Parse(json) as JsonObject; GeneralObject go = new GeneralObject(); go.FromJson(item); //列 ObjectList ol = go.GetPropertyValue("Column") as ObjectList; foreach (GeneralObject one in ol) { Column c = new Column(); c.Width = Int32.Parse(one.GetPropertyValue("Width") + ""); this.columns.Add(c); } //行 ol = go.GetPropertyValue("Row") as ObjectList; foreach (GeneralObject one in ol) { Row r = new Row(); r.Height = Int32.Parse(one.GetPropertyValue("Height")+""); this.rows.Add(r); } //单元格 ol = go.GetPropertyValue("Cell") as ObjectList; foreach (GeneralObject one in ol) { Cell c = new Cell(); c.Content = one.GetPropertyValue("Content") + ""; c.Column = Int32.Parse(one.GetPropertyValue("Column") + ""); c.ColumnSpan = Int32.Parse(one.GetPropertyValue("ColumnSpan") + ""); c.Row = Int32.Parse(one.GetPropertyValue("Row") + ""); c.RowSpan = Int32.Parse(one.GetPropertyValue("RowSpan") + ""); c.Location = one.GetPropertyValue("Location") + ""; this.cells.Add(c); } //绘制报表 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; }