如何将嵌入的Excel文件复制到硬盘? [英] How to copy an embeded excel file to hard disk?

查看:139
本文介绍了如何将嵌入的Excel文件复制到硬盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供一个代码片段,用于将嵌入的excel文件复制到硬盘。我知道有很多可用的解决方案,但我似乎在所有代码中都出错了。最受欢迎的是:



Can someone please provide a code snippet for copying embeded excel file to hard disk. I know there are tons of solution out there avaiable, but I seem to get an error in all the codes. The most popular one is :

File.Writeallbytes(dest_path, resource.resourcename) 





但我似乎在resourcename中出错了。当我输入资源名称时,C#无法识别它。我哪里错了?我会在这里写出资源的名称供你参考,以便你们可以使用它:PLS模板(2).xlsm







提前感谢!!



But I seem to get an error in resourcename. When I type the resource name C# is not recognizing it. Where am I going wrong? I will write the name of the resource here for your reference so that you guys can use it: "PLS template(2).xlsm"



Thanks a ton in advance!!

推荐答案

我假设您以这种方式将excel文件添加为嵌入式资源:created .resx文件并使用添加现有文件Visual Studio菜单项添加Excel文件。我们假设Excel文件名是Sample.xlsx。如果对项目进行以下更改:1)将Excel文件复制到项目中;如果文件已经在项目的根目录下,请注意它可能重复; 2)使用不复制(到输出目录)选项将文件引用添加到项目中;它不需要作为嵌入模块的文件,3)文件将在资源文件中引用,4)将添加自动生成的* .Designer.cs文件(它之前没有添加) ),5)此文件将在您的项目中引用,5)新的静态属性将自动添加到此自动生成的文件中。



我们来看看在上述财产中,在第5项中。假设您将Excel文件命名为Sample.xlsx,它将显示为:

I assume you added an excel file as an embedded resource this way: created an .resx file and added the Excel file using the "Add Existing File" Visual Studio menu item. Let's assume the Excel file name is "Sample.xlsx". If causes the following changes to your project: 1) the Excel file is copied to your project; if the file was already under your project's root directories, be aware of possible duplicate of it; 2) the file reference is added to the project with "Do not copy" (to the output directory) option; it is not needed as the file with come as embedded in the module, 3) the file will be referenced in the resource file, 4) the auto-generated *.Designer.cs file will be added (it it wasn't added before), 5) this file will be referenced in your project, 5) a new static property will be automatically added in this auto-generated file.

Let's take a look at the property mentioned above, in the item #5. Assuming you named the Excel file "Sample.xlsx", it will read:
// DON'T WRITE THIS CODE!
// It is already auto-generated for you:
internal static byte[] Sample {
    get {
        object obj = ResourceManager.GetObject("Sample", resourceCulture);
        return ((byte[])(obj));
    }
}



所以,几乎所有东西都已经完成了。该属性实际上将从资源中读取数据。所有你需要它来保存它。这样做的一种方法可能是最简单的方法: http:// msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx [ ^ ]。



这就是全部。



-SA


这篇关于如何将嵌入的Excel文件复制到硬盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆