Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 包括可供下载的资源文件_C#_Embedded Resource - Fatal编程技术网

C# 包括可供下载的资源文件

C# 包括可供下载的资源文件,c#,embedded-resource,C#,Embedded Resource,我是c#的新手,我几乎完成了一个简单的项目。此项目需要包含一个excel文件,可使用LinkLabel 如何在编译项目时包含此文件,以及在启用LinkLabel时,它将作为用户保存文件的位置单击 我的谷歌搜索总是指向我创建一个excel,我不需要创建它,它已经可用,我只需要包含在我的资源文件中 我被困在这里 private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {

我是c#的新手,我几乎完成了一个简单的项目。此项目需要包含一个excel文件,可使用
LinkLabel

如何在编译项目时包含此文件,以及在启用
LinkLabel
时,它将作为用户保存文件的位置单击

我的谷歌搜索总是指向我创建一个excel,我不需要创建它,它已经可用,我只需要包含在我的资源文件中

我被困在这里

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {

    }
现在它的工作与下面的代码很好,我不能回答我的问题,但由于低分

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        string filePath = null;
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            filePath = saveFileDialog1.FileName;
            File.WriteAllBytes(@filePath, Properties.Resources.importPurchases);
            MessageBox.Show("File Successfully saved.\r\n\r\n" + filePath, "Success Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            return;
        }
    }
  • 将excel文件添加到项目中(添加->现有项)
  • 右键单击添加的Excel文件,转到
    Properties
  • 生成操作
    设置为
    内容
  • Copy to Output Directory
    设置为
    Copy Always
    Copy If Newer
  • 通过这样做;Excel文件将始终在生成后复制到输出文件夹

                var saveFileDialog = new SaveFileDialog();
                saveFileDialog.DefaultExt = "xls";
                saveFileDialog.Filter = "Excel files (*.xls)|*.xls |All files (*.*)|*.*";
    
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    const string MyFileName = "myExcelFile.xls";
    
                    string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
    
                    var filePath = Path.Combine(execPath, MyFileName);
    
                    Microsoft.Office.Interop.Excel.Application app = new Application();
    
                    Microsoft.Office.Interop.Excel.Workbook book = app.Workbooks.Open(filePath);
    
                    book.SaveAs(saveFileDialog.FileName); //Save
    
                    book.Close();
                }
    

    更新:以上示例适用于Windows应用程序…

    我已编辑了您的标题。请看“”,其中的共识是“不,他们不应该”。感谢所有回答的人,它现在工作正常。