实例介绍
【实例简介】
亲测可用无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();
}
}
}
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)