9、员工的复合查询与NPOI导出Excel
2013-05-15 12:16
211 查看
1、基础框架准备工作
同6、员工档案管理
3、DAL层建立EmployeeDAL的类添加Search方法
4、EmployeeListUI界面添加GroupBox 搜索Box。
4.1 ListUI
4.2 查询交互
UI层第一次需要引用Model与DAL
5、NOPI下载与引用
NOPI下载,在VS用新将两个dll文件引用。
同6、员工档案管理
3、DAL层建立EmployeeDAL的类添加Search方法
public Employee[] Search(string sql,List<SqlParameter> parameter) { DataTable dt = SqlHelper.ExecuteDataTable(sql,parameter.ToArray()); Employee[] empl = new Employee[dt.Rows.Count]; for (int i = 0; i < dt.Rows.Count; i++) { empl[i] = ToEmployee(dt.Rows[i]); } return empl; }
4、EmployeeListUI界面添加GroupBox 搜索Box。
4.1 ListUI
<GroupBox Header="搜索" Height="100" DockPanel.Dock="Top"> <Grid> <CheckBox Name="cbSearchByName" Content="姓名" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,0,0,0"/> <TextBox Name="txtName" HorizontalAlignment="Left" Height="24" Margin="57,-4,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/> <CheckBox Name="cbSearchByInDate" Content="入职时间" HorizontalAlignment="Left" Margin="219,0,0,0" VerticalAlignment="Top"/> <DatePicker Name="dpInDateStart" HorizontalAlignment="Left" Margin="302,-4,0,0" VerticalAlignment="Top"/> <TextBlock HorizontalAlignment="Left" Margin="389,0,0,0" TextWrapping="Wrap" Text="至" VerticalAlignment="Top"/> <DatePicker Name="dpInDateEnd" HorizontalAlignment="Left" Margin="415,-4,0,0" VerticalAlignment="Top"/> <CheckBox Name="cbSearchByDept" Content="部门" HorizontalAlignment="Left" Margin="10,37,0,0" VerticalAlignment="Top"/> <ComboBox Name="cmbDept" DisplayMemberPath="Name" SelectedValuePath="Id" HorizontalAlignment="Left" Margin="57,34,0,0" VerticalAlignment="Top" Width="120"/> <Button Content="查询" Name="btnSearch" HorizontalAlignment="Left" Margin="521,34,0,0" VerticalAlignment="Top" Width="75" Click="btnSearch_Click" RenderTransformOrigin="1.187,0.545"/> <CheckBox x:Name="cbSearchByBaseSalary" Content="基本工资" HorizontalAlignment="Left" Margin="219,37,0,0" VerticalAlignment="Top"/> <TextBox x:Name="cbBaseSalarEnd" HorizontalAlignment="Left" Height="23" Margin="415,33,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="82"/> <TextBlock HorizontalAlignment="Left" Margin="389,36,0,0" TextWrapping="Wrap" Text="---" VerticalAlignment="Top"/> <TextBox x:Name="cbBaseSalarStart" HorizontalAlignment="Left" Height="23" Margin="302,33,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="82"/> </Grid> </GroupBox>
4.2 查询交互
UI层第一次需要引用Model与DAL
namespace HRMSys.UI.SystemMgr { /// <summary> /// EmployeeListUI.xaml 的交互逻辑 /// </summary> public partial class EmployeeListUI : Window { public EmployeeListUI() { InitializeComponent(); } private void btnAdd_Click(object sender, RoutedEventArgs e) { //新增按钮 EmployeeEditUI edit = new EmployeeEditUI(); edit.IsAddNew = true; if (edit.ShowDialog() == true) { LoadData(); } } private void Window_Loaded_1(object sender, RoutedEventArgs e) { //column中的部门与学历 columnDepartmentId.ItemsSource = new DepartmentDAL().ListAll(); columnEducationId.ItemsSource = new IdNameDAL().GetByCategory("学历"); LoadData(); cbSearchByName.IsChecked = true; cbSearchByDept.IsChecked = true; cbSearchByBaseSalary.IsChecked = true; dpInDateStart.SelectedDate = DateTime.Today.AddMonths(-1); dpInDateEnd.SelectedDate = DateTime.Today; cmbDept.ItemsSource = new DepartmentDAL().ListAll(); } private void LoadData() { gridEmployee.ItemsSource = new EmployeeDAL().ListAll(); } private void bthDelete_Click(object sender, RoutedEventArgs e) { Employee empl = (Employee)gridEmployee.SelectedItem; if (empl == null) { MessageBox.Show("没有选中任何数据"); return; } else if (MessageBox.Show("你真的要删除吗", "警告", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { new EmployeeDAL().Deletet(empl.Id); LoadData(); } } private void btnEdit_Click(object sender, RoutedEventArgs e) { Employee empl = (Employee)gridEmployee.SelectedItem; if (empl == null) { MessageBox.Show("你没有选择任何行!"); return; } EmployeeEditUI edit = new EmployeeEditUI(); edit.EditingId = empl.Id; edit.IsAddNew = false; if (edit.ShowDialog() == true) { LoadData(); } } private void btnSearch_Click(object sender, RoutedEventArgs e) { //搜索 List<string> whereList = new List<string>(); List<SqlParameter> parameter = new List<SqlParameter>(); if (cbSearchByName.IsChecked == true) { whereList.Add("Name=@Name"); parameter.Add(new SqlParameter("@Name",txtName.Text)); } if (cbSearchByInDate.IsChecked == true) { whereList.Add("InDate>=@InDateStart and InDate<=@InDateEnd"); parameter.Add(new SqlParameter("@InDateStart", dpInDateStart.SelectedDate)); parameter.Add(new SqlParameter("@InDateEnd", dpInDateEnd.SelectedDate)); } if (cbSearchByDept.IsChecked == true) { whereList.Add("DepartmentId=@DepartmentId"); parameter.Add(new SqlParameter("@DepartmentId", cmbDept.SelectedValue)); } if (cbSearchByBaseSalary.IsChecked == true) { whereList.Add("BaseSalary>=@BaseSalaryStart and BaseSalary<@BaseSalaryEnd"); parameter.Add(new SqlParameter("@BaseSalaryStart", cbBaseSalarStart.Text)); parameter.Add(new SqlParameter("@BaseSalaryEnd", cbBaseSalarEnd.Text)); } string whereSql = string.Join(" and ",whereList); string sql = "select * from T_Employee"; if (whereSql.Length > 0) { sql = sql + " where " + whereSql; } Employee[] result = new EmployeeDAL().Search(sql, parameter); gridEmployee.ItemsSource = result; } private void btExport_Click(object sender, RoutedEventArgs e) { SaveFileDialog sdfExport = new SaveFileDialog(); sdfExport.Filter = "Excel文件|*.xls"; if (sdfExport.ShowDialog() != true) { return; } string filename = sdfExport.FileName; HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("员工数据"); IRow rowHeader = sheet.CreateRow(0);//表头行 rowHeader.CreateCell(0, CellType.STRING).SetCellValue("姓名"); rowHeader.CreateCell(1, CellType.STRING).SetCellValue("工号"); rowHeader.CreateCell(2, CellType.STRING).SetCellValue("入职日期"); //把查询结果导出到Excel Employee[] employees = (Employee[])gridEmployee.ItemsSource; for (int i = 0; i < employees.Length; i++) { Employee employee = employees[i]; IRow row = sheet.CreateRow(i + 1); row.CreateCell(0, CellType.STRING).SetCellValue(employee.Name); row.CreateCell(1, CellType.STRING).SetCellValue(employee.Number); ICellStyle styledate = workbook.CreateCellStyle(); IDataFormat format = workbook.CreateDataFormat(); //格式具体有哪些请看单元格右键中的格式,有说明 styledate.DataFormat = format.GetFormat("yyyy\"年\"m\"月\"d\"日\""); ICell cellInDate = row.CreateCell(2, CellType.NUMERIC); cellInDate.CellStyle = styledate; cellInDate.SetCellValue(employee.InDate); } using (Stream stream = File.OpenWrite(filename)) { workbook.Write(stream); } } } }
5、NOPI下载与引用
NOPI下载,在VS用新将两个dll文件引用。
相关文章推荐
- C# 使用NPOI 导出Excel
- NPOI 导入导出excel 支持 03 07
- 将数据在[Excel]和[任何数据源]之间导入导出_可临时保存查询结果
- NPOI的使用Excel模板导出 可插入到指定行
- poi 对查询结果导出到excel
- AX2009使用NPOI导出EXCEL功能封装
- Jxls导出excel的若干方式总结(六)-- 分sheet导出查询记录,每条记录一个sheet
- oracle 查询表结构并导出EXCEL
- ●导出excel(NPOI)
- NPOI 导出数据到Excel(包括图片)
- NPOI 导出Excel 数据方式
- 回车即查询、excel查询导出、按钮置灰
- 使用NPOI实现word和excel的导入导出
- 使用NPOI将TABLE内容导出到EXCEL
- NPOI 利用DataTable导出Excel 2003和2007
- Elasticsearch获取ES查询的所有结果,并批量导出Excel2
- 从gridview 中查询数据导出Excel 简单方法
- Mysql查询结果导出为Excel的几种方法
- 项目实战之—使用NPOI导入导出Excel
- .net 使用NPOI或MyXls把DataTable导出到Excel