gpt4 book ai didi

c# - 使用 Microsoft.Office.Interop 通过 C# 保存创建的文件

转载 作者:行者123 更新时间:2023-11-30 18:09:07 24 4
gpt4 key购买 nike

我有这段代码可以创建 excel 文件和工作表,然后插入相同的值。

我面临的问题是我无法保存名称为 10 colse 的文件。

我使用了另存为但没有工作:

wb.SaveAs(@"C:\mymytest.xlsx", missing, missing, missing, missing,
missing, XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);

这行代码会给我这个错误:

Microsoft Office Excel 无法访问文件“C:\A3195000”。有几个可能的原因:

• 文件名或路径不存在。• 该文件正被另一个程序使用。• 您尝试保存的工作簿与当前打开的工作簿同名。

请指教解决这个问题。

这是我的代码:

private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

if (xlApp == null)
{
MessageBox.Show("EXCEL could not be started. Check that your office installation and project references are correct.");
return;
}
xlApp.Visible = true;

Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];

if (ws == null)
{
MessageBox.Show("Worksheet could not be created. Check that your office installation and project references are correct.");
}

// Select the Excel cells, in the range c1 to c7 in the worksheet.
Range aRange = ws.get_Range("C1", "C7");

if (aRange == null)
{
MessageBox.Show("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");
}

// Fill the cells in the C1 to C7 range of the worksheet with the number 6.
Object[] args = new Object[1];
args[0] = 6;
aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args);

// Change the cells in the C1 to C7 range of the worksheet to the number 8.
aRange.Value2 = 8;
// object missing = Type.Missing;
// wb.SaveAs(@"C:\mymytest.xlsx", missing, missing, missing, missing,
//missing, XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, //missing);

}

最佳答案

你真的有权限将文件保存到C的根目录吗?我认为默认情况下您在 Windows 7 中没有它。

您可以尝试手动将 Excel 文件保存到 C:。

关于c# - 使用 Microsoft.Office.Interop 通过 C# 保存创建的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2663212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com