I have created a simple and basic Hello World template with the sample coding I found in OfficeWriter. 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.
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.
Although most of our customers use OfficeWriter in .NET web applications, OfficeWriter can be used in any type of .NET application.
All OfficeWriter objects (ExcelTemplate, ExcelApplication, WordTemplate, and WordApplication) have four output options:
Save directly to disk
Save to System.IO Stream
Stream the generate file to the client as an attachment
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.
The Save method has a dependency on System.Web due to the Save() overloads that use the HttpResponse object. 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. 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". You can use .NET code to help resolve the full file path before passing that value to OfficeWriter.
Here are a couple posts I found that discuss how to get the full file path from a .NET console application:
How can I get the application's path in .NET in a console app?
How to get a path from a directory in a C# console application?
According to the ExcelTemplate.Save() documentation, the file is being saved to the server, even though you do not think it is. Since it is trying to save to the web server, System.Web is needed to resolve the physical path on the server.
You are using the wrong tool then. Directly from the documentation of Excel Writer 7...
SoftArtisans ExcelWriter is a high-performance pure .NET solution that
generates native Microsoft Excel spreadsheets on a Web server. 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
Look into using Visual Studio Tools for Office or some other library to satisfy your local Excel needs.
You should try adding the System.Web reference to see if that fixes your problem. The System.Web assembly is already installed with the full .NET framework, so it doesn't change anything if you reference it or not. Maybe it will detect that you're not in a web application and save it anyway, but it needs the reference to do so.
Alternatively, use the open source EPPlus project to create Excel spreadsheets and System.Web will not be required - 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. Once again I thank you all for the help rendered.
Related
So i Have this c# application that contains a button allowing a file copy from a network share folder.i use a vbscript to copy the file, this script takes the source and destination path.now i want to create a setup to install my application on any PC connected to the LAN. the thing is the path will eventually change so i'm not sure if it will work.
I never made a setup before and i'm wondering if there is a way to customise the setup to allow the installer to make the changes.otherwise any solution will be very helpfull. thank you
I don't normally like giving answers that are mostly links, but it is too much to post here. Here is a quick summary:
There are quite a few things that can do what you are asking.
The one I like is called Squirrel. I recently had to learn how to use it while deploying an application for my company.
The steps in a nutshell(see what I did there? :)
Build your application (optionally add the update checker code
first - see links for details)
Package your application into a .nuget file using Nuget Package Explorer(details in links below)
Run the squirrel --releasify on your nuget
It will output the setup files that you are looking for in the Releases directory.
More information (that you will likely need):
Github - Squirrel.Windows
Youtube - Video tutorial that I found helpful
Github - Squirrel Getting started guide
As for your vbscript, I would do the file copy inside C#. You are very likely to run into permissions issues when using vbs. In any case, why add the complexity of 2 different languages when C# can do a file copy easily.
Something like this during your application's startup.
if (File.Exists(localFileName) == false) // check to see if the file is needed
{
File.Copy(sourceFileOnLan, destinationFile); // get the file
}
If you are really set on using a vbs file, you can launch it using Process.Start() and let Windows execute it.
Also, you can store the paths in your app.config file, and update them if/when they change.
I am currently trying to add a VB file inside a C# project, but I am only able to add C# files. Is there a way for me to be able to add more language templates like what is shown below?
This picture below is an example of what I have been able to do in an old project which is to be able to add both VB and C# files into one project.
Update: I am redeveloping an ASP.NET 2.0 site to the newest version with the new bootstrap framework. I didn't realize the old project was using mostly VB until I started the redevelopment in C#. I noticed that the old project is also using some C#. I am trying to see if I can have those two languages inside a new ASP.NET project or not by adding VB files. If not, I'll just make a VB project and convert all of the C# code to it.
When you open the "Add new file" dialog inside a project, it is filtered by the current project type you're working on. E.g. you cannot add a *.vb file to a C# project type, without hacking it somehow.
The GUID within the *.csproj file defines the current project type. This site contains a set of known GUIDs, it is somewhat outdated since it's from 2008.
As I said in a comment, the reason you have been able to mix-in both VB and C# code within the web project, is by using CodeFile attribute, rather than the CodeBehind attribute. The latter will compile all the source files within the project into an assembly with the same name. This will be the file you are uplading to your webserver along with the .aspx files.
Sample file structure:
bin/MyApp.MyProject.dll
Index.aspx
The CodeFile variant, which compiles the source on the fly will need all the files in the same directory, or in the directory specified within the attribute, for this example it will reside within the same directory. This will also allow you to change the code at the web server, and not having to download the code to your development environment. (This is NOT recommended, as you wave goodbye to any version control and other useful tools.)
Sample file structure:
Index.aspx
Index.aspx.cs
My advice would be to decide whether or not to continue developing in VB, or switch to C#. Containing your codebase to one language is preferrable. And if you decide to refactor the whole solution, why not give ASP.NET MVC a go? :-)
Actually there used to be, not sure if it's still available, to put a VB file in a C# project. You would have needed to add a text file type but name it with a .vb extension. Then place it in a folder. Then in the app or web config, you needed to add some config to tell the runtime where to find the vb files and to use VB compiler to compile the code.
It worked but was clunky and not recommended. Again, not sure if it's available anymore.
Personally, if you have VB code, use a vb2cs converter like this one to begin converting it over. They are not perfect but it's at least a good start in most cases.
I tried to use Microsoft's Syn Framework Toolkit: http://www.microsoft.com/en-us/download/details.aspx?id=23217
And as I intend to use an Android client, I also downloaded the Toolkit for it: http://code.msdn.microsoft.com/wpapps/Sync-Framework-Toolkit-4dc10f0e
I installed the SDK, then extracted the Toolkit and built it with VS.
Following the instruction in the help file, I used SyncSvcUtilUI to create a Sync configuration file. It went through as planned.
The next step would be to use that configuration file to provision the database. After I select the aforementioned Sync configuration file, I get this message:
Running SyncSvcUtil command...
Invalid parameter passed
Parameter name: Sync
Aren't the two applications compatible with each other? What should I check? The two executables are in the same directory.
The directory that contained SyncSvcUtilUI had space in its name. After moving it to a simple directory, it worked. Great software design...
Several years before I started working at this job another developer who is no longer here wrote an application in classic ASP using HTML, vbscript and javascript. This is fine but the problem is that 2 pages were written in C# with an HTML file and a code behind file. There was no solution files for these two pages. They may have been originally created in Visual Studio but they don't exist in it now.
That is important because there is a lot of things that Visual Studio just does for you without even thinking.
My problem is that in these two C# pages I need to get them to reference a DLL. This is a simple task when using Visual Studio. You just add a reference to the project and life is good. But outside of VS nothing seems to work.
I tried putting the dll in the same folder as the pages and then I tried the following:
Using myDLL;
myDLL dll = new myDLL();
myDLL dll = myDLL();
I found some code online that said to create an internal static class and use [DLLImport()] but that didn't work either. It couldn't find the dll or the Entry Point for the dll. I am currently researching how to create an entry point, just in case this is the method to make everything work.
Outside of having to rewrite these pages in vbscript (which I don't have the time to do) I am at a loss.
Has anyone ran into this problem before? Is there something that I can put in the web.Config? Or is this just impossible and I am hosed.
BTW this is all running under the 2.0 .net framework.
If you drop the DLL you want the code to reference into the bin folder of the website, then open the web.config and locate the following section configuration -> system.web -> compilation -> assemblies.
You need to add the display name of the assembly to that list - so that the compiler will reference that assembly during it's late-bound build process.
Now you should be able to use the stuff that's in it on those pages.
If you don't the know the display name of the assembly (typically yourassembly, version=*.*.*.*, Culture=neutral, PublicKeyToken=null for culture-invariant, non-strong-named assemblies) you can open it in a tool like ILSpy (there are others, it's just become my favourite) and it tells you when you select it in it's UI:
sorry for the poor highlighting - jerky hand following far too much coffee
If all the code in that assembly is in a single namespace, also, you can also add a default using to all the .cs or .aspx code in the project by adding that namespace to configuration -> system.web -> pages -> namespaces - making it simpler to use that code in the pages.
I created a VS Solution/Project for my app. I compiled and published it to the web server. When I published it I had it copy all project files.
I ran it and it crashed because it could not find my dll.
I tried adding the lines that Andras mentioned above and it seemed like it was getting me closer but it only changed the errors I was getting.
Then I went into IIS on the web server. I expanded the folder listing under Web Site. I right clicked on the folder that contained my app and made that folder into an application folder.
After I did that everything just worked. So then I thought I would see what happened if I backed out all of the additional code I added to my C# app and the Web.Config file. It still worked. All I needed to do was to make the folder an application folder in IIS and put a Using statement in my C# app and life is wonderful again.
Thanks for all the comments and suggestion. Andras thanks for the link to ILSpy. That is a cool little tool.
Take care,
Robert
I agree with Jon, it sounds like you should try creating a new project for these files. It's always better to leave code better off than you found it. If a new project is not an option for some reason, you should indicate this in your question.
My problem today is that I'm trying to retrieve the version of a file that is located within a zip. I'm doing so in C#.
Just for context, what happens is that a 3rd party places builds of software onto a test server, and I'm manually updating the wiki with the version information.
I've thus far created the wikibot (not difficult), the difficulty appears to how I'm going to go about retrieving the version.
Any help is appreciated, thanks!
First unzip the file (using a 3rd-party tool like SharpZipLib etc., or using System.Io.Compression). Then use FileVersionInfo.FileVersion to get your file version. Voila!