c# - 在 C# 中编辑现有的 Excel 文件。异常

标签 c# wpf excel

所以,我已经在主窗口的同一个 wpf 应用程序中创建了一个 Excel 文件。 现在,我想编辑该文件,以便我可以添加另一个工作表并在其上放置一些数据。

文件自己创建得很好,并将所有必要的数据放在上面,但是当我尝试再次调用它时,COMExcpetion 出现说它找不到文件,但文件在那里!!

我认为这不是市长,但非常感谢您的帮助!

代码如下:

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

        xlApp.Visible = true;

        Workbook wb = xlApp.Workbooks.Open(@"C:Users\Public\Documents\Report.xls");
        Worksheet ws2 = (Worksheet)wb.Worksheets[2];
        ws2.Name = "Part Function";

        object misValue = System.Reflection.Missing.Value;

        int i = 1;
        ws2.Cells[i, 1] = "Part Function Auto";
        i++;
        ws2.Cells[i, 1] = "Problem Reported in TestID#:";
        i++;
        ws2.Cells[i, 1] = "Problem is:";
        i++;

        ws2.Cells[2, 2] = testBox.Text;

        wb.Save();
        wb.Close();
      }

这是我最初创建 Excel 文件的方式。

 private void submit_Click(object sender, RoutedEventArgs e)
    {

        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

        xlApp.Visible = false;

        Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        Worksheet ws = (Worksheet)wb.Worksheets[1];
        ws.Name = "Personal Info";
        wb.Sheets.Add(After: wb.Sheets[wb.Sheets.Count]);


        object misValue = System.Reflection.Missing.Value;

        TextRange programTxt = new TextRange(program.Document.ContentStart, program.Document.ContentEnd);
        TextRange dateTxt = new TextRange(date.Document.ContentStart, date.Document.ContentEnd);
        TextRange vinTxt = new TextRange(vin.Document.ContentStart, vin.Document.ContentEnd);
        TextRange dandrTxt = new TextRange(dandr.Document.ContentStart, dandr.Document.ContentEnd);
        TextRange cdsidTxt = new TextRange(cdsid.Document.ContentStart, cdsid.Document.ContentEnd);
        TextRange milestoneTxt = new TextRange(milestone.Document.ContentStart, milestone.Document.ContentEnd);
        TextRange alertTxt = new TextRange(alert.Document.ContentStart, alert.Document.ContentEnd);

        int i = 1;
        ws.Cells[i, 1] = "Program:";
        i++;
        ws.Cells[i, 1] = "Date:";
        i++;
        ws.Cells[i, 1] = "VIN:";
        i++;
        ws.Cells[i, 1] = "D & R:";
        i++;
        ws.Cells[i, 1] = "CDS ID:";
        i++;
        ws.Cells[i, 1] = "Milestone:";
        i++;
        ws.Cells[i, 1] = "Alert:";
        i++;
        ws.Cells[i, 1] = "Region:";
        i++;
        int j = 1;
        ws.Cells[j, 2] = programTxt.Text;
        j++;
        ws.Cells[j, 2] = dateTxt.Text;
        j++;
        ws.Cells[j, 2] = vinTxt.Text;
        j++;
        ws.Cells[j, 2] = dandrTxt.Text;
        j++;
        ws.Cells[j, 2] = cdsidTxt.Text;
        j++; 
        ws.Cells[j, 2] = milestoneTxt.Text;
        j++;
        ws.Cells[j, 2] = alertTxt.Text;
        j++;

        if (EU.IsChecked == true)
        {
            ws.Cells[j, 2] = EU.Content;
            j++;
        }
        if (Canada.IsChecked == true)
        {
            ws.Cells[j, 2] = Canada.Content;
            j++;
        }
        if (USA.IsChecked == true)
        {
            ws.Cells[j, 2] = USA.Content;
            j++;
        }

        wb.SaveAs(@"C:Users\Public\Documents\Report.xls");
        wb.Close();
      }

编辑,修复

所以,我们修复了它! 我真的不知道为什么,但显然文件路径是问题所在。我将其更改为 WB = _Excel.Workbooks.Open("C:\\Users\\Public\\Documents\\Report.xls"); 现在可以了!

最佳答案

您可能仍然锁定了文件。您需要关闭工作簿和应用程序。

wb.Close(); 
ws.Close(); 
xlapp.Quit();

关于c# - 在 C# 中编辑现有的 Excel 文件。异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33788178/

相关文章:

c# - 以 double 存储十进制数?使用 updatesourcetrigger 作为 PropertyChanged 的​​属性

c# - 如何在 OData v4 中获取枚举值而不是枚举名称?

excel - 根据 Excel 单元格中的数据造句

Python:如何导入 Excel 单元格的显示(格式化)值而不是实际值

c# - 如何显示 "modern"打开文件对话框?

java - eclipse:由于 "Could not read file ... ~$somefile.xlsx"而未构建项目

c# - 该类型不能用作泛型类型或方法 'T' 中的类型参数 'BaseController<T>' 。没有隐式引用

c# - 如何在通用 Windows 应用程序中垂直对齐 StackPanel?

c# - 从自己的 AppDomain 中的单元测试启动 WPF 应用程序

wpf - 如何确定 WPF DataGrid 是否处于编辑模式?