C#环境管理系统

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CCP5626/article/details/72627419

开发环境
VS2012+sql server2014

功能模块
这里写图片描述

页面展示
登录界面

这里写图片描述

主页面

这里写图片描述

通知中心

这里写图片描述
这里写图片描述

用户管理(用户资料可从Excel导入,也可从数据库导出到Excel)

这里写图片描述

信息登记

这里写图片描述

日志管理(错误类型不同写入不同日志文件,每天日志都以日期为前缀)

这里写图片描述

这里写图片描述

更多如打印报表等功能

这里写图片描述
代码视图
这里写图片描述

重要的类与方法:
数据库操作类:见上篇博客(C#酒店管理系统)
日志管理类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Microsoft.Office.Interop.Excel;
using System.IO;
namespace IBootStrapDemo
{
    public class LogManager
    {
        private static string logPath = string.Empty;
        /// <summary>
        /// 保存日志的文件夹
        /// </summary>
        public static string LogPath
        {
            get
            {
                if (logPath == string.Empty)
                {                  
                    logPath = AppDomain.CurrentDomain.BaseDirectory;
                }
                return logPath;
            }
            set { logPath = value; }
        }

        private static string logFielPrefix = string.Empty;
        /// <summary>
        /// 日志文件前缀
        /// </summary>
        public static string LogFielPrefix
        {
            get { return logFielPrefix; }
            set { logFielPrefix = value; }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logFile, string msg)
        {
            try
            {
                System.IO.StreamWriter sw = System.IO.File.AppendText(
                    LogPath + LogFielPrefix + logFile + " " +
                    DateTime.Now.ToString("yyyyMMdd") + ".Log"
                    );
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
                sw.Close();
            }
            catch
            { }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(LogFile logFile, string msg)
        {
            WriteLog(logFile.ToString(), msg);
        }
    }

    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogFile
    {
        Trace,
        Warning,
        Error,
        SQL
    }
}

数据输出到Excel方法:

 //到处数据到Excekl
        public static void ToExcel(DSkin.Controls.DSkinDataGridView dataGridView1)
        {
            try
            {
                //没有数据的话就不往下执行  
                if (dataGridView1.Rows.Count == 0)
                    return;
                //实例化一个Excel.Application对象  
                Excel.Application excel = new Excel.Application();
                //让后台执行设置为不可见,为true的话会看到打开一个Excel,然后数据在往里写  
                excel.Visible = true;

                //新增加一个工作簿,Workbook是直接保存,不会弹出保存对话框,加上Application会弹出保存对话框,值为false会报错  
                excel.Application.Workbooks.Add(true);
                //生成Excel中列头名称  
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {
                    if (dataGridView1.Columns[i].Visible == true)
                    {
                        excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
                    }

                }
                //把DataGridView当前页的数据保存在Excel中  
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    System.Windows.Forms.Application.DoEvents();
                    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                    {
                        if (dataGridView1.Columns[j].Visible == true)
                        {
                            if (dataGridView1[j, i].ValueType == typeof(string))
                            {
                                excel.Cells[i + 2, j + 1] = "'" + dataGridView1[j, i].Value.ToString();
                            }
                            else
                            {
                                excel.Cells[i + 2, j + 1] = dataGridView1[j, i].Value.ToString();
                            }
                        }

                    }
                }

                //设置禁止弹出保存和覆盖的询问提示框  
                excel.DisplayAlerts = true;
                excel.AlertBeforeOverwriting = true;              
                //确保Excel进程关闭  
                excel.Quit();            
                GC.Collect();//如果不使用这条语句会导致excel进程无法正常退出,使用后正常退出
                MessageBox.Show( "文件已经成功导出!", "信息提示");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }

        }

发布公告动态创建Panel类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSkin;
using DSkin.Controls;
using System.Drawing;
using System.Data;

namespace IBootStrapDemo
{
    class NewsMangment
    {
        //发布公告时动态创建panel
        public static void creatNewsPanel(DSkinPanel news,int id, String theme, String infoowner, string time,String content)
        {
            DSkinPanel newspanel = new DSkinPanel();
            DSkinLabel newlab = new DSkinLabel();
            newlab.Font = new Font("微软雅黑", 11);
            newspanel.Controls.Add(newlab);    
            newlab.Text = "\r\n"+"\r\n"+"   "+theme +"       " + infoowner + "        " + time + "\r\n" + "\r\n" +"       " +content;           
            newspanel.Width = 1000;
            newspanel.Height = 180;
            newspanel.BackColor = Color.LightGray;
            news.Controls.Add(newspanel);
            newspanel.Location = new Point(0, id * 184+60);

        }
        //从数据库中加载公告
        public static void viewNews(DSkinPanel newsviewpanel)
        {
            int id;
            string theme, main, owner, time;
            SqlCompose sqlcon = new SqlCompose();
            DataSet data = sqlcon.ExecuteSqlQuery("select * from news_table ");
            for (int i = data.Tables[0].Rows.Count - 1; i >= 0; i--)
            {
                id = (int)data.Tables[0].Rows[i][0];
                theme = data.Tables[0].Rows[i][1].ToString().Trim();
                main = data.Tables[0].Rows[i][2].ToString();
                owner = data.Tables[0].Rows[i][3].ToString().Trim();
                time = data.Tables[0].Rows[i][4].ToString().Trim();
                NewsMangment.creatNewsPanel(newsviewpanel, id, theme, owner, time, main);
            }

        }
    }
}

代码太多就不贴啦。
欢迎私聊讨论。
email:[email protected]

猜你喜欢

转载自blog.csdn.net/CCP5626/article/details/72627419