How to programmatically save an OPEN Excel Document - c#

I have UI tests. When the UI tests finish, they open the result files in an Excel document (Excel 2007 is installed on the test environment) - but the problem is that the excel document is not saved anywhere on the computer. They exist only when the AutoRecover feature saves the temporary files as a .xar file, so this is useless to us.
We use a C# .NET program to launch the UI tests (and do a bunch of other things), so I'm looking to see if it's possible to save this OPENED excel document programmatically.
Is that possible?
Thanks

This may help you How to: Save Workbooks
Example
This example creates a new workbook, prompts the user for a file name, and then saves the workbook.
Set NewBook = Workbooks.Add
Do
fName = Application.GetSaveAsFilename
Loop Until fName <> False
NewBook.SaveAs Filename:=fName

Related

Check if a Excel document has a macro to set correct file extension on save

I have a plugin where I download a Excel document from the net and open it.
As the Excel document can be .xls or .xlsx and the file might contain macros I need to check if the active workbook has macros.
I find a solution but this runs into security problems...
var hasMacro = false;
var cmp = ExcelApp.ActiveWorkbook.VBProject;
foreach (VBComponent c in from VBComponent c in cmp.VBComponents where c.CodeModule.CountOfLines > 1 select c)
hasMacro = true;
But running the code runs into
Programmatic access to Visual Basic Project is not trusted
if Excel has not correct properties set in the trust center.
As I do only want to check if a Excel document has macros or not to set the correct file extension to .xlsm is there a smarter way to check for macros?
Found this SO Answer but I do not have a property HasVBProject.
Why not just save all files as .xlsm
it won't hurt if it doesn't have a macro

Loading spreadsheet template file doesn't retain form button

I am using SpreadsheetGear 2012 in my application to load, modify then save a new copy of a template workbook.
My code takes invoicing information from a database and formats it for the workbook. Then the user will print an invoice from the formatted information using a button on the workbook.
I use a template workbook with some formatting already provided to make my life easier. Included on the original template workbook is a button that runs a VBA macro in the spreadsheet. The VBA macro loads successfully but the button just disappears in the new workbook.
Some of the steps I have tried to rectify the issue: I've added a new button, changed the VBA macro code from a function to a sub, saved the template file as a macro-enabled spreadsheet (.xlsm) and saved the revised copy as a macro-enabled spreadsheet file.
Has anyone experienced this issue and do you have a solution?
If you are using the Open XML file formats (XLSX/XLSM), then this is a known limitation:
http://www.spreadsheetgear.com/support/help/spreadsheetgear.net.7.0/#SpreadsheetGear_2012_Limitations.html
In short, SpreadsheetGear 2012 does not support reading/writing Form Controls (like buttons), as well as Cell Comments, when working with the Open XML file formats. So your button is getting dropped when the file is initially read into SpreadsheetGear.
If you want to preserve these objects through SpreadsheetGear, you will need to use the XLS (FileFormat.Excel8) file format, which does support them.

c# create instance of Excel workbook from console application

I have a Visual Studio solution that contains two projects (1) a C# console application; and (2) a C# Excel Workbook.
The console application creates some data, which I would then like to pass into the workbook.
To do this I have created a method in the Excel workbook project, which receives data from the console application. However, I cannot create an instance of the Excel book. At the moment I am trying:
Excel.Application excelApplication = new Excel.Application();
Excel.Workbook excelWorkBook = excelApplication.Workbooks.Add("MyBook.xls");
I could hard code the path to the xls file, but I don't really want to do this as I would prefer to wrap everything up into a single .exe
Any ideas?
So basically what you're asking is how to wrap two files (an exe and an excel file), into a single exe.
There is a simple (and fairly clumsy) way of doing this which revolves around using Resources in C#, meaning your program will have to do something similar to the following:
Take excel file source out of Resources, and write it to a file when the program is run (temporary file)
Use the Current Directory + excel temp file name to feed into the Worksheet.Add method
Once edited, store the temporary file's data back into the Resources of your exe.
Another possibility would be, instead of using a temporary file, read a stream of bytes directly into Worksheets.Add if this is supported by the API (feel free to tell me if this is so).
EDIT: Great tutorial on using resources in C#.

How can I open an Excel file without locking it?

I have a process that builds an Excel report, then opens it for the user. The problem is if someone leaves the file open, it stays locked and nobody else can build the report until the first person exits the excel file.
Is there a way to open an Excel file without locking it, using either Process.Start or Microsoft's Interop.Excel library?
I use the Interop library to build the file each time the report is run, and save it as a static file name in a shared network folder where this application is run from
using Excel = Microsoft.Office.Interop.Excel;
...
xlsBook.SaveAs(newFileName, Excel.XlFileFormat.xlWorkbookNormal);
And open the file using Process.Start
Process.Start(newFileName);
You can try to open the file in read-only mode:
var app = new Microsoft.Office.Interop.Excel.Application();
var workbook = app.Workbooks.Open(filename, ReadOnly: true);
Or you can try to save it in shared mode:
workbook.SaveAs(filename, AccessMode: XlSaveAsAccessMode.xlShared);
If the end user only has to read the file instead of also modifying it, you could create a shadow copy and then open that copy.
Simply copy the original file to a temporary location and open it from there. The original file remains untouched and can thus be opened by others.

XML to Excel (2007) Ideas using Windows XP, and C#.Net

I have a dataset that I have modified into an xml document and then used a xsl sheet to transform into an Excel xml format in order to allow the data to be opened programatically from my application. I have run into two problems with this:
Excel is not the default Windows application to open Excel files, therefore when Program.Start("xmlfilename.xml") is run, IE is opened and the XML file is not very readable.
If you rename the file to .xlsx, you receive a warning, "This is not an excel file, do you wish to continue". This is not ideal for customers.
Ideally, I would like Windows to open the file in Excel without modifying the default OS setting for opening Excel files. Office interop is a possibility, but seems like a little overkill for this application. Does anyone have any ideas to make this work?
The solution is in .Net/C#, but I am open to other possibilities to create a clean solution.
If you insert the following into the 2nd line of your XML it directs Windows to open with Excel
<?mso-application progid="Excel.Sheet"?>
Process.Start(#"C:\Program Files\Microsoft Office\Officexx\excel.exe", "yourfile.xml");
That being said, you will still get the message box. I suppose that you could use the Interop, but I am not sure how well it will work for you.
What if you save the file as an xlsx, the extension for XML-Excel?
As Sam mentioned, the xlsx file extension is probably a good route to go. However, there is more involved than just saving the xml file as xlsx. An xlsx is actually a zip file with a bunch of xml files inside folders. I found some good sample code here which seems to give some good explanations although I haven't personally given it a try.
Apologies in advance for plugging a third party library, and I know it's not free, but I use FlexCel Studio from TMS Software. If you're looking to do more than just dump data (formatting, dynamic cross-tabs, etc) it works very well. We generate hundreds of reports a week using it.
FlexCel accepts strongly-typed datasets, it can group data according to relationships, and the generated Excel file looks so much cleaner than what you can get from a Crystal Reports excel export. I've done the crystal reports thing, and the OLE automation thing. FlexCel is a steal at $125 EU.
Hope this helps.
OpenXML in MSDN - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.openxml(v=office.11).aspx
using Excel = Microsoft.Office.Interop.Excel;
string workbookPath= #"C:\temp\Results_2013Apr02_110133_6692.xml";
this.lblResultFile.Text = string.Format(#" File:{0}",workbookPath);
if (File.Exists(workbookPath))
{
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook excelWorkbook = excelApp.Workbooks.OpenXML(workbookPath, Type.Missing, Excel.XlXmlLoadOption.xlXmlLoadPromptUser);
}
else
{
MessageBox.Show(String.Format("File:{0} does not exists", workbookPath));
}

Categories

Resources