用WPF写一个表格列分别是日期、总量、合格产品、NG产品,并且可以导出为csv格式
时间: 2024-02-01 18:04:34 浏览: 109
Wpf读取和导出文件Demo
实现以上功能的WPF代码示例如下:
XAML代码:
```xml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Product Report" Height="350" Width="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="Start Date: " VerticalAlignment="Center"/>
<DatePicker x:Name="StartDatePicker" Width="120" Margin="5"/>
<TextBlock Text="End Date: " VerticalAlignment="Center" Margin="5"/>
<DatePicker x:Name="EndDatePicker" Width="120" Margin="5"/>
<Button x:Name="GenerateButton" Content="Generate Report" Click="GenerateButton_Click" Margin="5"/>
<Button x:Name="ExportButton" Content="Export to CSV" Click="ExportButton_Click" Margin="5"/>
</StackPanel>
<DataGrid x:Name="ProductGrid" Grid.Row="1" AutoGenerateColumns="False" Margin="5">
<DataGrid.Columns>
<DataGridTextColumn Header="Date" Binding="{Binding Date}" Width="*" />
<DataGridTextColumn Header="Total" Binding="{Binding Total}" Width="*" />
<DataGridTextColumn Header="Qualified" Binding="{Binding Qualified}" Width="*" />
<DataGridTextColumn Header="NG" Binding="{Binding NG}" Width="*" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
```
C#代码:
```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp1
{
public partial class MainWindow : Window
{
private List<Product> products = new List<Product>();
public MainWindow()
{
InitializeComponent();
InitData();
}
private void InitData()
{
// 初始化数据
var random = new Random();
for (int i = 0; i < 30; i++)
{
var date = DateTime.Today.AddDays(-i);
var total = random.Next(1000, 2000);
var qualified = random.Next(800, total);
var ng = total - qualified;
products.Add(new Product { Date = date, Total = total, Qualified = qualified, NG = ng });
}
ProductGrid.ItemsSource = products;
}
private void GenerateButton_Click(object sender, RoutedEventArgs e)
{
// 根据日期过滤数据
var startDate = StartDatePicker.SelectedDate ?? DateTime.MinValue;
var endDate = EndDatePicker.SelectedDate ?? DateTime.MaxValue;
var filteredProducts = products.Where(p => p.Date >= startDate && p.Date <= endDate).ToList();
ProductGrid.ItemsSource = filteredProducts;
}
private void ExportButton_Click(object sender, RoutedEventArgs e)
{
// 导出CSV文件
var csv = new StringBuilder();
csv.AppendLine("Date,Total,Qualified,NG");
foreach (var product in products)
{
csv.AppendLine($"{product.Date:yyyy/MM/dd},{product.Total},{product.Qualified},{product.NG}");
}
var filename = "product_report.csv";
File.WriteAllText(filename, csv.ToString());
MessageBox.Show($"Product report exported to {filename}");
}
}
public class Product
{
public DateTime Date { get; set; }
public int Total { get; set; }
public int Qualified { get; set; }
public int NG { get; set; }
}
}
```
该示例程序中生成了一些随机数据作为产品的日期、总量、合格产品和NG产品,并在窗口初始化时显示在DataGrid中。用户可以选择起始日期和结束日期,点击“Generate Report”按钮来过滤显示符合日期范围内的产品数据。用户也可以点击“Export to CSV”按钮来将所有产品数据导出为CSV文件,文件名为“product_report.csv”。
阅读全文