C# 将数据写入C中的现有Excel文件

C# 将数据写入C中的现有Excel文件,c#,asp.net,excel,C#,Asp.net,Excel,我想将数据写入现有Excel文件,同时保留原始数据 该文件有1页;我想写在第二张纸上,然后保存。 问题是每次保存时,它都会创建一个新的Excel文件并覆盖现有的Excel文件 有人能在保存旧数据时提供帮助吗 我有以下功能 using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsApplication1 { public partial class Form1

我想将数据写入现有Excel文件,同时保留原始数据

该文件有1页;我想写在第二张纸上,然后保存。 问题是每次保存时,它都会创建一个新的Excel文件并覆盖现有的Excel文件

有人能在保存旧数据时提供帮助吗

我有以下功能

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

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 
    }
}
根据:

下面是在已经存在的excel文件中进行插入的代码

根据:

下面是在已经存在的excel文件中进行插入的代码


这是我向现有excel文件添加数据的方法:非常简单高效

1-添加Microsoft.Office.Interop.Excel组件作为应用程序的引用 您可以在扩展部分的.NETFramework中找到它

2-然后添加:

using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
3-现在我有了一个简单的类,包含3个方法openExcel、addDataToExcel和closeExcel

public class ExcelFile
{

    private string excelFilePath = string.Empty;
    private int rowNumber = 1; // define first row number to enter data in excel

    Excel.Application myExcelApplication;
    Excel.Workbook myExcelWorkbook;
    Excel.Worksheet myExcelWorkSheet;

    public string ExcelFilePath
    {
        get { return excelFilePath; }
        set { excelFilePath = value; }
    }

    public int Rownumber
    {
        get { return rowNumber; }
        set { rowNumber = value; }
    }

    public void openExcel()
    {
        myExcelApplication = null;

        myExcelApplication = new Excel.Application(); // create Excell App
        myExcelApplication.DisplayAlerts = false; // turn off alerts


        myExcelWorkbook = (Excel.Workbook)(myExcelApplication.Workbooks._Open(excelFilePath, System.Reflection.Missing.Value,
           System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
           System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
           System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
           System.Reflection.Missing.Value, System.Reflection.Missing.Value)); // open the existing excel file

        int numberOfWorkbooks = myExcelApplication.Workbooks.Count; // get number of workbooks (optional)

        myExcelWorkSheet = (Excel.Worksheet)myExcelWorkbook.Worksheets[1]; // define in which worksheet, do you want to add data
        myExcelWorkSheet.Name = "WorkSheet 1"; // define a name for the worksheet (optinal)

        int numberOfSheets = myExcelWorkbook.Worksheets.Count; // get number of worksheets (optional)
    }

    public void addDataToExcel(string firstname, string lastname, string language, string email, string company)
    {

        myExcelWorkSheet.Cells[rowNumber, "H"] = firstname;
        myExcelWorkSheet.Cells[rowNumber, "J"] = lastname;
        myExcelWorkSheet.Cells[rowNumber, "Q"] = language;
        myExcelWorkSheet.Cells[rowNumber, "BH"] = email;
        myExcelWorkSheet.Cells[rowNumber, "CH"] = company;
        rowNumber++;  // if you put this method inside a loop, you should increase rownumber by one or wat ever is your logic

    }

    public void closeExcel()
    {
        try
        {
            myExcelWorkbook.SaveAs(excelFilePath, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                                           System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange,
                                           System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                                           System.Reflection.Missing.Value, System.Reflection.Missing.Value); // Save data in excel


            myExcelWorkbook.Close(true, excelFilePath, System.Reflection.Missing.Value); // close the worksheet


        }
        finally
        {
            if (myExcelApplication != null)
            {
                myExcelApplication.Quit(); // close the excel application
            }
        }

    }
}

这是我向现有excel文件添加数据的方法:非常简单高效

1-添加Microsoft.Office.Interop.Excel组件作为应用程序的引用 您可以在扩展部分的.NETFramework中找到它

2-然后添加:

using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
3-现在我有了一个简单的类,包含3个方法openExcel、addDataToExcel和closeExcel

public class ExcelFile
{

    private string excelFilePath = string.Empty;
    private int rowNumber = 1; // define first row number to enter data in excel

    Excel.Application myExcelApplication;
    Excel.Workbook myExcelWorkbook;
    Excel.Worksheet myExcelWorkSheet;

    public string ExcelFilePath
    {
        get { return excelFilePath; }
        set { excelFilePath = value; }
    }

    public int Rownumber
    {
        get { return rowNumber; }
        set { rowNumber = value; }
    }

    public void openExcel()
    {
        myExcelApplication = null;

        myExcelApplication = new Excel.Application(); // create Excell App
        myExcelApplication.DisplayAlerts = false; // turn off alerts


        myExcelWorkbook = (Excel.Workbook)(myExcelApplication.Workbooks._Open(excelFilePath, System.Reflection.Missing.Value,
           System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
           System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
           System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
           System.Reflection.Missing.Value, System.Reflection.Missing.Value)); // open the existing excel file

        int numberOfWorkbooks = myExcelApplication.Workbooks.Count; // get number of workbooks (optional)

        myExcelWorkSheet = (Excel.Worksheet)myExcelWorkbook.Worksheets[1]; // define in which worksheet, do you want to add data
        myExcelWorkSheet.Name = "WorkSheet 1"; // define a name for the worksheet (optinal)

        int numberOfSheets = myExcelWorkbook.Worksheets.Count; // get number of worksheets (optional)
    }

    public void addDataToExcel(string firstname, string lastname, string language, string email, string company)
    {

        myExcelWorkSheet.Cells[rowNumber, "H"] = firstname;
        myExcelWorkSheet.Cells[rowNumber, "J"] = lastname;
        myExcelWorkSheet.Cells[rowNumber, "Q"] = language;
        myExcelWorkSheet.Cells[rowNumber, "BH"] = email;
        myExcelWorkSheet.Cells[rowNumber, "CH"] = company;
        rowNumber++;  // if you put this method inside a loop, you should increase rownumber by one or wat ever is your logic

    }

    public void closeExcel()
    {
        try
        {
            myExcelWorkbook.SaveAs(excelFilePath, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                                           System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSaveAsAccessMode.xlNoChange,
                                           System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value,
                                           System.Reflection.Missing.Value, System.Reflection.Missing.Value); // Save data in excel


            myExcelWorkbook.Close(true, excelFilePath, System.Reflection.Missing.Value); // close the worksheet


        }
        finally
        {
            if (myExcelApplication != null)
            {
                myExcelApplication.Quit(); // close the excel application
            }
        }

    }
}

在我的案例中使用ClosedXml dll解决了我的问题

 var workbook = new XLWorkbook(fileName);
  var ws1 = workbook.Worksheet(1);
  var ws2 = workbook.Worksheet(2);
  workBook.SaveAs(fileName);
这是我的密码:


在我的案例中使用ClosedXml dll解决了我的问题

 var workbook = new XLWorkbook(fileName);
  var ws1 = workbook.Worksheet(1);
  var ws2 = workbook.Worksheet(2);
  workBook.SaveAs(fileName);
这是我的密码:


你能发布你试过的代码吗?我刚刚编辑了问题。你能发布你试过的代码吗?我刚刚编辑了问题。它不起作用。它仍然会创建新文件,并覆盖现有文件,但它不起作用。它仍然会创建新文件,并覆盖现有文件