简体   繁体   English

如何离线保存C#中的excel输出?

[英]How to save the excel output in C# offline?

I have created a simple and basic Hello World template with the sample coding I found in OfficeWriter. 我使用在OfficeWriter中找到的示例代码创建了一个简单且基本的Hello World模板。 However, I want to save the output in a folder inside the computer, not store online (web) 但是,我想将输出保存在计算机内部的文件夹中,而不是在线存储(网络)

The website have already stated how to save to a folder but it is not working in my case. 该网站已经说明了如何保存到文件夹,但在我的情况下不起作用。 Anyone can help on this? 有人可以帮忙吗? By the way, I used a console application to do the coding. 顺便说一句,我使用控制台应用程序进行编码。

The error mentions that I must add System.Web reference which I think it is not necessary since I am not doing a web or something. 该错误提到我必须添加System.Web参考,我认为这是没有必要的,因为我没有做网络之类的工作。

    using SoftArtisans.OfficeWriter.ExcelWriter;

    namespace ConsoleApplication1
    class Program
    {
        static void Main(string[] args)
        {
            ExcelTemplate XLT = new ExcelTemplate();
            XLT.Open(@"C:\Users\administrator\Desktop\Hello World.xlsx);
            DataBindProperties dataProps = XLT.CreateDataBindingProperties();
            string value = "Hello World";
            XLT.BindCellData(value, "DataValue", dataProps);
            XLT.Process();
            XLT.Save("Output.xlsx"); //this coding is giving me problem.
        }
    }

Note: I work for SoftArtisans, makers of OfficeWriter. 注意:我为OfficeWriter的制造商SoftArtisans工作。

Although most of our customers use OfficeWriter in .NET web applications, OfficeWriter can be used in any type of .NET application. 尽管我们大多数客户在.NET Web应用程序中使用OfficeWriter,但OfficeWriter可以在任何类型的.NET应用程序中使用。

All OfficeWriter objects ( ExcelTemplate , ExcelApplication , WordTemplate , and WordApplication ) have four output options: 所有OfficeWriter对象( ExcelTemplateExcelApplicationWordTemplateWordApplication )都有四个输出选项:

  1. Save directly to disk 直接保存到磁盘
  2. Save to System.IO Stream 保存到System.IO流
  3. Stream the generate file to the client as an attachment 将生成的文件作为附件流式传输到客户端
  4. Stream the generated file to the client to be viewed in the browser. 将生成的文件流传输到客户端以在浏览器中查看。 This only works for Internet Explorer and if viewing Office files in IE is enabled. 这仅适用于Internet Explorer,并且已启用在IE中查看Office文件的功能。

The Save method has a dependency on System.Web due to the Save() overloads that use the HttpResponse object. 由于使用HttpResponse对象的Save()重载,因此Save方法依赖于System.Web。 I know customers have run into trouble with the dependency if they were using the .NET 4 client profile because a reference to System.Web is not included automatically. 我知道客户在使用.NET 4客户端配置文件时会遇到依赖关系方面的麻烦,因为不会自动包含对System.Web的引用。 I believe the same is also true for projects like console or forms applications. 我相信对于控制台或表单应用程序等项目也是如此。

To save a file to a particular folder on disk, you will need to provide the full file path to the location on disk. 要将文件保存到磁盘上的特定文件夹,您将需要提供磁盘上位置的完整文件路径。 For example "C:\\Reports\\SampleReport.xlsx". 例如“ C:\\ Reports \\ SampleReport.xlsx”。 You can use .NET code to help resolve the full file path before passing that value to OfficeWriter. 在将该值传递给OfficeWriter之前,可以使用.NET代码来帮助解析完整的文件路径。

Here are a couple posts I found that discuss how to get the full file path from a .NET console application: 这是我发现的几篇文章,它们讨论了如何从.NET控制台应用程序获取完整的文件路径:

According to the ExcelTemplate.Save() documentation , the file is being saved to the server, even though you do not think it is. 根据ExcelTemplate.Save()文档 ,即使您认为不是,该文件仍被保存到服务器。 Since it is trying to save to the web server, System.Web is needed to resolve the physical path on the server. 由于它试图保存到Web服务器,因此需要System.Web来解析服务器上的物理路径。

You are using the wrong tool then. 您使用的是错误的工具。 Directly from the documentation of Excel Writer 7... 直接从Excel Writer 7的文档中...

SoftArtisans ExcelWriter is a high-performance pure .NET solution that generates native Microsoft Excel spreadsheets on a Web server. SoftArtisans ExcelWriter是一种高性能的纯.NET解决方案,可在Web服务器上生成本机Microsoft Excel电子表格。 A few simple lines of code generate editable presentation-quality spreadsheets that can be saved on the server or viewed instantly by thousands of concurrent users. 几行简单的代码即可生成可编辑的演示质量电子表格,这些电子表格可以保存在服务器上,也可以由成千上万的并发用户立即查看。

http://wiki.softartisans.com/pages/viewpage.action?pageId=3114609 http://wiki.softartisans.com/pages/viewpage.action?pageId=3114609

Look into using Visual Studio Tools for Office or some other library to satisfy your local Excel needs. 考虑使用Visual Studio Tools for Office或其他一些库来满足本地Excel的需求。

You should try adding the System.Web reference to see if that fixes your problem. 您应该尝试添加System.Web引用以查看是否可以解决您的问题。 The System.Web assembly is already installed with the full .NET framework, so it doesn't change anything if you reference it or not. System.Web程序集已经与完整的.NET框架一起安装,因此,无论您是否引用它,它都不会更改任何内容。 Maybe it will detect that you're not in a web application and save it anyway, but it needs the reference to do so. 也许它会检测到您不在Web应用程序中并保存它,但是这样做需要参考。

Alternatively, use the open source EPPlus project to create Excel spreadsheets and System.Web will not be required - http://epplus.codeplex.com/ 或者,使用开源EPPlus项目创建Excel电子表格,并且不需要System.Web- http: //epplus.codeplex.com/

Okay, I found the answer to that problem, I just have to add the web reference to the project and it worked like magic. 好的,我找到了该问题的答案,我只需要向该项目添加Web引用即可,它就像魔术一样工作。 Once again I thank you all for the help rendered. 我再次感谢大家提供的帮助。

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

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