一.许多WPF或者Winform程序在保存数据时,会以EXCEL或者文本的形式展现可视化界面的数据,本文就简单介绍下将DataGrid列表信息保存为EXCEL到指定的路径下,例子很简单,用的着的直接用吧。
没有太多的理论性东东,下面直接为大家展现实例。
1.先添加一个按钮用于选择保存路径
private void button3_Click(object sender, EventArgs e)
{
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = "领用历史记录";
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
ExportExcels(saveFileName, dataGridView1);
}
2.下面时将Datagridview显示的数据转化为EXCEL并保存到路径
private void ExportExcels(string fileName, DataGridView myDGV)
{ if (fileName.IndexOf(":") < ) return; //被点了取消
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您未安装Excel");
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[];//取得sheet1
//写入标题
for (int i = ; i < myDGV.ColumnCount; i++)
{
worksheet.Cells[, i + ] = myDGV.Columns[i].HeaderText;
}
//写入数值
for (int r = ; r < myDGV.Rows.Count; r++)
{
for (int i = ; i < myDGV.ColumnCount; i++)
{
worksheet.Cells[r + , i + ] = myDGV.Rows[r].Cells[i].Value;
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
if (fileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveAs(fileName);
}
catch (Exception ex)
{
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
xlApp.Quit();
workbook.Close();
GC.Collect();//强行销毁
MessageBox.Show("文件: " + fileName + ".xls 保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
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="" 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 = ; i < DT.Tables[].Rows.Count; i++)
{
peopleList.Add(new BendView()
{
gongdan = DT.Tables[].Rows[i][].ToString(),
jizhongming = DT.Tables[].Rows[i][].ToString(),
liaohao = DT.Tables[].Rows[i][].ToString(),
yujingleixing = DT.Tables[].Rows[i][].ToString(),
fasongshijian = DT.Tables[].Rows[i][].ToString(),
});
}
PendingAlarm.ItemsSource = peopleList;