如何使用C#Wpf中的Datarow写入excel文件 [英] How to write to excel file using Datarow in C# Wpf

查看:344
本文介绍了如何使用C#Wpf中的Datarow写入excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Wpf应用程序,其中我要插入数据行到excel文件。当数据行被添加到datatable时,我希望该数据行保存在Excel文件中。



在此C#语句之前,Datarow应保存在excel文件中: / p>

table.Rows.Add(datarow);



每次将Datarow添加到datatable.It不应该替换Excel文件中存在的以前的数据,而应该每次将数据行追加到excel文件。

解决方案

尝试下面的代码,并确保从引用添加 Microsoft.Office.Interop.Excel dll引用。



如果您希望您可以进一步优化代码,但下面的一个可以用于您的基本了解。

  using System; 
使用System.Data;
使用System.Windows;
使用Excel = Microsoft.Office.Interop.Excel;

命名空间DatagridDemo
{
public partial class MainWindow:Window
{
public MainWindow()
{
InitializeComponent() ;

//定义Datatble
DataTable dt = new DataTable();

//创建Excel应用程序实例
Excel.Application ExcelApp = new Excel.Application();

//创建工作簿实例并从以下位置打开工作簿
Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(@E:\test.xlsx);

dt.Columns.Add(EmpNo,typeof(int));
dt.Columns.Add(EmpName,typeof(string));对于(int i = 1; i


{
ExcelApp.Cells [1,i] = dt.Columns [i-1] .ColumnName;
}

//创建DataRow
DataRow dr = dt.NewRow();

dr [0] = 1;
dr [1] =ABC;

ExcelApp.Cells [2,1] = dr [0] .ToString();
ExcelApp.Cells [2,2] = dr [1] .ToString();

dt.Rows.Add(dr);

dr = dt.NewRow();

dr [0] = 2;
dr [1] =DEF;

ExcelApp.Cells [3,1] = dr [0] .ToString();
ExcelApp.Cells [3,2] = dr [1] .ToString();

dt.Rows.Add(dr);

dr = dt.NewRow();

dr [0] = 3;
dr [1] =XYZ;

ExcelApp.Cells [4,1] = dr [0] .ToString();
ExcelApp.Cells [4,2] = dr [1] .ToString();

dt.Rows.Add(dr);

//保存工作簿
ExcelWorkBook.Save();

//关闭工作簿
ExcelWorkBook.Close();

//退出excel进程
ExcelApp.Quit();
}
}
}


I have a Wpf application in which I want to insert data row to excel file. At the time when datarow is added to the datatable, I want that datarow to get saved in Excel file.

Datarow should be saved in excel file before this C# statement:

table.Rows.Add(datarow);

This process will be repeated each time when Datarow is added to datatable.It should not replace the previous data present in the Excel file, rather it should append datarow to excel file each time.

解决方案

try the below code and make sure add Microsoft.Office.Interop.Excel dll reference from references.

If you want you can optimize the code further but the below one works for your basic understanding

using System;
using System.Data;
using System.Windows;
using Excel = Microsoft.Office.Interop.Excel;

namespace DatagridDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //Define Datatble
            DataTable dt = new DataTable();

            //Create Excel Application Instance
            Excel.Application ExcelApp = new Excel.Application();

            //Create workbook Instance and open the workbook from the below location
            Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Open(@"E:\test.xlsx");

            dt.Columns.Add("EmpNo", typeof(int));
            dt.Columns.Add("EmpName", typeof(string));


            for (int i = 1; i < dt.Columns.Count + 1; i++)
            {
                ExcelApp.Cells[1, i] = dt.Columns[i - 1].ColumnName;
            }

            //Create DataRow
            DataRow dr = dt.NewRow();

            dr[0] = 1;
            dr[1] = "ABC";

            ExcelApp.Cells[2, 1] = dr[0].ToString();
            ExcelApp.Cells[2, 2] = dr[1].ToString();

            dt.Rows.Add(dr);

            dr = dt.NewRow();

            dr[0] = 2;
            dr[1] = "DEF";

            ExcelApp.Cells[3, 1] = dr[0].ToString();
            ExcelApp.Cells[3, 2] = dr[1].ToString();

            dt.Rows.Add(dr);

            dr = dt.NewRow();

            dr[0] = 3;
            dr[1] = "XYZ";

            ExcelApp.Cells[4, 1] = dr[0].ToString();
            ExcelApp.Cells[4, 2] = dr[1].ToString();

            dt.Rows.Add(dr);

            //Save the workbook
            ExcelWorkBook.Save();

            //Close the workbook
            ExcelWorkBook.Close();

            //Quit the excel process
            ExcelApp.Quit();
        }
    }
}

这篇关于如何使用C#Wpf中的Datarow写入excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆