Tutorial (Excel To XML to VisualStudio 2008 Express)

Transcription

Tutorial (Excel To XML to VisualStudio 2008 Express)
Jákup Wenningstedt Hansen
Side 1 af 12
18-12-2009
Tutorial (Excel To XML to VisualStudio 2008 Express)
Installation for Excel 2003.............................................................................................2
Run the program ........................................................................................................2
Installing and Setting Up the Excel XML Tools Add-in ...........................................2
This is how it looked inn a Danish set up ..................................................................3
Using the Excel XML Tools Add-in..........................................................................3
Convert a Range to an XML List...............................................................................3
If you have Office 2007 .............................................................................................3
Create an XML file from a Excel file. ...........................................................................4
Importing the Created XML file into Visual Studio......................................................5
The Code for working with the XML file......................................................................6
An Example of the power of XML ................................................................................6
OpenOffice Calc instead of Excel................................................................................10
Free/Open code for generating XML from OpenOffice.org Calc spreadsheets ......10
Background ..........................................................................................................10
How it works........................................................................................................11
How to install / use ..............................................................................................11
FAQ......................................................................................................................11
Contact .................................................................................................................12
Jákup Wenningstedt Hansen
Side 2 af 12
18-12-2009
Installation for Excel 2003
For Excel 2003 we need to download a software for creating the xml files. It can be
downloaded at:
http://www.microsoft.com/downloads/thankyou.aspx?familyId=72852247-6afd-425c83b1-1f94e4ac2775&displayLang=en
It looks like this.
Run the program
You must run the program after the downloading.
Installing and Setting Up the Excel XML Tools Add-in
To install the add-in, complete the following steps:
1.
Start Excel.
2.
On the Tools menu, click Add-Ins.
3.
Click the Browse button and navigate to the XmlTools.xla file.
4.
Select it and then click OK.
5.
Click Yes to any file copy or overwrite prompts.
6.
Verify the XmlTools box is selected.
1.
7.
Note
To remove this add-in, repeat this procedure, but clear the XMLTools box.
Click OK to use the add-in. A top-level menu entry, XML Tools should be present.
Jákup Wenningstedt Hansen
Side 3 af 12
18-12-2009
This is how it looked inn a Danish set up
Using the Excel XML Tools Add-in
The Excel XML Tools Add-in enables you to automate several tasks that normally you need to perform
manually. For example, refresh all of the XML maps in a worksheet or display the XML properties of
the active cell. The following sections describe each of these tasks and more.
Convert a Range to an XML List
To convert a range to an XML list, complete the following steps:
1.
On the XML Tools menu, click Convert a Range to an XML List. The Convert range to
XML list dialog box is displayed.
2.
In the text box, click the button on the right. A floating text box is displayed.
3.
Click the upper-left corner of the range and drag the mouse to the lower-right of the range.
Notice that the range address is displayed in the text box. Click the icon on the right side of
the text box.
4.
Select whether the first row of the selected range contains column header labels or actual
data.
5.
If desired, you can change the names of the root and row elements under Advanced
Options. This is useful for custom XML file creation for use with other apps that expect
certain root and row names. It can also be useful in creating larger XML files created from
several exports where there may be some nesting.
If you have Office 2007
It is much the same as 2003, but here is the link to 2007…
http://office.microsoft.com/en-us/excel/HA102635091033.aspx
Jákup Wenningstedt Hansen
Side 4 af 12
18-12-2009
Create an XML file from a Excel file.
I went on the internet and found a “Medlemsliste” for a Tabel Tennis club. It is not
important which file you use, this is just an example.
Then you start the “XML Tools” and then “Converting a range to an XML List” and
then you follow the picture following her…
Jákup Wenningstedt Hansen
Side 5 af 12
18-12-2009
Importing the Created XML file into Visual Studio.
It is relative simple to import the file, just do as we normally do when we import or
add something extra to the project.
Jákup Wenningstedt Hansen
Side 6 af 12
18-12-2009
The Code for working with the XML file.
First: On the form you must at least drag a dataset and a dataGridView and then two
buttons.
Her I run the program (Ctrl + F5) and then I have pushed the “DataGrid” button and
the XML data is inserted into at table. The fantastic thing here is that, I can add data
or edit data, and then just push “Save”. And everything is saved into the
“VibyMedlemsliste.xml” file.
An Example of the power of XML
Let me show you an example that I thing shows the true power of XML. It is not
complicated code, but I points to the basic idea of xml, and why it is so suitable for
data transportation.
To start, I made a copy of the VibyMedlemsliste.xml and called it
VibyMedlemslisteShor.xml. And I have shortened it to two members and only
included “Fornavn” and “Efternavn”, her is the xml file
<?xml version="1.0" standalone="yes"?>
<Medlemsliste2005>
<Medlem>
<Efternavn>August</Efternavn>
<Fornavn>Patrick</Fornavn>
</Medlem>
<Medlem>
<Efternavn>Baadstorp</Efternavn>
<Fornavn>Mikkel</Fornavn>
</Medlem>
</Medlemsliste2005>
Jákup Wenningstedt Hansen
Side 7 af 12
18-12-2009
Now I created a new button and gave it this code.
private void button5_Click(object sender, EventArgs e)
{
dataSet1.ReadXml("C:/Documents and Settings/Student/My Documents/Visual Studio
2008/Projects/FormXML/FormXML/VibyMedlemslisteShort.xml");
dataGridView1.DataSource = dataSet1.Tables["Medlem"];
for (int i = 0; i < dataGridView1.Columns.Count ; i++)
{
if (dataGridView1.Columns[i].Name.Equals("Fornavn"))
{
dataGridView1.Columns[i].Visible = true;
}
else
{
dataGridView1.Columns[i].Visible = false;
}
}
}
Let me show the output…
This output should not be that surprising because it was only the “Fornavn” I wanted
to see.
So let’s get on with it.
Open the XML file again, and insert a field named “alder” and give the two persons
some age. In my case the XML file looks like this now…
<?xml version="1.0" standalone="yes"?>
<Medlemsliste2005>
<Medlem>
<Efternavn>August</Efternavn>
<Fornavn>Patrick</Fornavn>
<Alder>23</Alder>
</Medlem>
<Medlem>
<Efternavn>Baadstorp</Efternavn>
<Fornavn>Mikkel</Fornavn>
<Alder>74</Alder>
</Medlem>
</Medlemsliste2005>
Jákup Wenningstedt Hansen
Side 8 af 12
18-12-2009
Now, run the program again, and you will see, be exactly the same output.
This may not be a surprise to you, but if you think about it, the program could have
crashed, because the data is not the same. And this is exactly the power of XML, that
we can EXTEND the data (in this example we extended with the “Alder”), and the
program just ignores the extra data that is available.
We can also save to the xml file just as before. So if I create a new “Fornavn” called
“Finn” it looks like this…
This is extremely powerful. The program just creates a new “Medlem” and it gets
only one attribute namely the “Fornavn”. I can easily add an attribute to this “Finn”
person. Just open the program with the DataGrid button, and everything is visible and
editable. Let me show you…
Jákup Wenningstedt Hansen
Side 9 af 12
18-12-2009
You se, that the XML file has got a “Efternavn” with the value “Finsen”. And there is
no “Alder” jet, because I have not given “Alder” a value, and so it is just excluded. I
expect this is also for saving space, because the XML files can become large.
This was the XML with Visual Studio tutorial. I hope you have learned some basic
stuff about XML, VS and Excel.
Thanks:
www.jakupwhansen.dk
Jákup Wenningstedt Hansen
Side 10 af 12
18-12-2009
OpenOffice Calc instead of Excel
It is also possible to use OpenOffice Calc which is the same as Office Excel only free.
I have tried it and it works fine.
Here is the link: http://digitalimprint.com/misc/oooexport/#install
I have copied everything from that page into this document, and here it comes….
Free/Open code for generating XML from OpenOffice.org Calc
spreadsheets
Background
XML is used a lot by developers, but not so much by your average person. So, quite
often you'll find yourself needing to get data that was sent to you by others into an
XML format. When it comes to any type of tabular data or repeating record, it is
almost certain that the data will come to you in the form of a spreadsheet.
OpenOffice.org Calc has built-in capabilities to allow you to transform your
spreadsheets (or any other OO.o document, for that matter) by writing custom XSLT
filters. While this is powerful, it is also time consuming for many tasks where you just
want to get a basic export of the data. Writing a custom exporter is fine when you
are targeting a specific (and fixed) output format, but it is overkill when you simply
need to get arbitrary data into a well-formed XML document for further use.
The Simple XML Exporter for OpenOffice.org Calc uses the built-in XML
transformation capabilities of the application to provide a general XML exporter to
allow a quick and easy way to get your data in XML format. This data can then be
further manipulated in any text editor to allow you to provide any tweaks necessary
to get it into your desired output format.
Jákup Wenningstedt Hansen
Side 11 af 12
18-12-2009
How it works
The exporter installs into OpenOffice.org (version 3 and above) and appears as an
option in the File>Export dialog box from within the Calc application. The exporter
will create an output file with the contents of your workbook - including all sheets
that have content. Each sheet is included in the exported file within <ooo_sheet>
</ooo_sheet> tags. The name of the sheet is included as an attribute to the
<ooo_sheet> - to help you keep track of what sheet generated which section of
output.
Each row on the sheet (after the first) is exported as a data row in your XML file,
using the values from the first row in the column as the tag name. Since XML tag
names have more restrictions than cell values can have - the exporter will attempt to
use your tag name when it can, and will try to generate a valid name when it cannot.
Quite often, the first row of a sheet already contains column names, so this is an
easy way to set tag names for your output XML.
How to install / use
To install, make sure you are running version 3 (or above) of OpenOffice - as the
exporter uses some XSLT features not available in version 2 of OpenOffice.
1. Download the Simple XML Exporter JAR file
(MD5: b6a22d870dd334b8a2b84b965d653fc3) and remember where on your system
you save the file.
[If prompted, make sure to SAVE the file and not RUN/OPEN it. You may need to
right-click the link and choose to save the file to prevent Java (if installed on your
system) from attempting to run the file.]
2. Install the JAR file from within OpenOffice by going to the Tools menu and choosing
XML Filter Settings...
This will bring up the XML Filter Settings dialog box
3. Click the 'Open Package' button
4. Select the JAR file from the location on your system to which you saved the file.
5. Close the XML Filter Settings dialog.
To use the exporter, simply go to the File menu and choose the Export option.
The exporter will only be visible from within Calc since it works specifically on
spreadsheet data.
Choose 'XML File' from the 'File Format' drop-down box, and enter a filename and
location to save your file.
That's it.
FAQ
For the most part, things are pretty straight-forward and (hopefully) easy to use.
There are a couple small items that need to be noted - mostly due to the way in
which the XSL transformation interface accesses the "internal" structure of the
document, which can cause a few minor irregularities.
Can I limit the export to selected cells?
No. The 'Selection' checkbox in the default export dialog has no effect on the
exported data when using this filter. The XML file will be generated for all worksheets
and all active columns/rows in the sheet.
How do I know what names are valid for tag/header values?
You can name your header rows anything you wish, however the names may be
modified when generating the XML in order to ensure a well-formed XML document.
Jákup Wenningstedt Hansen
Side 12 af 12
18-12-2009
The values pulled from the first row will be modified in this way when converting
them into tag names:
 leading and trailing space will be removed
 any non-alphanumeric characters (including space) will be converted to underscores
 if the name does not begin with an alphabetic character, the name will be prepended
with 'COL_NUM_' followed by the header value (where NUM is the number of the
column)
 if the header cell is blank, an auto-generated tag name of 'COL_NUM' will be used
(where NUM is the number of the column)
Why do I sometimes get empty rows/sheets in the output XML?
This seems to sometimes happen in spreadsheets where there was once data, but it
is no longer there. Occasionally, the application will 'hold onto' the empty rows in
memory, and they get processed (like all others) by the exporter.
Why does the row 'num' attribute sometimes not match the actual row number?
If you have empty rows in your sheet, they may be completely skipped in the export,
causing the numbering to be off on subsequent rows.
What if I want to modify or improve on this, where is the source code?
The JAR file is simply a ZIP file with some specific files that tell OO.o how to handle
the contents. You can either open the JAR with a Zip tool, or edit the XML filter
properties (after installing) to locate the XSLT file that contains the export
transformation code.
If you do have improvements, I encourage you to send them to me so that they can
(possibly) be incorporated here and others can benefit from the improvements as
well.
Is there any way to use a different tag name for the row (instead of 'ooo_row')?
You can modify the values used by editing the XSLT file that gets unpacked on your
system after installing the exporter. The ooo_row value was used to be easy to
find/replace later in a text editor. It is unlikely the exported XML is exactly what you
will end up needing and you'll likely want to do a replace on the row (and sheet)
tagnames in a text editor later.
Will this work in OpenOffice.org 2.4, or other office suites based on the
OpenOffice.org codebase?
The exporter uses some XSLT features not available in OpenOffice.org 2.x - so the
exporter will not function. Other office suites that are based on OpenOffice code may
or may not work, depending on what OpenOffice.org and XSLT features they
implement. Additionally, they would need to use the ODS file format or the internal
document structure would not be formatted in a way to transform properly.
Is there any kind of warranty in case this messes up my data?
Since this is an export filter, your original data should remain untouched, however it
is a script running in the OpenOffice.org environment and is susceptible to any bugs
within the application itself. The export transformation code is available inside the
package and should be reviewed if you are concerned about its operation. (The long
way of saying 'no')
Contact
Questions, comments, or any other feedback?
Email me at:
oooexport(at)digitalimprint(dot)com