在桌面上打开**时是否可以写入现有的Excel文件?

我正在创build一个将数据写入现有Excel文件的代码。 只有在文件closures的情况下才能读/写文件。 如果在桌面上打开文件时尝试写入文件,则不会更改或保存文档。 在运行代码之前或运行时打开Excel文件时,我也无法closures工作簿(使用.close())或退出应用程序(使用.quit())。

有没有办法,我可以写入Excel文件,而它是在我的桌面上打开,实际显示的变化? 或者我可以至lessclosures一个已经打开的文件读/写,保存并用C#代码再次打开它? 这是我正在使用的代码; 它有点无组织,但如果你运行它,你可以看到我基本上试图做什么。 请不要将您要保存文件的属地址更改为代码工作(一般地址保存为名为excelsource的stringvariables)。 代码将首先创build一个以今天的月份和date(MM_YY)命名的文件。 每次初始化代码时都会继续写入。 如果尝试在新创build的文件打开时写入文件,则不会对文件应用更改(只有在文件closures时才写入Excel文件)。

using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Excel = Microsoft.Office.Interop.Excel; using System.Reflection; using System.Diagnostics; using System.Threading; namespace ConsoleApplication4 { class Program { static public string excelsource = @"\user\desktop\generic\"; // excelsource is the "general" address of where excel file wil be saved. static public bool truth; static public bool truth1; static public bool scan_thru = false; static public int range2; static public int index = 1; static Excel.Application excel = new Excel.Application(); static Excel.Workbook workbook; static Excel.Worksheet sheet; static Excel.Range range; static FileInfo file; static void Main(string[] args) { DateTime current_time = DateTime.Now; string file_name = excelsource + current_time.Month.ToString() + "_" + current_time.Year.ToString() + ".xlsx"; string curfile = file_name; truth = File.Exists(curfile); // truth determines if file exists if truth == true file exists and does not need to be created, if false a new file is created. if (truth == false) { workbook = excel.Workbooks.Add(); sheet = workbook.Sheets[1]; sheet.Name = (current_time.Month.ToString() + "_" + current_time.Day + "_" + current_time.Year); } else { file = new FileInfo(file_name); truth1 = IsFileinUse(file); // truth1 determines if the existing file is open; truth1 == false if the file is currently closed and is true when it is open. workbook = excel.Workbooks.Open(file_name); sheet = workbook.Sheets[current_time.Month.ToString() + "_" + current_time.Day + "_" + current_time.Year]; if (truth1 == true) { excel.Visible = false; excel.DisplayAlerts = false; workbook.Save(); } while (scan_thru == false & truth1 == false) { string box = "A" + index.ToString(); range = sheet.get_Range(box, Missing.Value); string range1 = range.Text; bool judge = int.TryParse(range1, out range2); if (judge == true) { index++; } else { scan_thru = true; } } scan_thru = false; } int i = index; while (i < (index + 100) & truth1 == false) { sheet.Cells[i, "A"].Value2 = i.ToString(); i++; } if (truth == false) { workbook.SaveAs(file_name); } if (truth == true & truth1 == false) { excel.DisplayAlerts = false; } index = 1; workbook.Close(true); excel.Quit(); } protected static bool IsFileinUse(FileInfo file) { FileStream stream = null; try { stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch (IOException) { return true; } finally { if (stream != null) stream.Close(); } return false; } } 

}

从C#应用程序中这是不可能做到的。 在Excel中打开文件时,它会阻止来自其他应用程序的写入访问,以便在打开的文件中保持数据完整性。 这意味着您应该拥有只读访问权限。

此外,强制打开和closures来自外部应用程序的任何应用程序是非常非常less的更新文件的最佳方法。 如果您正在修改当前打开的文件的数据,您应该学习编写Excelmacros。

编辑1:澄清问题后:

您不能添加到打开的Excel文件。 但是,如果我正确理解你的问题,我认为这将解决你的问题(我试过了,它适用于我):

  1. 写入一个以逗号分隔的.csv文件,您可以控制从C#应用程序读取/写入的权限。
  2. 在Excel电子表格“From Text”中添加引用.csv文件的“External Data Source”。 来源: https : //support.office.com/en-us/article/Import-or-export-text-txt-or-csv-files-5250ac4c-663c-47ce-937b-339e391393ba “导入文本文件通过连接到它“部分。
  3. 在“数据”选项卡的“连接”标题下,您需要更改“属性”以便每隔一段时间或打开文件时进行刷新。 如果您不会每次更改文件名,我将取消选中“在刷新时提示文件名”框。

希望这对你有用。 如果您需要更多的解释,请再次评论。