简体   繁体   English

使用C#和Excel Interop

[英]Working with C# and Excel Interop

I have this code: 我有以下代码:

Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
xla.Visible = false;
Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xla.ActiveSheet;
ws.Name = "Serial";
int i = 1;
foreach (DataRow comp in dsView.Tables[0].Rows)
{
    ws.Cells[i, 1] = "'" + comp[0].ToString();
    ws.Cells[i, 2] = "'" + comp[1].ToString();
    ws.Cells[i, 3] = "'" + comp[2].ToString();
    i++;
}
if (File.Exists(@"d:\DDD.xlsx"))
    File.Delete(@"d:\DDD.xlsx");
xla.Save(@"d:\DDD.xlsx");    ---->>>>  on this line i get the error

The error: 错误:

Exception from HRESULT: 0x800A03EC 来自HRESULT的异常:0x800A03EC

I am working on C# with Office 2012 我正在使用Office 2012开发C#

Your problem is that the variable xla is your excel application and not a workbook. 您的问题是变量xla是您的excel应用程序,而不是工作簿。 You want to save the workbook. 您要保存工作簿。

so 所以

xla.Save(@"d:\DDD.xlsx");    ---->>>>  on this line i get the error

should be 应该

wb.SaveAs(@"d:\DDD.xlsx", System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,System.Reflection.Missing.Value,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value,System.Reflection.Missing.Value);

When I run Excel.Application.Save() with Visual Studio 2010 and Excel 2010 interop (Office 14) targeting .NET 4, it displays a dialog to select a location to save the file. 当我以.NET 4为目标运行带有Visual Studio 2010和Excel 2010互操作(Office 14)的Excel.Application.Save()时,它会显示一个对话框,以选择保存文件的位置。 Clicking Cancel on that dialog results in a COM exception. 在该对话框上单击“取消”会导致COM异常。 But clicking Save has odd results: When I supply a valid file path and name as a parameter like Excel.Application.Save(@"C:\\blah.xlsx") , I end up with two files saved. 但是单击“保存”会产生奇怪的结果:当我提供有效的文件路径和名称作为Excel.Application.Save(@"C:\\blah.xlsx") ,最终将保存两个文件。 The first is named Sheet1.xlsx and contains what I'd expect, even though I didn't choose that name in the dialog. 第一个名为Sheet1.xlsx,包含我期望的内容,即使我没有在对话框中选择该名称也是如此。 The other is named blah.xlsx (as per my file name parameter) and won't open correctly in Excel. 另一个名为blah.xlsx(根据我的文件名参数),无法在Excel中正确打开。 If I call Excel.Application.Save() without a file name as a parameter, a valid file is saved with the name and path I selected in the dialog, but I also get a "RESUME.XLW" file as well. 如果我不使用文件名作为参数调用Excel.Application.Save() ,则会使用在对话框中选择的名称和路径来保存有效文件,但同时也会得到“ RESUME.XLW”文件。 So it seems that the Application.Save method may not be a good choice to use. 因此,似乎Application.Save方法可能不是一个很好的选择。 Instead, you can do something like this, which works as you'd expect: 相反,您可以执行以下操作,该操作可以按预期运行:

using System;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication2
{
    static class Program
    {
        static void Main()
        {
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook book = xlApp.Workbooks.Add(Excel.XlSheetType.xlWorksheet);
            Excel.Worksheet sheet = book.ActiveSheet;
            sheet.Name = "Booya";
            Excel.Range range = sheet.Cells[1, 1];
            range.Value = "This is some text";
            book.SaveAs(@"C:\blah.xlsx");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM