如何在 C# Wpf 中使用 Datarow 写入 Excel 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22665176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:21:12  来源:igfitidea点击:

How to write to excel file using Datarow in C# Wpf

c#wpfexcel

提问by user3269629

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.

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

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

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

table.Rows.Add(datarow);

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.

每次将 Datarow 添加到数据表时,都会重复此过程。它不应替换 Excel 文件中存在的先前数据,而是应每次将 datarow 附加到 Excel 文件中。

回答by Gun

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

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

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();
        }
    }
}