可以编辑打开的Excel文件吗 [英] Is posible edit opened excel file
问题描述
我将一些数据导出到现有的excel文件中.问题是,用户可以打开此文件.
I exporting some data to existing excel file. Problem is that, user can have this file opened.
对于读写excel,我使用 Microsoft.Office.Interop.Excel.
For reading and writing to excel I use Microsoft.Office.Interop.Excel.
是否可以在Excel桌面应用程序中显示更改的编辑文件?还是可以检查是否在excel中打开了文件?
Is possible edit file with showing changes in Excel desktop app? Or is possible to check if file is opened in excel?
这是我打开该文件的方式:
this is how I open that file:
xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;
workbook = xlApp.Workbooks;
xlWorkBook = workbook.Open(filePath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, false, false);
var worksheets = xlWorkBook.Worksheets;
然后,我遍历所有工作表,并向单元格写入值来填充数据
than I iterate over all sheets and fill data with writing values to Cells
Excel.Worksheet xlWorkSheet = worksheets.get_Item(sheetIndex);
Excel.Range range = GetRange(xlWorkSheet);
range.Cells[row,column] = value;
然后我保存文件:
xlWorkBook.Save();
谢谢雅各布
推荐答案
整个魔术是:
try
{
xlApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
}
catch (Exception ex)
{
if (ex.ToString().Contains("0x800401E3 (MK_E_UNAVAILABLE)"))
{
xlApp = new Excel.Application();
}
else
{
throw;
}
}
这将打开打开的excel应用程序.然后,在此Excel实例中进行所有更改.因此,所有内容都已编辑,可以在打开的excel应用程序中看到.
this will get opened excel application. Then all changes are made in this instance of Excel. So everything is edited, is seen in opened excel application.
谢谢大家
这篇关于可以编辑打开的Excel文件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!