Exemplo n.º 1
0
        public static void ExportXmlMappedListToHDF5()
        {
            String         sFileName = "test.h5";
            ExcelReference selection = null;

            MOIE.Application ma           = null;
            MOIE.Range       mr           = null;
            MOIE.XmlMap      aXmlMap      = null;
            MOIE.ListObject  aListObject  = null;
            RecordSetMD      aRecordSetMD = null;

            //using isContinuing instead of throwing on last steps
            Boolean isContinuing = true;

            try {
                selection = (ExcelReference)XlCall.Excel(XlCall.xlfSelection);
                ma        = (ExcelDnaUtil.Application as MOIE.Application);
                mr        = ma.Evaluate(XlCall.Excel(XlCall.xlfReftext, selection, true)) as MOIE.Range;

                try {
                    aListObject = mr.ListObject;
                    aXmlMap     = aListObject.XmlMap;
                }
                catch (Exception) {
                    throw new com.WDataSci.WDS.WDSException("Error: could not pull XmlMap from selection");
                }

                aRecordSetMD = new RecordSetMD(RecordSetMDEnums.eMode.Internal)
                               .cAs(RecordSetMDEnums.eType.HDF5, RecordSetMDEnums.eSchemaType.XSD)
                ;

                aRecordSetMD.SchemaMatter.InputSchema = new XmlDocument();
                aRecordSetMD.SchemaMatter.InputSchema.LoadXml(aXmlMap.Schemas.Item[1].XML);

                MessageBoxButtons msgboxbuttons  = MessageBoxButtons.YesNoCancel;
                DialogResult      msgboxresponse = MessageBox.Show("Write HDF5 file from XmlMap'd ListObject of selection?", "Confirm", msgboxbuttons);

                isContinuing = (isContinuing && msgboxresponse == System.Windows.Forms.DialogResult.Yes);

                if (isContinuing)
                {
                    using (SaveFileDialog aSaveFileDialog = new SaveFileDialog()) {
                        aSaveFileDialog.InitialDirectory = ma.ActiveWorkbook.Path;
                        aSaveFileDialog.Filter           = "HDF5 Files (*.h5)|*.h5|All Files (*.*)|*.*";
                        aSaveFileDialog.FilterIndex      = 1;
                        aSaveFileDialog.RestoreDirectory = true;
                        aSaveFileDialog.FileName         = sFileName;
                        aSaveFileDialog.AddExtension     = true;
                        //aSaveFileDialog.CheckFileExists = true;
                        aSaveFileDialog.CheckPathExists = true;
                        aSaveFileDialog.Title           = "Export XmlMap'd ListObject to HDF5 (*.h5) File....";

                        if (aSaveFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            sFileName = aSaveFileDialog.FileName;
                            if (!sFileName.ToLower().EndsWith(".h5"))
                            {
                                sFileName += ".h5";
                            }
                        }
                        else
                        {
                            isContinuing = false;
                        }
                    }
                }

                if (isContinuing)
                {
                    aRecordSetMD
                    .cToFile(sFileName)
                    .cWithDataSetName("RecordSet")
                    .mReadMapFor(null, null, true);

                    int nColumns = aRecordSetMD.nColumns();
                    if (aListObject.ListColumns.Count != nColumns)
                    {
                        throw new com.WDataSci.WDS.WDSException("ListObject Column Count Does Not Match Schema Node List Count!");
                    }

                    aRecordSetMD.HDF5Matter.mWriteRecordSet(aRecordSetMD, aListObject);
                }
            }
            catch (com.WDataSci.WDS.WDSException e) {
                MessageBox.Show(e.getMessage());
            }
            catch (Exception e) {
                com.WDataSci.WDS.WDSException we = new com.WDataSci.WDS.WDSException("Error in ExportXmlMappedListToHDF5 to " + sFileName, e);
                MessageBox.Show(we.getMessage());
            }
            finally {
                selection   = null;
                aListObject = null;
                aXmlMap     = null;
                mr          = null;
                ma          = null;
                if (aRecordSetMD != null)
                {
                    aRecordSetMD.Dispose();
                }
                aRecordSetMD = null;
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return;
        }