public static void WriteTo(this DataTable tbl, Worksheet dest) { dest.UsedRange.Clear(); var arr = tbl.ToArray(); dest.Range["A1"].Resize[arr.GetLength(0), arr.GetLength(1)].Value = arr; }
public static string ToJson(DataTable dt) { //array if (dt.Columns.Count == 1) { //string name = dt.Columns[0].ColumnName; string json = VAL.Boxing(dt.ToArray(row => row[0])).ToJson(); //string.Format("{0}={1}", name, json); return json; } string[] columns = dt.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray(); VAL L = new VAL(); foreach (DataRow row in dt.Rows) { VAL V = new VAL(); for (int i = 0; i < columns.Length; i++) { V.AddMember(columns[i], row[i]); } L.Add(V); } return L.ToJson(); }
public void FromTableToArrayTest() { DataTable table = new DataTable(); table.Columns.Add("A", typeof(bool)); table.Columns.Add("B", typeof(string)); table.Rows.Add(true, "1.0"); table.Rows.Add(true, "0"); table.Rows.Add(false, "1"); table.Rows.Add(false, "0.0"); double[][] actual = table.ToArray(System.Globalization.CultureInfo.InvariantCulture); double[][] expected = { new double[] { 1, 1 }, new double[] { 1, 0 }, new double[] { 0, 1 }, new double[] { 0, 0 }, }; Assert.IsTrue(expected.IsEqual(actual)); }
public void ToArrayTest1() { DataTable table = new DataTable("myData"); table.Columns.Add("Double", typeof(double)); table.Columns.Add("Integer", typeof(int)); table.Columns.Add("Boolean", typeof(bool)); table.Rows.Add(4.20, 42, true); table.Rows.Add(-3.14, -17, false); table.Rows.Add(21.00, 0, false); double[][] expected = { new double[] { 4.20, 42, 1 }, new double[] { -3.14, -17, 0 }, new double[] { 21.00, 0, 0 }, }; double[][] actual = table.ToArray(); Assert.IsTrue(expected.IsEqual(actual)); string[] expectedNames = { "Double", "Integer", "Boolean" }; string[] actualNames; table.ToArray(out actualNames); Assert.IsTrue(expectedNames.IsEqual(actualNames)); }
public List<ScheduledEvents> FindAll() { DataTable dt = new DataTable(); dt.Columns.Add("key", typeof(string)); dt.Columns.Add("scheduletype", typeof(string)); dt.Columns.Add("exetime", typeof(string)); dt.Columns.Add("lastexecuted", typeof(DateTime)); dt.Columns.Add("issystemevent", typeof(bool)); dt.Columns.Add("enable", typeof(bool)); EventInfo[] events = ScheduleConfigs.GetConfig().Events; foreach (EventInfo ev in events) { DataRow dr = dt.NewRow(); dr["key"] = ev.Key; dr["scheduletype"] = ev.ScheduleType; if (ev.TimeOfDay != -1) { dr["exetime"] = "定时执行:" + (ev.TimeOfDay / 60) + "时" + (ev.TimeOfDay % 60) + "分"; } else { dr["exetime"] = "周期执行:" + ev.Minutes + "分钟"; } DateTime lastExecute = Event.GetLastExecuteScheduledEventDateTime(ev.Key, Environment.MachineName); if (lastExecute == DateTime.MinValue) { dr["lastexecuted"] = Convert.ToDateTime("1999-01-01").ToString("yyyy-MM-dd HH:mm:ss"); } else { dr["lastexecuted"] = lastExecute.ToString("yyyy-MM-dd HH:mm:ss"); } dr["issystemevent"] = ev.IsSystemEvent.ToString();// ? "系统级" : "非系统级"; dr["enable"] = ev.Enabled.ToString();// ? "启用" : "禁用"; dt.Rows.Add(dr); } var list = dt.ToArray<ScheduledEvents>(); return list.ToList(); }
private static string ToCSharp(DataTable dt, string clss) { clss = clss ?? string.Empty; //array if (dt.Columns.Count == 1) { var L1 = dt.ToArray(row => VAL.Boxing(row[0]).ToString()); return "{" + string.Join(",", L1) + "}"; } string[] columns = dt.Columns.Cast<DataColumn>().Select(col => col.ColumnName).ToArray(); List<string> L = new List<string>(); foreach (DataRow row in dt.Rows) { List<string> V = new List<string>(); for (int i = 0; i < columns.Length; i++) { V.Add(string.Format("{0}={1}", columns[i], VAL.Boxing(row[i]).ToString())); } L.Add($"new {clss}" + "{" + string.Join(", ", V) + "}"); } return $"new {clss}[]" + "{\n" + string.Join(",\n", L) + "\n}"; }