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

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));
}

Related

Creating excel - improve performance

Good afternoon,
we have a small problem with performance of generating excel.
First, we was creating excel cell by cell - it is ... let's say unacceptable.
Second, we started insert into excel with one command - range creating and it is much faster, but still not perfect so we are searching next solutions.
Because we can load XML file from database, we tried used XSLT and from these two files create xls file. It is nice, but after open this file there is error message shown (it is because of problem or bug in registry). User has to accept this message and after excel is opened. We want to eliminate this error message. However we don't know how.
We was thinking about convert this xls file into xlsx but we are unable to do it becouse we can't install office on server (we cannot use Interop) and OpenXML libraries don't know work with normal xls file. So my question is:
Is possible to generate from XML file with using of some XLST (or something) the xlsx file?
Eventually can what files do we need to create and zip together if we want create xlsx file?
Thank you for information
You mention not being able to use the OpenXML libraries because they don't work with .xls files, but you also say "creating cell by cell", which implies that you are generating the file from scratch. Where is the xls file coming from? You mention excel opening, but then say you can't install it on the server. So, it appears to me that a user is uploading an xls file to your server, and then you are doing something with it and giving it back to them? If that is the case and you must be able to read/write an xls file without installing office, then I would suggest using ExcelLibrary, as mentioned in this post
Indeed, creating an xlsx file is much magnitudes faster with the open xml sdk.

Exporting to Excel without MS office installed for Wpf Product

I have a small wpf product which requires exporting data to excel with out excel installed on the client machine.How to achieve this in C#.After exporting, this excel can be opened by Open office. All I wanted is to save excel file to the client hard disk. Even excel is not installed he should be able to save the file,he may not be read it without excel but should be able to save. I dont want to any 3rd party or some other open xmls.
Recently I downloaded a product which is able to export to excel without excel installed and able to open it with open office.
When i checked their binaries they contain office.dll ,Microsoft.Vbe.Interop.dll and Microsoft.Office.Interop.Excel.dll's only .I want to know how they are able to manage with these dlls.
I have already written code for this but its breaking when excel is not installed.
I have read many open xml and other stuff relating to this but not satisfied.
My requirement is too simple ,just exporting datatable data to excel,no reading back the data and no fancy oparations with excel.
Please give me suggestions and links will be appreciated.
Thanks in advance
Either work out with CSV format or you may like to use EPPlus library. See similar answer here
You can use CSV, XML, or ADO
How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET
xslt transformation can also to the job. i use it to export wpf datagrid data.

C# output to Excel spreadsheet, using Excel 2000+

I'm writing a program that reads a text file, extracts information, and outputs it to a template Excel spreadsheet that already exists.
I've managed to do this on my computer using the Microsoft.Office.Interop.Excel reference and its related methods, and it works fine. I have Excel 2010. However the computers that this program will be used on mostly have either Excel 2000 or Excel 2003, and it won't work on them.
Does anyone know a way to make a program target all versions of Excel from 2000 upwards?
Cheers,
Greg
If your needs are simple and you don't have $900 for Aspose.Cells to throw around, you can do any of the following:
Use NPOI to read, inject data into, and export your template.
Create a basic HTML file with a table and just named it *.xls. You can save your template in Excel as HTML and replace bits and pieces to insert your data.
Create an XML file using Office 2002/2003 XML format, it's pretty straightforward (caveat: can't be read in Excel 2000). As above, you can save your template in XML Spreadsheet format, read it in, and do some simple stuff to inject your data.
You really need to target 2000 or under. 2010 and 2003 will open a 2000 format document whereas 2000 will not open a 2010 document. Office has a single format for 97-2000 and that's what you need to create to make everybody happy.
Interop depends on the version you have installed and I personally have dodged using interop due to its "unmanaged" nature (and it seems to love file locks).
If you want hassle free and extremely fast/powerful creation of Excel documents, you really can not do better than Aspose.Cells in my opinion.
Find it here.

Reading Excel Cells using C#

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.

Excel Conversion of SpreadsheetML to Open XML (XLSX)

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();
}

Categories

Resources