DataGrid列表信息保存为EXCEL到指定的路径 ---------继续DataGrid在WPF的数据绑定
一.许多WPF或者Winform程序在保存数据时,会以EXCEL或者文本的形式展现可视化界面的数据,本文就简单介绍下将DataGrid列表信息保存为EXCEL到指定的路径下,例子很简单,用的着的直接用吧。
没有太多的理论性东东,下面直接为大家展现实例。
1.先添加一个按钮用于选择保存路径
1 private void button3_Click(object sender, EventArgs e) 2 { 3 string saveFileName = ""; 4 SaveFileDialog saveDialog = new SaveFileDialog(); 5 saveDialog.DefaultExt = "xls"; 6 saveDialog.Filter = "Excel文件|*.xls"; 7 saveDialog.FileName = "领用历史记录"; 8 saveDialog.ShowDialog(); 9 saveFileName = saveDialog.FileName; 10 ExportExcels(saveFileName, dataGridView1); 11 }
2.下面时将Datagridview显示的数据转化为EXCEL并保存到路径
1 private void ExportExcels(string fileName, DataGridView myDGV) 2 { 3 4 if (fileName.IndexOf(":") < 0) return; //被点了取消 5 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 6 if (xlApp == null) 7 { 8 MessageBox.Show("无法创建Excel对象,可能您未安装Excel"); 9 return; 10 } 11 Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; 12 Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); 13 Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 14 //写入标题 15 for (int i = 0; i < myDGV.ColumnCount; i++) 16 { 17 worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText; 18 } 19 //写入数值 20 for (int r = 0; r < myDGV.Rows.Count; r++) 21 { 22 for (int i = 0; i < myDGV.ColumnCount; i++) 23 { 24 worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value; 25 } 26 System.Windows.Forms.Application.DoEvents(); 27 } 28 worksheet.Columns.EntireColumn.AutoFit();//列宽自适应 29 if (fileName != "") 30 { 31 try 32 { 33 workbook.Saved = true; 34 workbook.SaveAs(fileName); 35 } 36 catch (Exception ex) 37 { 38 MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message); 39 } 40 } 41 xlApp.Quit(); 42 workbook.Close(); 43 GC.Collect();//强行销毁 44 MessageBox.Show("文件: " + fileName + ".xls 保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 45 46 }
3.结果就不给大家显示了,按照EXCEL的流程直接码代码就好了,简单实用
二. 关于WPF中的DataGrid数据绑定问题;
在之前用的DataGrid过程中,由于自己很少再次操作Datagrid,所以一直都是以作为显示的数据集来处理,为了后需的记忆,现在这里记录下便以后堆代码(吼吼)
1.首先定义一个数据类,用于绑定DataGrid的各栏位数据,代码如下:
public class BendView: INotifyPropertyChanged {public string gongdan{ get; set; } public float jizhongming { get; set; } public string liaohao{ get; set; } public string yujingleixing{ get; set; } public DateTime fasongshijian{ get; set; } }
2.前台XAML代码,就是添加一个DataGrid控件。
<DataGrid Name="PendingAlarm" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="False" Margin="4" Background="Transparent"> <DataGrid.ColumnHeaderStyle> <Style TargetType="DataGridColumnHeader"> <Setter Property="HorizontalContentAlignment" Value="Center"/> </Style> </DataGrid.ColumnHeaderStyle> <DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="HorizontalContentAlignment" Value="Center"/> </Style> </DataGrid.CellStyle> <DataGrid.Columns> <DataGridTextColumn Header="工单" Binding="{Binding gongdan}" Width="*"/> <DataGridTextColumn Header="机种名" Binding="{Binding jizhongming }" Width="*"/> <DataGridTextColumn Header="料号" Binding="{Binding liaohao}" Width="*" /> <DataGridTextColumn Header="预警类型" Binding="{Binding yujingleixing}" Width="*"/> <DataGridTextColumn Header="发送日期" Binding="{Binding fasongshijian}" Width="*"/> <DataGridTemplateColumn Header="操作" Width="*"> <DataGridTemplateColumn.CellTemplate > <DataTemplate> <Button Content="处理" Click="Deal_BtnClicked"></Button> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>
3.向Datagrid添加数据
ObservableCollection<BendView> peopleList = new ObservableCollection<BendView>();
peopleList.Clear(); for (int i = 0; i < DT.Tables[0].Rows.Count; i++) { peopleList.Add(new BendView() { gongdan = DT.Tables[0].Rows[i][0].ToString(), jizhongming = DT.Tables[0].Rows[i][1].ToString(), liaohao = DT.Tables[0].Rows[i][2].ToString(), yujingleixing = DT.Tables[0].Rows[i][3].ToString(), fasongshijian = DT.Tables[0].Rows[i][4].ToString(), }); } PendingAlarm.ItemsSource = peopleList;