[英]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.