一、数据导出:
目标: 将数据库的数据导出成Excel工作表或是Word文档
基本步骤:
1、首先将数据库中的数据封装成实体类
2、写好查询数据的方法,在主窗体中调用查看所有的数据
3、利用saveFileDialog 和StreamWriter将数据导出去,期间需要用到HTML中的table表格结构
即<table>
<tr><td></td></tr>
</table>
方法1:用字符串拼接,但此法效率低
方法2:
List<Users> list = new List<Users>(); public Form1() { InitializeComponent(); list = new UsersData().Select(); int id = 1; foreach (Users u in list)//遍历数据库中的所有数据放到泛型集合中 { ListViewItem li = new ListViewItem(); li.Text = id.ToString(); li.SubItems.Add(u.UserName); li.SubItems.Add(u.PassWord); li.SubItems.Add(u.NickName); li.SubItems.Add(u.SexStr); li.SubItems.Add((DateTime.Now.Year - u.Birthday.Year).ToString()); li.SubItems.Add(u.BirthdayStr); li.SubItems.Add(u.NationName); listView1.Items.Add(li); id++; } } private void button1_Click(object sender, EventArgs e) { saveFileDialog1.Filter = "Excel工作表|*.xlsx|Word文档|*.doc";//保存的时候需要显示的文件保存类型 saveFileDialog1.FileName = DateTime.Now.Year + "年" + DateTime.Now.Month + "月人员数据";//文件默认名称 DialogResult dr = saveFileDialog1.ShowDialog(); //获取一个用户要保存的路径 if (dr == DialogResult.OK) { StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); int id = 1; StringBuilder ss = new StringBuilder(); ss.Append("<table border=\"1\">"); ss.Append("<tr><td>序号</td><td>用户名</td><td>密码</td><td>昵称</td><td>性别</td><td>年龄</td><td>生日</td><td>民族</td></tr>"); foreach (Users u in list) { ss.Append("<tr>"); ss.Append("<td>" + id + "</td>"); ss.Append("<td>" + u.UserName + "</td>"); ss.Append("<td>" + u.PassWord + "</td>"); ss.Append("<td>" + u.NickName + "</td>"); ss.Append("<td>" + u.SexStr + "</td>"); ss.Append("<td>" + (DateTime.Now.Year - u.Birthday.Year) + "</td>"); ss.Append("<td>" + u.BirthdayStr + "</td>"); ss.Append("<td>" + u.NationName + "</td>"); ss.Append("</tr>"); id++; } ss.Append("</table>"); sw.Write(ss); sw.Close(); }
二、 TreeView的使用
1、依然是将数据库中的数据封装成实体类
2、写查询所有数据的方法(以下以中国、省、市、县为例)
3.主窗体中的按钮事件和方法
private void button1_Click(object sender, EventArgs e) { List<ChinaStates> list1 = new ChinaData().Select(); TreeNode tn = new TreeNode("中国"); tn.Tag = "0001";//用户自定义数据 treeView1.Nodes.Add(tn); TreeViewBind(tn, list1); } private static void TreeViewBind(TreeNode tn, List<ChinaStates> list) { foreach (ChinaStates c in list) { if (c.ParentAreaCode == tn.Tag.ToString()) { TreeNode tnn = new TreeNode(c.AreaName); tnn.Tag = c.AreaCode; tn.Nodes.Add(tnn); TreeViewBind(tnn, list); } }
private void button1_Click(object sender, EventArgs e) { List<ChinaStates> list1 = new ChinaData().Select(); TreeNode tn = new TreeNode("中国"); tn.Tag = "0001"; treeView1.Nodes.Add(tn); tn.Nodes.Add(new TreeNode("加载中...")); } private static void TreeViewBind(TreeNode tn, List<ChinaStates> list) { List<ChinaStates> list = new ChinaData().Select(tn.Tag.ToString()); foreach (ChinaStates c in list) { TreeNode tnn = new TreeNode(c.AreaName); tnn.Tag = c.AreaCode; tn.Nodes.Add(tnn); tnn.Nodes.Add(new TreeNode("加载中...")); } } private void treeView1_AfterExpand(object sender, TreeViewEventArgs e) { List<ChinaStates> list = new ChinaData().Select(e.Node.Tag.ToString()); e.Node.Nodes.Clear(); foreach (ChinaStates c in list) { TreeNode tnn = new TreeNode(c.AreaName); tnn.Tag = c.AreaCode; e.Node.Nodes.Add(tnn); tnn.Nodes.Add(new TreeNode("加载中...")); } }
ToolTip后台代码
1.ToolTip.Show时显示不能隐藏解决方法:
this.toolTip1.Dispose(); this.toolTip1 = new ToolTip(); this.toolTip1.Show("个性签名更新:"+U.Write,this.U_Write);
2.
ToolTip toolTip1 = new ToolTip(); toolTip1.AutoPopDelay = 5000; toolTip1.InitialDelay = 1000; toolTip1.ReshowDelay = 500; toolTip1.ShowAlways = true; toolTip1.SetToolTip(this.U_Write, "个性签名更新:" + U.Write);