I would like to develop some add-in. This add-in should use open xml (not everything is doable using VSTO + Open XML is much faster), BUT is there any possibility to edit opened document? For example I have opened PowerPoint presentation (or Word doc/Excel spreadsheet) and I would like to replace some text using Open XML, can I read it (from that what I read, reading is possible) and update without closing that file, using Open XML of course? If yes, how to do it?
There is some feeddata() method, memory streams, but I do not know how to use it. Could anyone show some example?
Below code works for me:
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
string XML = doc.Content.WordOpenXML;
// XML Changes
doc.Content.InsertXML(XML);
var fileFullName = Globals.ThisAddIn.Application.ActiveDocument.FullName;
Globals.ThisAddIn.Application.ActiveDocument.Close(WdSaveOptions.wdSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat, true);
//edit document using OpenXml here
Globals.ThisAddIn.Application.Documents.Open(fileFullName);
This works for Word documents.
The purpose of VSTO and Open XML are different.
VSTO is basically extending office to your purpose where you run addin in the context of the Office application. And you can make use of Com interops as they are already available.
Open XML is used for automation like generating or editing docs, workbooks where you dont have office application installed. for eg in Asp.net server you dont need to install Office but use Open XML for creating, editing docs, spreadsheets or ppt.
In your case if you going for VSTO approach you dont need to use Open XML sdk. Use the available find and replace functionality in the corresponding interops.
After replace dont forget to save the doc, xls or ppt
Word
Excel
Powerpoint
Related
What I've done:
I am trying to be able to open an excel file in a windows form so that I can create a disposable pivot table. I have tried a few options that I've found on Google but I've run into a brick wall.
One option that I found was to open in wpf user control document viewer, while I can open the file I cannot edit it.
I have not figured out a way to edit the excel file.
My Question: I need to display a blank excel sheet inside a windows form, then I will need to add a pivot table to the sheet that is embedded inside the windows form where when the form closes it disregards the sheet that was open in the form. What is the best way to do this?
For all tasks related to programmatic construction or manipulation of Office documents, the Open XML SDK is the proper solution. All other APIs are either limited in scope (other 3rd-party Excel writer APIs support only a subset of the features of Excel documents) or have issues being used programmatically (Office automation is notoriously error-prone and should NEVER be used in a server environment). The Open XML SDK allows you to directly create and manipulate the data structure that is the Office document. It does not require Office to be installed. Additionally, the Open XML Productivity tool (also available via the download link below) will allow you to open an existing document and see EXACTLY the code required to construct EXACTLY that document. Very useful!
Download: https://www.microsoft.com/en-us/download/details.aspx?id=30425
What's the way to open a excel workbook and to read excel cells?
In this thread you will get details how to open and handle excel through C#.
Use Visual Studio Tools for Office (VSTO) to extend Office products with your code and here's a good tutorial to get you started in Excel programming
There is an open source .NET library called Koogra for reading Excel files, both BIFF (older Excel) and XSLX (newer Excel). Otherwise you can use Excel automation, but I would avoid that if possible.
You can also use open xml sdk to read (and write) excel files (in open xml format off course). There is an msdn article describing how to use the open xml sdk.
A big advantage is that you don't have to install excel on the server (or client) to read excel files.
I want to know what is the best practice to create a Excel 2007 Workbook using C#, with its datasource being a raw flat file or a table in database.
You can use
"Open XML SDK 2.0 for Microsoft Office"
It's more comfortable than harcore manually hacking OpenXML spec. There are .NET strongly typed wrapper classes so it's not hard to create a simple sheet. You don't need any interop and msoffice installed and it's safe for server soluitions - there are only a few dlls which you can ship in your solution.
I did mail-merge solution and it wasn't so scary.
But as always, when it's possible, I'm prefering plain csv format.
I personally like creating CSV's, which can be opened directly in Excel. It's a lot less work than trying to hack the Office Open XML specification, and you don't need COM interop to Excel (which requires a copy of Excel to work).
You can use the Office Primary Interop Assemblies to completely automate Excel 2007, and create the workbook from within C#.
This gives you the most control, as you have complete control over how you map from DB or flat file -> Excel workbook/worksheet.
I am using
http://www.leniel.net/2009/07/creating-excel-spreadsheets-xls-xlsx-c.html
for creating excel . Seems good so far.
Simple question. Does anyone know an easy way to convert SpreadsheetML (Excel 2003 XML) to the Open Document XML used for Excel 2007 (xlsx) files?
I've got a library that unfortunately doesn't read the XML format, so I need to try and find a way to read the data, that doesn't involve another library.
Any suggestions appreciated.
If you've got deep pockets Aspose.Cells can read/write both formats and should provide for really easy conversion without automation.
http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/default.aspx
Aspose.Cells for .NET
Aspose.Cells for .NET is an award-winning Spreadsheet component that allows .NET developers to embed the ability to read, write and manipulate Excel spreadsheets into their own applications without needing to rely on Microsoft Excel.
Aspose.Cells for .NET is a mature, scalable and feature rich component that offers many functions that are way beyond the simple data exporting capabilities of other vendors. With Aspose.Cells for .NET developers can export data, format spreadsheets to the most granular level, import images, import charts, create charts, apply and calculate complex formulas, stream Excel data, save in various formats and much more - all this without the need to use Microsoft Excel or Microsoft Office Automation.
Pricing starts at $899 per developer for enterprise (internal) development and goes up from there very steeply.
The file format has indeed changed significantly from SpreadsheetML to Office Open XML.
And, since now spreadsheet files are broken into multiple XML files (which are all then zipped), there's no real hope of an easy XLST solution.
The most straightforward course of action, unfortunately, is to automate Excel using a macro to open each SpreadsheetML files and do a "Save As" to the newer format. This can be done in Office 2003 with the Office 2007 file format plug-in. Perhaps this can be relegated to a batch process so the server is not directly involved?
If the data in the spreadsheets are trivial and follow a consistent format, you can write your own parser to import directly from the SpreadsheetML files.
An easy way would be to use Excel's COM Library (Excel 2007), but I think that's not the answer you are looking for.
What's your library capable of? You could use the Open XML SDK 2.0 to write the spreadsheet document based on the output of your library.
Best Regards
Try using JODConverter. JODConverter allows conversion of SpreadsheetML using the OpenOffice.org or Libreoffice engine.
IIRC the Office 2003 format works like OpenDocument format: It's a ZIP file with XML files inside, so (if you have enough time/courage) you can open it, find the XML file that contains the data and finally deal with XML.
I know, this answer is for brave developers ;)
Regards.
check this code static void XlsToXlsx
static void XlsToXlsx (string sourceFile, string destinationFile)
{
Type officeType = Type.GetTypeFromProgID("Excel.Application");
Excel.Application app = new Excel.Application();
app.DisplayAlerts = false;
// Open Excel Workbook for conversion.
Excel.Workbook excelWorkbook = app.Workbooks.Open(sourceFile);
// Save file as CSV file.
//excelWorkbook.SaveAs(destinationFile, Excel.XlFileFormat.xlCSV);
excelWorkbook.SaveAs(destinationFile, Excel.XlFileFormat.xlOpenXMLWorkbook);
// Close the Workbook.
excelWorkbook.Close();
// Quit Excel Application.
app.Quit();
}
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));
}