简体   繁体   English

Excel Interop-提示保存excel不出现-C#.net

[英]Excel Interop - prompt to save excel not appearing - c# .net

I am using Excel Interop to export a dataset to excel file. 我正在使用Excel Interop将数据集导出到excel文件。 The below code exports and opens the excel file directly. 下面的代码直接导出并打开excel文件。 I need a prompt to ask the user to save, open and cancel. 我需要提示要求用户保存,打开和取消。 Please help.. 请帮忙..

 public static void CreateWorkbook(DataSet ds, String path)
{
    int rowindex = 0;
    int columnindex = 0;

    Microsoft.Office.Interop.Excel.Application wapp = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Worksheet wsheet;
    Microsoft.Office.Interop.Excel.Workbook wbook;

    wapp.Visible = false;

    wbook = wapp.Workbooks.Add(true);
    wsheet = (Worksheet)wbook.ActiveSheet;

    try
    {
        for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
        {
            wsheet.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;

        }

        foreach (DataRow row in ds.Tables[0].Rows)
        {
            rowindex++;
            columnindex = 0;
            foreach (DataColumn col in ds.Tables[0].Columns)
            {
                columnindex++;
                wsheet.Cells[rowindex + 1, columnindex] = row[col.ColumnName];
            }
        }
    }
    catch (Exception ex)
    {
        String err = ex.Message;
    }
    wapp.UserControl = true;
    wapp.Visible = true;
}

Kind of an old-ish question at this point, but if you are still looking for help: 此时有点旧的问题,但是如果您仍在寻求帮助,请执行以下操作:

Simply said, you need a SaveFileDialog or an OpenFileDialog, which I'd imagine you can see which each does. 简而言之,您需要一个SaveFileDialog或OpenFileDialog,我想您可以看到每个文件都可以。

It's simple enough to make as well: 制作起来也很简单:

SaveFileDialog saveDialog = new SaveFileDialog();

See this MSDN article for reference. 请参阅此MSDN文章以供参考。

OpenFileDialogs work the same way, but keep in mind that these dialogs don't actually do anything on their own. OpenFileDialogs的工作方式相同,但请记住,这些对话框实际上并不自行执行任何操作。 All that these dialogs do is define file names and locations when the user hits OK (and they are stored as properties). 这些对话框所要做的就是在用户单击“确定”时定义文件名和位置(并将它们存储为属性)。

There's two ways to work with these: Using the Event FileOk (Raised when OK is clicked) or using the method showDialog(). 有两种方法可以使用它们:使用事件FileOk(单击“确定”时引发)或使用方法showDialog()。 It's a lot easier to work with when you use the method. 使用该方法时,使用起来要容易得多。

As an example, if you are opening a file: 例如,如果要打开文件:

If saveDialog.ShowDialog() == DialogResult.OK Then
   /// Do stuff, like save the Excel sheet to the path in the dialog.
End If

Note what this does in itself, though: it only sets a file name as typed out in the dialog. 请注意,它本身的作用是: 它只设置对话框中键入的文件名。 It does not save anything. 它不会保存任何内容。 You'll still need to call the save/open methods with Excel interop methods. 您仍然需要使用Excel互操作方法来调用save / open方法。

Keep in mind you can preset certain things like initial directory or file types above that If statement using the respective properties listed in that article. 请记住,您可以使用该文章中列出的相应属性在If语句上方预设某些内容,例如初始目录或文件类型。

In your case, I see 'path' as an argument to the method. 在您的情况下,我将“ path”视为该方法的参数。 What you could do is before this method call (use an OpenFileDialog, of course), add in the above If/Then block to get the 'open' path, and pass in the dialog.fileName property as the path to your method. 您可以做的是在此方法调用之前(当然,使用OpenFileDialog),在上面的If / Then块中添加以获取“打开”路径,然后将dialog.fileName属性传递为方法的路径。

Putting that all together, for opening: 放在一起,打开:

OpenFileDialog openDialog = new OpenFileDialog();

If openDialog.ShowDialog() == DialogResult.OK Then
       CreateWorkbook(ds:= dataset, openDialog.fileName)
End If

Again, remember this doesn't actually open it . 再次提醒您, 实际上并没有打开它 Somewhere in your method, after you define the workbook, you can open it with wbook.Open(path). 在方法中的某个位置,定义工作簿后,可以使用wbook.Open(path)打开它。

And for closing, I'd avoid giving the user control or making the app visible outside of debugging. 对于关闭,我将避免赋予用户控制权或使应用程序在调试之外可见。 That's asking for issues left and right. 那是在问左右问题。

Instead, insert this in that location in your method: 而是将其插入您的方法中的该位置:

SaveFileDialog saveDialog = new SaveFileDialog();

If saveDialog.ShowDialog() == DialogResult.OK Then
     wbook.Close(True, saveDialog.fileName, )
Else
     wbook.Close(False, , )
End If

wapp.Quit()

Once you do that, manually open the Excel for editing if its still necessary. 完成此操作后,如果仍然需要,请手动打开Excel进行编辑。 Depending on what you need, you might need to tinker around with that idea, but I think this is generally what you need. 根据您的需要,您可能需要修改该想法,但是我认为这通常是您所需要的。

EDIT: For understanding my code with the wbook.Close, see the Workbook.Close Method in the MSDN: https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.close.aspx 编辑:为了解wbook.Close的代码,请参阅MSDN中的Workbook.Close方法: https ://msdn.microsoft.com/zh-cn/library/microsoft.office.tools.excel.workbook.close 。 aspx

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

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