こんにちは。
下記の変数名Objの「★固定の配列」に、ループ処理にて「★動的に追加したい値」の内容に
動的Aと動的Bの箇所を動的に変化させた文字列をセットし
変数名Objの「★固定の配列」に追加していきたい。
結果として「★結果」のようにしたいのですが
どのようにすれば良いでしょうか?
ご教授宜しくお願い致します。
C#
1★固定の配列 2 Object[,] Obj = { 3 { "ID", "UniqId", 70, false, VerticalAlign.Middle, HorizontalAlign.Center, "LABEL_CHAR" }, 4 { "職員名", "ShokuinName", 320, true, VerticalAlign.Middle, HorizontalAlign.Left, "LABEL_CHAR" }, 5 }; 6 7**************************************************************************************************************** 8★動的に追加したい値 9 ※動的Aと動的Bはループ処理により動的に命名する 10{ 動的A, 動的B, 80, true, VerticalAlign.Middle, HorizontalAlign.Center, "MONEY" }, 11**************************************************************************************************************** 12 13★結果 14 15 Object[,] Obj = { 16 { "ID", "UniqId", 70, false, VerticalAlign.Middle, HorizontalAlign.Center, "LABEL_CHAR" }, 17 { "職員名", "ShokuinName", 320, true, VerticalAlign.Middle, HorizontalAlign.Left, "LABEL_CHAR" }, 18 { "ほげほげ1", "Hogehoge1", 80, true, VerticalAlign.Middle, HorizontalAlign.Center, "MONEY" }, 19 { "ほげほげ2", "Hogehoge2", 80, true, VerticalAlign.Middle, HorizontalAlign.Center, "MONEY" }, 20 { "ほげほげ3", "Hogehoge3", 80, true, VerticalAlign.Middle, HorizontalAlign.Center, "MONEY" }, 21 { "ほげほげ4", "Hogehoge4", 80, true, VerticalAlign.Middle, HorizontalAlign.Center, "MONEY" }, 22 23
List形式からObject[,]への変換までを書いてみました。
なんとかできました。ありがとうございました。
C#
1 var objList = new List<List<object>>(); 2 objList.Add(new List<object>() { "UniqId", "UniqId", 70, false, 11111, 22222, "LABEL_CHAR" }); 3 objList.Add(new List<object>() { "職員名", "ShokuinName", 320, true, 11111, 22222, "LABEL_CHAR" }); 4 objList.Add(new List<object>() { "科目コード", "AAA", 80, true, 11111, 22222, "MONEY" }); 5 6 for (var i = 0; i < 10; i++) 7 { 8 9 objList.Add(new List<object>() { "ほげほげ" + i, "Hogehoge" + i, 80, true, 11111, 22222, "MONEY" }); 10 11 } 12 13 //これだとObject[][]になってしまう 14 //var parsedList = objList 15 // .Select(val => val 16 // .Select(val2 => val2).ToArray() 17 // ).ToArray(); 18 19 var rowCount = objList.Count; 20 var colCount = 7; 21 22 var r = new Object[objList.Count, 7]; 23 24 for (int y = 0; y < rowCount; y++) 25 { 26 var src = objList[y]; 27 for (int x = 0; x < colCount; x++) 28 { 29 r[y, x] = objList[x]; 30 } 31 }
実装してみました。
うまくいきました。
C#
1 2using System; 3using System.Collections.Generic; 4using System.Linq; 5using System.Text; 6using System.Threading.Tasks; 7 8namespace WindowsFormsApplication1 9{ 10 public class Poco 11 { 12 public string Name; 13 public string Id; 14 public int Width; 15 public bool IsValid; 16 public int Valign; 17 public int Halign; 18 public string Label; 19 public Poco(string name, string id, int width, bool isvalid, int valign, int halign, string label) 20 { 21 Name = name; Id = id; Width = width; IsValid = isvalid; Valign = valign; Halign = halign; Label = label; 22 } 23 24 25 public static IList<Poco> ToList(object[,] hatchpotch) 26 { 27 var innerLen = 7; 28 var outerLen = hatchpotch.Length / innerLen; 29 return Enumerable.Range(0, outerLen).Select(i => new Poco((string)hatchpotch[i, 0], (string)hatchpotch[i, 1], (int)hatchpotch[i, 2], (bool)hatchpotch[i, 3], (int)hatchpotch[i, 4], (int)hatchpotch[i, 5], (string)hatchpotch[i, 6])).ToList(); 30 } 31 32 public static object[,] ToRect(IList<Poco> data) //理解しやすくするためList<Poco>としてます。実用では副作用に注意しましょう 33 { 34 var ret = new object[data.Count(), 7]; 35 for (var i = 0; i < data.Count(); i++) 36 { 37 ret[i, 0] = (object)data[i].Name; 38 ret[i, 1] = (object)data[i].Id; 39 ret[i, 2] = (object)data[i].Width; 40 ret[i, 3] = (object)data[i].IsValid; 41 ret[i, 4] = (object)data[i].Valign; 42 ret[i, 5] = (object)data[i].Halign; 43 ret[i, 6] = (object)data[i].Label; 44 } 45 return ret; 46 } 47 48 49 } 50 51} 52 53 54 55//実行部 56sub main() 57{ 58 Object[,] Obj = { 59 { "UniqId", "UniqId", 70, false, 11111, 22222, "LABEL_CHAR" }, 60 { "職員名", "ShokuinName", 320, true, 11111, 22222, "LABEL_CHAR" }, 61 { "科目コード", "AAA", 80, true, 11111, 22222, "MONEY" }, 62 }; 63 64 IList<Poco> PocoList = Poco.ToList(Obj); 65 66 67 for (var i = 0; i < 10; i++) 68 { 69 Poco _Poco = new Poco("ほげほげ" + i, "Hogehoge" + i, 80, true, 11111, 22222, "MONEY"); 70 PocoList.Add(_Poco); 71 } 72 73 74 Object[,] Obj2 = Poco.ToRect(PocoList); 75} 76
元々コボラーの独学なので
低レベルなコーディングでお目汚しですが
毎回、案件ごとにSPREAD定義を一から書くのが億劫だったので
SPREAD for WinFormsもASP.NETでも同じように
以下のような関数に[,]を食わせて定義してます。
C#
1 /// <summary> 2 /// スプレッドシート設定 3 /// </summary> 4 /// <param name="sheet"></param> 5 /// <param name="hd"></param> 6 /// <param name="FrozenColumnCount"></param> 7 /// <param name="FrozenRowCount"></param> 8 /// <param name="rowHeader"></param> 9 /// <param name="columnHeader"></param> 10 /// <param name="allowGroup"></param> 11 /// <param name="columnHeaderSet"></param> 12 /// <param name="AlternatingSw"></param> 13 public void InitSpreadStyles(FarPoint.Web.Spread.SheetView sheet, 14 Object[,] hd = null, 15 int FrozenColumnCount = 0, 16 int FrozenRowCount = 0, 17 Boolean rowHeader = true, 18 Boolean columnHeader = true, 19 Boolean allowGroup = false, 20 Boolean columnHeaderSet = true, 21 Boolean AlternatingSw = true) 22 { 23 24 FarPoint.Web.Spread.Inset mg = new FarPoint.Web.Spread.Inset(3, 0, 3, 0); 25 26 // 余白の設定をシートに適用 27 sheet.DefaultStyle.Margin = mg; 28 29 sheet.GridLines = GridLines.Both; 30 31 sheet.DefaultStyle.Border.BorderColor = Color.LightBlue; 32 sheet.DefaultStyle.Border.BorderSize = 1; 33 sheet.DefaultStyle.Border.BorderStyle = BorderStyle.Solid; 34 35 //選択行の色 36 sheet.SelectionBackColor = System.Drawing.Color.Silver; 37 38 // 縦方向の揃え位置を中央に設定 39 sheet.DefaultStyle.VerticalAlign = VerticalAlign.Middle; 40 41 //バインドしたデータでのカラム作成を抑止する 42 sheet.AutoGenerateColumns = false; 43 44 //カラム移動を可能にする 45 sheet.AllowColumnMove = false; 46 47 if (hd != null) 48 { 49 //カラムを作成 50 sheet.ColumnCount = hd.GetLength(0); 51 for (int x = 0; x <= hd.GetLength(0) - 1; ++x) 52 { 53 if (columnHeaderSet) 54 { 55 // カラムヘッダを設定します。 56 sheet.ColumnHeader.Cells[0, x].Text = (String)hd[x, 0]; 57 } 58 59 sheet.Columns[x].DataField = (String)hd[x, 1]; 60 61 sheet.Columns[x].Width = (int)hd[x, 2]; 62 63 sheet.Columns[x].Visible = (Boolean)hd[x, 3]; 64 65 sheet.Columns[x].VerticalAlign = (VerticalAlign)hd[x, 4]; 66 67 sheet.Columns[x].HorizontalAlign = (HorizontalAlign)hd[x, 5]; 68 69 sheet.Columns[x].Font.Name = "Meiryo UI"; 70 71 //セルタイプをセット 72 String cellType = (String)hd[x, 6]; 73 74 if (String.IsNullOrEmpty(cellType)) 75 { 76 sheet.Columns[x].CanFocus = false; 77 sheet.Columns[x].Locked = true; 78 } 79 80 if ("LABEL_NUM".Equals(cellType)) 81 { 82 LabelCellType celltype = new LabelCellType(); 83 celltype.AllowWrap = false; 84 sheet.Columns[x].CellType = celltype; 85 } 86 ・ 87 ・ 88 ・いろんなcelltypeの設定を定義してます 89 ・ 90 ・ 91 ・ 92 93 } 94 } 95 96 // グルーピング設定 97 sheet.AllowGroup = allowGroup; 98 sheet.GroupBarVisible = allowGroup; 99 100 if (AlternatingSw) 101 { 102 // 1行おきにスタイルを設定します 103 sheet.AlternatingRows.Count = 2; 104 System.Drawing.Color fc = System.Drawing.Color.FromName("OldLace"); 105 sheet.AlternatingRows[1].BackColor = fc; 106 } 107 108 // 行ヘッダ表示制御 109 sheet.RowHeader.Visible = rowHeader; 110 // 列ヘッダ表示制御 111 sheet.ColumnHeader.Visible = columnHeader; 112 // 列の固定 113 sheet.FrozenColumnCount = FrozenColumnCount; 114 // 行の固定 115 sheet.FrozenRowCount = FrozenRowCount; 116 117 118 }
回答3件
あなたの回答
tips
プレビュー