I am developing a job application. Each job generates an excel file. I will have 50 parallel job. So 50 excel files will be generated parallelly.
I am using C#3.5 and Excel 2003. The problem is I am unable to instantiate more Excel objects. I am getting COMException. So do I need to create excel processing only one at a time? Do you have any solution for this?
Pls help me.
Edit:
The excel genration doesn't need user interaction. Its a scheduled job.
I am generating Excel (xls). I need to do formatting and coloring in excel, so I can't use csv. Now I have synchronized the code, so at a time only one job processes Excel. But its taking too much time, since only one excel processing at a time.
Any kind of Excel pooling logic will help? Please direct me.
To do this you would need to start 50 instances of Excel, that would not work.
You have 3 options:
Use Open XML SDK 2.0 for Microsoft Office. This allows you to write to an excel file as if it was an xml file. No need to start Excel.
Use SharePoint Excel Services. This allows you to do server side Excel processing. No need to start Excel. The problem with this is that the SharePoint version that includes Excel Services is expensive.
Process the files one at a time, as you suggested.
ooooh i once have that kind of problem b4.
my solution is i use html excel. honestly, its kinda stupid solution, but it work pretty well and very easy to implement.
1 create html template a bit like this
<html>
<body>
name - <div>$<name>$</div>
</body>
</html>
2 read your template as string
System.IO.StreamReader stream = new System.IO.StreamReader("");
string template = stream.ReadToEnd();
template = template.Replace("$<name>$", "John");
3 then save your string as .xls
**to create a template sheets is very easy.
1 create excel template like u normally do in excel
2 in the cell u want to replace yout value type in $$name$$ *note that above i use $<'value'>$. but if we do this way i suggest u cahnge to $$name$$ becus for < and > excel gonna do HtmlEncode for us
3 u can create many sheets as u like
4 'Save as' .mht
5 then later change file.mht to .xls and string.Replace("$$name$$", "John");
Does the generation of 50 excel files require user interaction?
If not, you can create a nightly job that generates files.
If it is a plain text kind of a format(or CSV), you don't need Excel.
Also, you can use one excel instance to generate all the files, one after the other.
Related
This Question might be repeated, But I couldn't get solution regarding my problem so far. I'm new to Interop. I'm using excel file (as a database).
Here is data presentation in excel file
in my data If Card ID repeated then I need to increment '1' in Counter in the same row, similarly I need to fetch IP address of same row..
I'm using Interop Excel approach to insert data in excel file..
Kindly tell me how can I perform that update operation to that excel file through C# (WPF)
Sorry for bad English..
Thanks
I recommend using Closed XML
You write to the file directly and don't need Excel. It will need to be the latest version of an Excel file to work (The open xml standard).
Epplus.dll or npoi.dll will also read/write to excel files w/o excel.
Save the data in an XML or JSON file, then when you want to visualize them you create the excel file from these data, so you will have a very light file and easy to read and update if you wish.
I haven't done this specifically through wpf, but you can access powershell cmdlets through .net and powershell has commands for retrieving and writing Excel data.
That said, my experience has been it's very tedious and inconsistent with bugs. I would tell your client that using an Excel file as a database is impossible and certainly prone to failure in practice.
For one thing you will run into read/write restrictions if it is used by anything else.
If you don't mind to use comercial libraries, you can try to use Aspose.Cells. It has rich cells API and able to work without Excel interop API.
I am creating a series of Excel Workbooks using EPPlus v3.1.3. When I open the newly created files, if I close it without touching anything it asks me if I want to save my changes. The only thing I've noticed changes if I say "yes" is that the app.xml file is slightly altered - there is no visible difference in the workbook, and the rest of the XML files are the same. I have tried both of these approaches:
ExcelPackage p = new ExcelPackage(new FileInfo(filename));
p.Save();
as well as
ExcelPackage p = new ExcelPackage();
p.SaveAs(new FileInfo(filename));
and both have the same problem. Is there a way to have the app.xml file output in its final form?
The reason this is an issue is because we use a SAS program to QC, and when the SAS program opens the files as they have been directly output from the EPPlus program it doesn't pick up the values from cells that have formulas in them. If it is opened and "yes" is chosen for "do you want to save changes", it works fine. However, as we are creating several hundred of these, that is not practical.
Also, I am using a template. The template appears normal.
What is particularly strange is that we have been using this system for well over a year, and this is the first time we have encountered this issue.
Is there any way around this? On either the C# or SAS side?
What you are seeing is not unusual actually. Epplus does not actually generate a full XLSX file - rather it creates the raw XML content (all office 2007 document formats are xml-based) and places it in the zip file which is renamed to XLSX. Since it has not been ran through the Excel engine it has not be fully formatted to excels liking.
If it is a simple data sheet then chances are Excel does not have to do much calculation - just basic formatting. So in that case it will not prompt you to save. But even then if you do you will see it change the XLSX file a little. If you really want to see what it is doing behind the scenes rename the file to .zip and look at the xml files inside before and after.
The problem you are running in to is because it is not just a simple table export Excel has to run calculations when opened for the first time. This could be many things - formulas, autofilters, auto column/row height adustments, outlining, etc. Basically, anything that will make the sheet look a little "different" after excel gets done with it.
Unfortunately, there is no easy fix for this. Running it through excel's DOM somehow would be simplest which of course defeats the purpose of using EPPlus. The other thing you could do is see the difference between the before and after of the xml files (and there are a bunch in there you would have to look at) and mimic what excel would change/add in the "after" file version by manually editing the XML content. This is not a very pretty option depending on how extensive the changes would be. You can see how I have done it in other situations here:
Create Pivot Table Filters With EPPLUS
Adding a specific autofilter on a column
Set Gridline Color Using EPPlus?
I ran into this same issue using EPPlus (version 4.1.0, fyi) and found adding the following code before closing fixed the problem:
p.Workbook.Calculate();
p.Workbook.FullCalcOnLoad = false;
I'm aware that I can generate an HTML <table> and save it as an .xls file to read into Excel, and that works fine and all, but it only gives me one sheet.
Is there a way to generate HTML so that I can have multiple sheets in a single .xls file? I've tried to simply generate more then one <table>, but they just end up getting appended to eachother.
Short Answer: No.
Longer Answer: You cannot cause an HTML generated page to split into multiple worksheets in an Excel file. Further, the HTML you generate for even a single page could cause Excel to choke on certain machines as it does the conversion when the file is loaded. We've seen a number of low powered machines take upwards of 5 minutes to show a HTML file in excel (simple table with rows/columns, nothing fancy) depending on size.
Better Answer: Use a third party product like ClosedXML or FileHelpers to generate a proper xlsx file.
there seems to be way though I didn't try it, see http://www.c-sharpcorner.com/UploadFile/kaushikborah28/79Nick08302007171404PM/79Nick.aspx and check the Worksheets attribute
check the official documentation at http://msdn.microsoft.com/en-us/library/Aa155477%28office.10%29.aspx
After installing the Help file you can find an example of a file with 3 Worksheets in XML Reference / Excel Workbook...
You can use open source ClosedXML, а wrapper around OpenXML to conveniently generate xlsx files - i.e. Office 2007+ format Excel files.
In C# ASP.NET 3.5 web application, I need to export multiple datatables (or a dataset) to an Excel 2007 file with multiple sheets, and then provide the user with 'Open/Save' dialog box, WITHOUT saving the Excel file on the web server.
I have used Excel Interop before. I have been reading that it's not efficient and is not the best approach to achieve this and there are more ways to do it, 2 of them being: 1) Converting data in datatables to an XML string that Excel understands 2) Using OPEN XML SDK 2.0.
It looks like OPEN XML SDK 2.0 is better, please let me know. Are there any other ways to do it? I don't want to use any third-party tools.
If I use OPEN XML SDK, it creates an excel file, right? I don't want to save it on the (Windows 2003) server hard drive (I don't want to use Server.MapPath, these Excel files are dynamically created, and they are not required on the server, once client gets them). I directly want to prompt the user to open/save it. I know how to do it when the 'XML string' approach is used.
Please help.
Thank you.
Is Excel 2007 support absolutely required?
We have used NPOI with great success, and it supports all the features we want (multiple worksheets, formatting, formulas). It is also pretty stable.
The files it produces are in Excel 2003 format though, so they are binary, not OOXML.
This question has been asked before, see here for a better discussion.
You can easily stream the xml response to the user as an XML Excel file.
Any Page:
Open excel Report
Report.aspx:
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition"
, "attachment;filename=" & _fileName & ".xml");
Response.Write("<?xml version=""1.0""?>");
Response.Write(excelXML);
I too had come across similar requirement to export dataset into excel. I used this open source library. It's based on Open XML standards and doesn't use Office Interop. It met my requirement. But, my requirement was basic. So, check if it fulfills your requirement.
I have a requirement to export the results of a SQL query to excel. I am currently exporting it into 2007 format, but everything I have found will only run in a x86 enviroment. The web site where the export is to take place is running on a x64 version of IIS.
Changing IIS to run x86 is not an option. My current solution is to save the export request to a DB, and write a windows service that can run in x86 and use that service to process the request.
I have started to look at the Open XML Format SDK 2.0 as a possible better solution.
Has anyone got any experence that would lend to a native .NET solution.
FYI: My budget is $0.00 so buying a tool is a no-go.
Personally from an ASP.NET application standpoint I have found that using XML and XSLT to go into the Excel XML format is the most effective, and really trouble free way to go. There are no costs involved, and once you get the hang of it, it is really quick!
In addition to that, I'm personally strongly against any form of automation to accomplish that kind of task.
I agree with Mitchel Sellers.
Another way is create a simple html document with table, the excel will quite accept.
Example:
<html><body>
<table>
<tr><td>First column</td><td>Second column</td></tr>
<tr><td>Value of first row in first column</td><td>Value of first row in second column</td></tr>
<tr><td>Value of second row in first column</td><td>Value of secondrow in second column</td></tr>
</table>
</body></html>
Mike, do you have any example I can look at? Exporting to Excel isn't something I have had much call foor ( None really ), so any head start would be nice.
TcKs, I tried the Html route and didn't much care for the way excel complained about the file format of html not being the expected .xls(x) format. I'm exporting for Sales guys, so the less messages thay have to wade through, ( and as a result questions I have to anyswer ), the better.
The simplest way that worked well for me is to create the Excel file in the correct format with one row of data.
Then save the file in html format and use it as a template for the actual data.
There is some metadata included that will automatically start Excel when doubleclicking the generated html file.
I had to go this way because we use Excel 2002 and 2003 which have no open file format.
Have a look at: http://www.codeproject.com/KB/office/excelxmlspreadsheet.aspx
I think we modelled an inhouse solution from this.