MainWindow.xaml:
<Window x:Class="HelloSolution.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:HelloSolution"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DataGrid AutoGenerateColumns="False" GridLinesVisibility="None" CanUserDeleteRows="False"
CanUserAddRows="False" Margin="0,4" Grid.Row="0" FontSize="16" ItemsSource="{Binding YTTable}">
<DataGrid.Columns>
<DataGridTextColumn Header="PtypId" Binding="{Binding PtypId}" Width="120" />
<DataGridTextColumn Header="UserCode" Binding="{Binding UserCode}" Width="120" />
<DataGridTextColumn Header="Full
Name" Binding="{Binding FullName}" Width="120" />
</DataGrid.Columns>
</DataGrid>
<Button Name="ClickMe" Content="Click Me" Height="36" Command="{Binding AddCommand}" Grid.Row="1"/>
</Grid>
</Window>
MainWindow.xaml.cs:
using HelloSolution.ViewModels;
using System.Windows;
namespace HelloSolution
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
}
}
MainWindowViewModel.cs
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System;
using System.Data;
namespace HelloSolution.ViewModels
{
class MainWindowViewModel:NotificationObject
{
public DelegateCommand AddCommand { get; set; }
private DataTable yTTable;
public DataTable YTTable
{
get { return yTTable; }
set
{
yTTable = value;
this.RaisePropertyChanged("YTTable");
}
}
public MainWindowViewModel()
{
YTTable = new System.Data.DataTable();
YTTable.Columns.Add(new DataColumn("PtypId", typeof(string)));
YTTable.Columns.Add(new DataColumn("UserCode", typeof(string)));
YTTable.Columns.Add(new DataColumn("FullName", typeof(string)));
this.AddCommand = new DelegateCommand(new Action(this.Add));
}
private void Add()
{
DataRow ytRow = YTTable.NewRow();
ytRow["PtypId"] = "dddd";
ytRow["UserCode"] = "eeee";
ytRow["FullName"] = "qqqqqqqq";
YTTable.Rows.Add(ytRow);
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
https://blog.csdn.net/qq_37246121/article/details/79652062
namespace BtsMaster
{
public class CSVhelper
{
/// <summary>
/// 将Csv读入DataTable
/// </summary>
/// <param name="filePath">csv文件路径</param>
/// <param name="n">表示第n行是字段title,第n+1行是记录开始</param>
/// <param name="k">可选参数表示最后K行不算记录默认0</param>
public DataTable csv2dt(string filePath, int n,DataTable dt) //这个dt 是个空白的没有任何行列的DataTable
{
String csvSplitBy = "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)";
StreamReader reader = new StreamReader(filePath, System.Text.Encoding.Default, false);
int i = 0, m = 0;
reader.Peek();
while (reader.Peek() > 0)
{
m = m + 1;
string str = reader.ReadLine();
if (m >= n + 1)
{
if (m == n + 1) //如果是字段行,则自动加入字段。
{
MatchCollection mcs = Regex.Matches(str, csvSplitBy);
foreach (Match mc in mcs)
{
dt.Columns.Add(mc.Value); //增加列标题
}
}
else
{
MatchCollection mcs = Regex.Matches(str, "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)");
i = 0;
System.Data.DataRow dr = dt.NewRow();
foreach (Match mc in mcs)
{
dr[i] = mc.Value;
i++;
}
dt.Rows.Add(dr); //DataTable 增加一行
}
}
}
return dt;
}
}
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
https://blog.csdn.net/xinyalong/article/details/90031816
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
Messenger.Default.Register<string>(this, "MasterCommand", ExecuteMasterCmd);
MasterCmd = new RelayCommand<string>(ExecuteMasterCmd);
}
public RelayCommand<string> MasterCmd
{
get;
private set;
}
void ExecuteMasterCmd(string arg)
{
try
{
string primaryname = "个人信息" + DateTime.Now.ToString("yyyyMMddHHmmss");
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "CSV (*.CSV)|*.csv"; ;
saveFileDialog.AddExtension = true;//是否自动添加扩展名
saveFileDialog.OverwritePrompt = true;//文件已存在是否提示覆盖
saveFileDialog.CheckPathExists = true;//提示输入的文件名无效
saveFileDialog.FileName = primaryname;//文件初始名
if (saveFileDialog.ShowDialog() == true)
{
//首先模拟建立将要导出的数据,这些数据都存于DataTable中
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Type", typeof(string));
dt.Columns.Add("StationID", typeof(string));
dt.Columns.Add("Time", typeof(string));
DataRow row;
for (int i = 0; i < 3; i++)
{
row = dt.NewRow();
row["ID"] = "123";
row["Name"] = "成龙";
row["Type"] = "男";
row["StationID"] = "666";
row["Time"] = "2019-4-25";
dt.Rows.Add(row);
}
string path = saveFileDialog.FileName.ToString();//保存路径
System.IO.FileStream fs = new FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, new System.Text.UnicodeEncoding());
//Tabel header
for (int i = 0; i < dt.Columns.Count; i++)
{
sw.Write(dt.Columns[i].ColumnName);
sw.Write("\t");
}
sw.WriteLine("");
//Table 数据块
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
sw.Write(dt.Rows[i][j].ToString());
sw.Write("\t");
}
sw.WriteLine("");
}
sw.Flush();
sw.Close();
MessageBox.Show("导出成功", "提示");
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}