C#,WPF怎么把txt里的内容导到DataGrid里

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#,WPF怎么把txt里的内容导到DataGrid里相关的知识,希望对你有一定的参考价值。

txt文档中的数据格式是一列一列的如下图,我想实现点一个按钮,把txt里的内容根据列的多少,显示在DataGrid里面。

参考技术A DataGrid是绑定及显示实体类集合的。所以你首先得自定义一个实体类。然后你在后台通过System.IO读取txt文件并赋值到你的实体集合。 参考技术B 用System.IO

c# winform 报表打印

c#开发winform系统,怎么做报表、表单的打印。
比较简单点的 用户可以自己设置打印表单的样式。

看到过fastreport这个的,但是这个在c#中集成不上。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ToExcelDemo

class ExportExcel

public static void ExportToExcel(System.Windows.Forms.DataGridView grdView)

int rowCount = grdView.Rows.Count;
int columnCount = grdView.Columns.Count;
if (rowCount == 0)

System.Windows.Forms.MessageBox.Show("没有数据可供导出...", "Prompt", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
return;

System.Windows.Forms.SaveFileDialog saveDlg = new System.Windows.Forms.SaveFileDialog();
saveDlg.Title = "导出文件保存路径";
saveDlg.Filter = "Excel files(*.xls)|*.xls";
saveDlg.FilterIndex = 0;
saveDlg.RestoreDirectory = true;
saveDlg.ShowDialog();
string fileName = saveDlg.FileName;
if (fileName.Length != 0)

System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面

if (excel == null)

System.Windows.Forms.MessageBox.Show("Excel无法启动!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
return;

Microsoft.Office.Interop.Excel.Workbooks workBooks = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workBook = (Microsoft.Office.Interop.Excel.Workbook)workBooks.Add(miss);
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;
workSheet.Name = "Test";
excel.Cells[1, 1] = grdView.TopLeftHeaderCell.Value;
for (int i = 0; i < grdView.Columns.Count; i++)

excel.Cells[1, i + 2] = grdView.Columns[i].HeaderText.ToString();

for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)

excel.Cells[rowIndex + 2, 1] = grdView.Rows[rowIndex].HeaderCell.Value.ToString();
for (int colIndex = 0; colIndex < columnCount; colIndex++)

excel.Cells[rowIndex + 2, colIndex + 2] = grdView[colIndex,rowIndex].Value.ToString();


try

workBook.SaveCopyAs(fileName);

catch (Exception ex)

System.Windows.Forms.MessageBox.Show(ex.Message);





我做过一个 看看能不能给你点启发
参考技术A using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
ToExcelDemo

class
ExportExcel

public
static
void
ExportToExcel(System.Windows.Forms.DataGridView
grdView)

int
rowCount
=
grdView.Rows.Count;
int
columnCount
=
grdView.Columns.Count;
if
(rowCount
==
0)

System.Windows.Forms.MessageBox.Show("没有数据可供导出...",
"Prompt",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation);
return;

System.Windows.Forms.SaveFileDialog
saveDlg
=
new
System.Windows.Forms.SaveFileDialog();
saveDlg.Title
=
"导出文件保存路径";
saveDlg.Filter
=
"Excel
files(*.xls)|*.xls";
saveDlg.FilterIndex
=
0;
saveDlg.RestoreDirectory
=
true;
saveDlg.ShowDialog();
string
fileName
=
saveDlg.FileName;
if
(fileName.Length
!=
0)

System.Reflection.Missing
miss
=
System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.ApplicationClass
excel
=
new
Microsoft.Office.Interop.Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
excel.Visible
=
false;//若是true,则在导出的时候会显示EXcel界面
if
(excel
==
null)

System.Windows.Forms.MessageBox.Show("Excel无法启动!",
"Error",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
return;

Microsoft.Office.Interop.Excel.Workbooks
workBooks
=
(Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook
workBook
=
(Microso

以上是关于C#,WPF怎么把txt里的内容导到DataGrid里的主要内容,如果未能解决你的问题,请参考以下文章

C# 、WPF 怎么把程序里的图片拖拽到其它的看图程序啊?

c# winform 报表打印

c#实现遍历文件夹里的所有文件内容,然后删除某个内容?

WPF C#怎么批量读取和创建文件(类似txt文件)

在python怎么样把输入的txt文件里的句子分成词

怎样把多个TXT文档合并成一个,并且以每个文档为单位空一行?