I have a workbook with a couple of external links.
Regularly we need to update the links to point to a new excel database, so I'm making a small windows application to do it automatically.
I'm trying to use OpenXML to get this done. I dont want to use Interop, coz it takes too long to open the file, change the link and then close the workbooks again.
Any Ideas , please let me know. I'm open to using ClosedXML to this as well.
Are you using this on a limited amount of machines? e.g. One laptop for the db admin? If yes I would suggest you use DSN connections in Excel and that you update them from the DSN editor when the db change
See the following in Windows.
C:\Windows\System32\odbcad32.exe
This way any files using these DSN will have updated connections.
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 have a windows app that opens excel. The user can then decide to save it.
This uses Interop and is very slow when we have over 100 rows. I'm experimenting with EPPlus and see how to save directly to a file. But I don't want to do that, I need to open Excel from the app. So I'm thinking to write out the worksheet using EPPlus (thus saving time) and then copying the whole worksheet into Excel and open it. Does that make sense? Are there examples of this?
First off, I've used EPPlus and it's a great tool.
I'm a little confused by your question, so let me ask some clarifying questions.
Why are you launching excel from your app? Is it to edit some kind of file your program generates, or just random files the user wants to edit?
Once the user is finished editing the file, do you need to read it back into your app? If so, that can also be done with EPPlus.
Once I know more about what exactly you're trying to do, I'll be glad to help you.
--John
I need to read particular columns from an Excel sheet (say Columns A,P,Q,B) and also some particular cells (say C3 or D10). I do not need it to be displayed in a DataGrid view or anything (all examples I have seen use DataGrid).
How do I do that and write them into a new CSV file?
I have no sample code as I do not know how to proceed.
I sugest you use the ExcelReaderInterop library.
using Microsoft.Office.Interop.Excel;
Detailed example can be found here:
http://www.dotnetperls.com/excel
I had to deal with a similar issue a few weeks ago and could not find a simpler way to deal with this. The post suggest this overkill approach may be due to lots of legacy code in the library.
We have successfully used the Microsoft Access Database engine to open and read Excel files. The "2010 Access Redistributable" can also be installed on a server free of charge. What you asking for is a multi-step process:
Open a connection to the file using the Access OleDbConnection. In the connection string the "Data Source" is the file name.
Select the appropriate worksheet, which will return a DataTable object.
Grab a row from the data table or iterate over top of all of them myDataTable.Rows.
Access the column in question.
This post shows some of the process:
SSIS Excel Source Connection. What does it use to read Excel?
Hopefully this gets you pointed in the right direction.
I copied the columns that I needed to another excel sheet and saved it as CSV. Then read this csv to perform the task.
This was the easiest option as the machine I was suppose to run the program didn't have Microsoft office.
I am using FileUpload control of asp.net and uploading the excel with some data. I can't save it in some folder. I can have stream of excel sheet file or I can have Blobstream after uploading excel as a blob. Now I want to convert that excel sheets 1st sheet to datatable so how shall I do that? I am using C# .NET. I don't want to use Interop library. I can use external libraries. Oledb connection is getting failed since I don't have any physical path of excel as a data source. I tried following links:
1) http://www.codeproject.com/Articles/14639/Fast-Excel-file-reader-with-basic-functionality
2) http://exceldatareader.codeplex.com/
Please help.
Depending on the type of Excel file you can use the examples you posted or go for the OpenXML alternative (for xlsx files): http://openexcel.codeplex.com/
Now, the problem with the physical path is easy to solve. Saving the file to blob storage is great. But if you want, you can also save it in a local resource to have it locally. This will allow you to process the file using a simple OleDb connection. Once you're done with the file, you can just delete it from the local resource (it will still be available in the blob storage since you also uploaded it there).
Don't forget to have some kind of clean up mechanism in case your processing fails. You wouldn't want to end up with a disk filled with temporary files (even though it could take a while before this happens).
Read more on local resources here: http://msdn.microsoft.com/en-us/library/windowsazure/ee758708.aspx
You should use OpenXML SDK which is an officially suggested way of working with MS Office documents - http://www.microsoft.com/download/en/details.aspx?id=5124
I first created local storage as per the link:
http://msdn.microsoft.com/en-us/library/windowsazure/ee758708.aspx
suggested by Sandrino above. Thanks Sandrino for this. Then I used oledb connection and it gave me an error "Microsoft.Jet.Oledb.4.0 dll is not registered". Then I logged on to the azure server and in the IIS changed app pool configuration for 32-bit. To change app pool to 32-bit refer the following link:
http://blog.nkadesign.com/2008/windows-2008-the-microsoftjetoledb40-provider-is-not-registered-on-the-local-machine/
The approach you followed is not the correct one, as you said you logged on to azure and changed, the VM which is running on azure is not the permanent one for you. For any updates you are going to get new VM machine. you might have to find turn around for this, instead of modifying manually. You can make use of the startup tasks in your azure app. See the link below it may help you.
http://msdn.microsoft.com/en-us/library/gg456327.aspx
I've got a pretty simple web form which lets users pick reports, one specific report they want in xls with pretty formatting instead of CSV. I've whipped up the report using COM INTEROP with excel 2007. It works on my local machine and can write the file, but on the web server it fails with this error:
Microsoft Office Excel cannot open or save any more documents because there is not enough available memory or disk space.
• To make more memory available, close workbooks or programs you no longer need.
• To free disk space, delete files you no longer need from the disk you are saving to.
There is plenty of disk space on the server, so I don't think it's that - could this be a permissions issue? I've escalated ASPNET and NETWORK SERVICE accounts to have write and modify access to the folder we're saving reports to - but still no joy. Any ideas?
Do I need to invoke the com object with a specific user account, or maybe elevate the interop calls to administrator account? Any idea how to do this?
Sorry guys, sorted it.
Serge you are halfway there. I ended up running Excel as administrator:
in component services, select excel application and choose admin as the user that runs it. Seems to work okay for the week of use it'll get :)
Thanks all!
SpreadsheetGear for .NET will let you save xls and xlsx workbooks directly to a response stream with IWorkbook.SaveToStream. It is all safe managed C# code so there is no need to run anything as admin and you will not run into the problems that Excel COM Interop bring on an ASP.NET server.
You can see some simple ASP.NET Excel Reporting samples (C# and VB) using SpreadsheetGear here and download the free trial here.
Disclaimer: I own SpreadsheetGear LLC
you might want to use impersonation for your ASPNET process; here's some info and example on how to do this: killing a win process from a C# console application: How do I set permissions? more info on impersonation How to implement impersonation in an ASP.NET application