基本信息
源码名称:宿舍管理系统源码(含数据库)
源码大小:5.09M
文件格式:.zip
开发语言:C#
更新时间:2020-05-26
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 3 元 
   源码介绍

亲测可用无Bug。

using System;
using System.Text;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using HotelManage.DataLevl;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using HotelManage.BussinessLevel;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
namespace HotelManage.UILevel
{
    public partial class FeeSet : Form
    {
        private string selectStr;
        private SqlCommand sqlCommand1 = null;
        private SqlDataReader sqlDataReader1 = null;
        private SqlConnection sqlConnection1;
        private void TextClear()
        {
            this.textName.Clear();
            this.textRoomID.Clear();
            this.textRecordDate.Text = "";


        }
        public FeeSet()
        {
            this.sqlConnection1 = new SqlConnection(HotelManage.DataLevl.Connection.ConnString);
            this.sqlCommand1 = new SqlCommand();
            this.sqlCommand1.Connection = this.sqlConnection1;
            InitializeComponent();


        }
        private void UpdateListView(string selectstr)
        {
            this.listView1.Items.Clear();
            this.listView1.Refresh();
            this.sqlCommand1.CommandText = selectstr;
            try
            {
                if (this.sqlConnection1.State == ConnectionState.Closed) this.sqlConnection1.Open();
                this.sqlDataReader1 = this.sqlCommand1.ExecuteReader();
                while (this.sqlDataReader1.Read())
                {
                    ListViewItem li = new ListViewItem();//赋值给listview
                    li.SubItems.Clear();
                    li.SubItems[0].Text = sqlDataReader1["ClientName"].ToString();
                    li.SubItems.Add(sqlDataReader1["RoomID"].ToString()); ;
                    li.SubItems.Add(sqlDataReader1["NetFee"].ToString());
                    li.SubItems.Add(sqlDataReader1["Elec"].ToString());
                    li.SubItems.Add(sqlDataReader1["Water"].ToString());
                    li.SubItems.Add(sqlDataReader1["RoomRent"].ToString());
                    li.SubItems.Add(sqlDataReader1["Total"].ToString());
                    li.SubItems.Add(sqlDataReader1["Remark"].ToString());
                    li.SubItems.Add(sqlDataReader1["RecordDate"].ToString());
                    this.listView1.Items.Add(li);
                }
            }
            catch (System.Exception E)
            {
                MessageBox.Show(E.ToString());

            }
            finally
            {
                this.sqlDataReader1.Close();
                this.sqlConnection1.Close();
            }
            if (this.listView1.Items.Count == 0)
            {
                MessageBox.Show("没有记录存在", "没有记录", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            else
            {
                this.listView1.Items[0].Selected = true;
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.textName.Clear();
            this.textRoomID.Clear();
        }
        private void btnSearch_Click(object sender, EventArgs e)//查找按钮
        {
            bool first = true;
            this.selectStr = "select * from View_FeeSet where";
            if (this.textName.Text == "" && this.textRoomID.Text == "" && this.textRecordDate.Text == "")
            {
                MessageBox.Show("请先输入需要搜索的信息", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                return;
            }


            if (this.textName.Text != "")//如果姓名有输入查询值
            {
                this.selectStr = this.selectStr " ClientName=" "'" this.textName.Text "'";

            }
            else  //如果没数输入查询姓名 下面两种情况
            {
                if (this.textRoomID.Text != "") //如果有输入房间号查询值
                {
                    this.selectStr = this.selectStr " RoomID=" "'" this.textRoomID.Text "'";//查找出没有输入姓名但是有输入房间号值的信息

                    first = true;
                }
                else
                {
                    if (this.textRecordDate.Text != "")//如果没有输入姓名,也没数输入房间值
                    {
                        if (first)
                            this.selectStr = this.selectStr " Convert(varchar(7),RecordDate,120)=" "'" this.textRecordDate.Text "'";
                        else
                            this.selectStr = this.selectStr " and Convert(varchar(7),RecordDate,120)=" "'" this.textRecordDate.Text "'";
                        first = false;
                    }

                }
                //string sql1 = this.selectStr;
                //MessageBox.Show(sql1, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);

            }
            this.UpdateListView(this.selectStr);
            this.TextClear();

            //this.comboRoomType.Dispose();  comboBox1.DataSource=null
        }


        private void FeeSet_Load(object sender, EventArgs e)
        {

        }

        private void btnEmptyRoom_Click(object sender, EventArgs e)//全部按钮
        {
            this.selectStr = "select * from View_FeeSet";
            this.UpdateListView(this.selectStr);
        }

        private void btnCancel_Click_1(object sender, EventArgs e)//清除按钮
        {
            this.textName.Clear();
            this.textRoomID.Clear();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        string sql = "D:";
        private void btnOutput_Click(object sender, EventArgs e)//导出按钮

        {
            ExportToExecl();
        }

        /// <summary>
        /// 执行导出数据
        /// </summary>
        public void ExportToExecl()
        {
            System.Windows.Forms.SaveFileDialog sfd = new SaveFileDialog();
            sfd.DefaultExt = "xls";
            sfd.Filter = "Excel文件(*.xls)|*.xls";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                DoExport(this.listView1, sfd.FileName);
            }
        }
        /// <summary>
        /// 具体导出的方法
        /// </summary>
        /// <param name="listView">ListView</param>
        /// <param name="strFileName">导出到的文件名</param>
        private void DoExport(ListView listView, string strFileName)
        {
            int rowNum = listView.Items.Count;
            int columnNum = listView.Items[0].SubItems.Count;
            int rowIndex = 1;
            int columnIndex = 0;
            if (rowNum == 0 || string.IsNullOrEmpty(strFileName))
            {
                return;
            }
            if (rowNum > 0)
            {

                Microsoft.Office.Interop.Excel.Application xlApp = new Excel.Application();
                if (xlApp == null)
                {
                    MessageBox.Show("无法创建excel对象,可能您的系统没有安装excel");
                    return;
                }
                xlApp.DefaultFilePath = "";
                xlApp.DisplayAlerts = true;
                xlApp.SheetsInNewWorkbook = 1;
                Microsoft.Office.Interop.Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
                //将ListView的列名导入Excel表第一行
                foreach (ColumnHeader dc in listView.Columns)
                {
                    columnIndex ;
                    xlApp.Cells[rowIndex, columnIndex] = dc.Text;
                }
                //将ListView中的数据导入Excel中
                for (int i = 0; i < rowNum; i )
                {
                    rowIndex ;
                    columnIndex = 0;
                    for (int j = 0; j < columnNum; j )
                    {
                        columnIndex ;
                        //注意这个在导出的时候加了“\t” 的目的就是避免导出的数据显示为科学计数法。可以放在每行的首尾。
                        xlApp.Cells[rowIndex, columnIndex] = Convert.ToString(listView.Items[i].SubItems[j].Text) "\t";
                    }
                }
                xlBook.SaveAs(strFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                xlApp = null;
                xlBook = null;
                MessageBox.Show("导出成功","提示信息");
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
    }