简体   繁体   English

如何通过使用Excel Interop添加新工作表来覆盖现有Excel文件-C#

[英]How to Overwrite existing Excel file by adding new sheet with Excel Interop - C#

My program, every hour, does some math calculations and saves these result into excel. 我的程序每小时都会进行一些数学计算,并将这些结果保存到excel中。 When it first run(lets say 08:00 AM) it creates a excel workbook and one sheet namely "Sheet1". 第一次运行时(假设是上午08:00),它将创建一个excel工作簿和一个工作表,即“ Sheet1”。 It saves excel and releases the COM objects. 它保存excel并释放COM对象。 so far everything is fine. 到目前为止,一切都很好。

My problem begins with second run (09:00 AM). 我的问题开始于第二轮跑步(09:00 AM)。 when it tries to save new results, it overwrites existing excel file (This is OK, the way i want it) but it overwrites Sheet1 which was created in 08:00 AM. 当它尝试保存新结果时,它会覆盖现有的excel文件(这是可以的,我想要的方式),但是会覆盖在08:00 AM创建的Sheet1。 I want it to save new result in Sheet2. 我希望它将新结果保存在Sheet2中。

In third run, i want it to save result in Sheet3 在第三次运行中,我希望它将结果保存到Sheet3中
In fourth run, i want it to save result in Sheet4. 在第四次运行中,我希望它将结果保存在Sheet4中。 so on so forth.. 依此类推..

How can i change my code to do like above ? 我如何更改我的代码以执行上述操作? thanks in advance.. 提前致谢..

My Code: 我的代码:

using excelApp = Microsoft.Office.Interop.Excel;

public static void Main(string[] arg)
    {
        while (true)
        {               
            writeToExcel();
            int wait = 3600 * 1000;
            System.Threading.Thread.Sleep(Convert.ToInt32(wait));
        }
    }
public static void writeToExcel()
    {
        excelApp.Application excl = new Microsoft.Office.Interop.Excel.Application();
        excl.Visible = true;

        //MATH CALCULATIONS...... 

        excelApp.Workbook wb = excl.Workbooks.Add(excelApp.XlWBATemplate.xlWBATWorksheet);
        excelApp.Worksheet ws1 = (excelApp.Worksheet)wb.Worksheets[1];
        excelApp.Worksheet ws2 = (excelApp.Worksheet)wb.Sheets.Add();
        excelApp.Worksheet ws3 = (excelApp.Worksheet)wb.Sheets.Add();
        excelApp.Worksheet ws4 = (excelApp.Worksheet)wb.Sheets.Add();
        excelApp.Worksheet ws5 = (excelApp.Worksheet)wb.Sheets.Add();
        excl.DisplayAlerts = false;
        string fileName = string.Format(@"{0}\Data_" + DateTime.Now.Month + "-" DateTime.Now.Day  + ".xlsx", Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
        workSheet.SaveAs(fileName);
        Console.WriteLine("Excel Saved Successfully!!");

        excl.Quit();

        // Release COM objects
        if (excl != null)                                       
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excl);

        if (workSheet != null)                   
        System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);

        excl = null;
        workSheet = null;

        GC.Collect();
}

You need to get the work book from the saved file. 您需要从保存的文件中获取工作簿。 So at the beginning of your routine, you need a mechanism to determine if today's file already exists, if so, use the the following to get your Workbook. 因此,在例程开始时,您需要一种机制来确定今天的文件是否已经存在,如果存在,请使用以下内容获取您的工作簿。

        Workbook WB = ExcelApp.Workbooks.Open(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

Hopefully this helps you see the error in your ways. 希望这可以帮助您以自己的方式查看错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Excel Interop保存/覆盖现有的Excel文件 - C# - How to Save/Overwrite existing Excel file with Excel Interop - C# 上传Excel文件并使用C#将工作表重命名为新工作表名称不使用excel interop - upload excel file and rename sheet to new sheet name using c# not use excel interop 如何覆盖现有的Excel文件的工作表 - How to overwrite an existing Excel file's sheet 如何使用带有 Interop.Excel 库的 C# 从现有 Excel 文件中获取特定元素? - How to get specific elements from an existing Excel file using C# with the Interop.Excel library? C#如何使用Interop重命名Excel工作表 - C# how to rename Excel Sheet using Interop 如何在不使用互操作的情况下通过c#取消隐藏excel表 - How to unhide an excel sheet through c# without using interop 如何复制具有不同名称的工作表 - C# 和 Excel 互操作 - How to copy a sheet with a different name - C# and Excel Interop 如何在C#Excel Interop中将文本添加到现有形状(文本框) - How to add text to an existing shape(textbox) in C# Excel Interop 打开 XML:如何使用 c# 在现有 Excel 文件中追加新工作表后添加行和单元格值 - Open XML: How to add rows and cell value after appending new sheet in existing Excel file using c# 使用Excel.Interop C#保存覆盖和更改 - Save Overwrite and changes using Excel.Interop C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM