我正在创建一个将数据写入现有 Excel 文件的代码。仅当文件关闭时它才会读/写文件。如果我尝试在桌面上打开该文件时写入该文件,它将不会更改或保存该文档。当代码运行之前或运行期间打开 Excel 文件时,我也无法关闭工作簿(使用 .close())或退出应用程序(使用 .quit())。
有没有一种方法可以在桌面上打开 Excel 文件时对其进行写入并实际显示更改?或者我至少可以关闭一个已经打开的文件读/写,保存并使用 C# 代码再次打开它吗?这是我正在使用的代码;它有点无组织,但如果你运行它,你可以看到我本质上想做的事情。请注意,您必须更改要保存文件的通用地址才能使代码正常工作(通用地址保存为名为 excelsource 的字符串变量)。该代码将首先创建一个以今天的月份和日期 (MM_YY) 命名的文件。每次初始化代码时它都会继续写入。如果您在新创建的文件打开时尝试写入文件,则不会对文件应用任何更改(仅在文件关闭时写入 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 中打开文件时,它会阻止其他应用程序的写入访问,以维护打开的文件内的数据完整性。这意味着您应该具有只读访问权限。
此外,从外部应用程序强制打开和关闭任何应用程序很少是更新文件的最佳方法。如果您想要修改当前打开的文件中的数据,您应该考虑学习编写 Excel 宏。
Edit 1: After clarification of question:
您无法添加到打开的 Excel 文件。但是,如果我正确理解您的问题,我认为这会解决您的问题(我尝试过并且它对我有用):
- 写入以逗号分隔的 .csv 文件,您可以控制 C# 应用程序的读/写访问权限。
- 在 Excel 电子表格“来自文本”中添加引用您的 .csv 文件的“外部数据源”。来源:https://support.office.com/en-us/article/Import-or-export-text-txt-or-csv-files-5250ac4c-663c-47ce-937b-339e391393ba在“通过连接来导入文本文件”部分下。
- 在“数据”选项卡的“连接”标题下,您希望将“属性”更改为经常刷新或在打开文件时刷新。如果您不想每次都更改文件名,我会取消选中“刷新时提示输入文件名”框。
希望这对您有用。如果您需要更多说明,请再次发表评论。
关于c# - 当现有的 Excel 文件在桌面上**打开**时,是否可以写入该文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42491403/