示例#1
0
 public void ImportOdf(string odfFileName)
 {
     try
     {
         // call the exposed add-in method for importing ODF
         _application.Invoke("COMAddIns", _progIdAddin).Invoke("Object").Invoke("importOdfFile", odfFileName);
     }
     catch (Exception ex)
     {
         Trace.WriteLine(ex.ToString());
         throw;
     }
 }
示例#2
0
        /// <summary>
        /// Read an ODF file.
        /// </summary>
        public override bool importOdfFile(string odfFile)
        {
            try
            {
                bool isTemplate = Path.GetExtension(odfFile).ToUpper().Equals(".OTS");
                // create a temporary file
                string outputExtension = isTemplate ? ".xltx" : ".xlsx";
                string fileName        = this._addinLib.GetTempFileName(odfFile, outputExtension);

                ConversionOptions options = new ConversionOptions();
                options.InputFullName       = odfFile;
                options.OutputFullName      = fileName;
                options.ConversionDirection = ConversionDirection.OdsToXlsx;
                options.Generator           = this.GetGenerator();
                options.DocumentType        = isTemplate ? DocumentType.Template : DocumentType.Document;
                options.ShowProgress        = true;
                options.ShowUserInterface   = true;

                this._addinLib.OdfToOox(odfFile, fileName, options);

                // open the document
                bool confirmConversions = false;
                bool readOnly           = true;
                bool addToRecentFiles   = false;
                bool isVisible          = true;
                bool openAndRepair      = false;

                // conversion may have been cancelled and file deleted.
                if (File.Exists((string)fileName))
                {
                    // Workaround to excel bug. "Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))"
                    System.Globalization.CultureInfo ci;
                    ci = System.Threading.Thread.CurrentThread.CurrentCulture;
                    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

                    LateBindingObject doc = OpenDocument(fileName, confirmConversions, readOnly, addToRecentFiles, isVisible, openAndRepair);

                    // and activate it
                    doc.Invoke("Activate");

                    System.Threading.Thread.CurrentThread.CurrentCulture = ci;
                }
                return(true);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
                System.Windows.Forms.MessageBox.Show(this._addinLib.GetString("OdfUnexpectedError"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                return(false);
            }
        }
示例#3
0
        /// <summary>
        /// Save as ODF.
        /// </summary>
        public override bool ExportOdf()
        {
            System.Globalization.CultureInfo ci;
            ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            try
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                LateBindingObject doc = _application.Invoke("ActiveWorkbook");

                // the second test deals with blank documents
                // (which are in a 'saved' state and have no extension yet(?))
                if (!doc.GetBool("Saved") ||
                    doc.GetString("FullName").IndexOf('.') < 0 ||
                    doc.GetString("FullName").IndexOf("http://") == 0 ||
                    doc.GetString("FullName").IndexOf("https://") == 0 ||
                    doc.GetString("FullName").IndexOf("ftp://") == 0
                    )
                {
                    System.Windows.Forms.MessageBox.Show(_addinLib.GetString("OdfSaveDocumentBeforeExport"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                    return(false);
                }
                else
                {
                    System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();

                    sfd.AddExtension = true;
                    sfd.DefaultExt   = "ods";
                    sfd.Filter       = this._addinLib.GetString(this.OdfFileType) + this.ExportOdfFileFilter
                                       + this._addinLib.GetString(ALL_FILE_TYPE) + this.ExportAllFileFilter;
                    sfd.InitialDirectory = doc.GetString("Path");
                    sfd.OverwritePrompt  = true;
                    sfd.Title            = this._addinLib.GetString(EXPORT_LABEL);
                    string ext = '.' + sfd.DefaultExt;
                    sfd.FileName = doc.GetString("FullName").Substring(0, doc.GetString("FullName").LastIndexOf('.')) + ext;

                    // process the chosen documents
                    if (System.Windows.Forms.DialogResult.OK == sfd.ShowDialog())
                    {
                        // name of the file to create
                        string odfFileName = sfd.FileName;
                        // multi dotted extensions support
                        if (!odfFileName.EndsWith(ext))
                        {
                            odfFileName += ext;
                        }
                        // name of the document to convert
                        string sourceFileName = doc.GetString("FullName");
                        // name of the temporary Word12 file created if current file is not already a Word12 document
                        string tempXlsxName = null;

                        if (!(doc.GetInt32("FileFormat") == (int)XlFileFormat.xlOpenXMLWorkbook ||
                              doc.GetInt32("FileFormat") == (int)XlFileFormat.xlOpenXMLWorkbookMacroEnabled ||
                              doc.GetInt32("FileFormat") == (int)XlFileFormat.xlOpenXMLTemplate ||
                              doc.GetInt32("FileFormat") == (int)XlFileFormat.xlOpenXMLTemplateMacroEnabled ||
                              doc.GetInt32("FileFormat") == (int)XlFileFormat.xlExcel12))
                        {
                            // if file is not currently in Excel12 format
                            // 1. Create a copy
                            // 2. Open it and do a "Save as Excel12" (copy needed not to perturb current openened document
                            // 3. Convert the Excel12 copy to ODF
                            // 4. Remove both temporary created files

                            // duplicate the file to keep current file "as is"
                            string tempCopyName = Path.GetTempFileName() + Path.GetExtension((string)sourceFileName);
                            File.Copy((string)sourceFileName, (string)tempCopyName);

                            //BUG FIX #1743469
                            FileInfo fi = new FileInfo(tempCopyName);
                            if (fi.IsReadOnly)
                            {
                                fi.IsReadOnly = false;
                            }
                            //BUG FIX #1743469

                            // open the duplicated file
                            bool confirmConversions = false;
                            bool readOnly           = false;
                            bool addToRecentFiles   = false;
                            bool isVisible          = false;

                            LateBindingObject newDoc = OpenDocument(tempCopyName, confirmConversions, readOnly, addToRecentFiles, isVisible, false);

                            newDoc.Invoke("Windows").Invoke("Item", 1).SetBool("Visible", false);

                            // generate xlsx file from the duplicated file (under a temporary file)
                            tempXlsxName = this._addinLib.GetTempPath((string)sourceFileName, ".xlsx");

                            SaveDocumentAs(newDoc, tempXlsxName);

                            // close and remove the duplicated file
                            newDoc.Invoke("Close", WdSaveOptions.wdDoNotSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat, Type.Missing);

                            //BUG FIX #1743469
                            try
                            {
                                File.Delete((string)tempCopyName);
                            }
                            catch (Exception ex)
                            {
                                //If delete does not work, don't stop the rest of the process
                                //The tempFile will be deleted by the system
                                System.Diagnostics.Trace.WriteLine(ex.ToString());
                            }
                            //BUG FIX #1743469

                            // Now the file to be converted is
                            sourceFileName = tempXlsxName;
                        }

                        ConversionOptions options = new ConversionOptions();
                        options.InputFullName         = sourceFileName;
                        options.InputFullNameOriginal = doc.GetString("FullName");
                        options.OutputFullName        = odfFileName;
                        options.ConversionDirection   = ConversionDirection.XlsxToOds;
                        options.Generator             = this.GetGenerator();
                        options.DocumentType          = Path.GetExtension(odfFileName).ToUpper().Equals(".OTS") ? DocumentType.Template : DocumentType.Document;
                        options.ShowProgress          = true;
                        options.ShowUserInterface     = true;

                        this._addinLib.OoxToOdf(sourceFileName, odfFileName, options);

                        if (tempXlsxName != null && File.Exists((string)tempXlsxName))
                        {
                            this._addinLib.DeleteTempPath((string)tempXlsxName);
                        }
                    }
                    return(true);
                }
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = ci;
            }
        }
示例#4
0
        /// <summary>
        /// Save as ODF.
        /// </summary>
        public override bool ExportOdf()
        {
            // check if Word12 converter is installed
            if (_word12SaveFormat == -1)
            {
                System.Windows.Forms.MessageBox.Show(_addinLib.GetString("OdfConverterNotInstalled"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                return(false);
            }

            LateBindingObject doc = _application.Invoke("ActiveDocument");

            // the second test deals with blank documents
            // (which are in a 'saved' state and have no extension yet(?))
            if (!doc.GetBool("Saved") ||
                doc.GetString("FullName").IndexOf('.') < 0 ||
                doc.GetString("FullName").IndexOf("http://") == 0 ||
                doc.GetString("FullName").IndexOf("https://") == 0 ||
                doc.GetString("FullName").IndexOf("ftp://") == 0
                )
            {
                System.Windows.Forms.MessageBox.Show(_addinLib.GetString("OdfSaveDocumentBeforeExport"), DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                return(false);
            }
            else
            {
                System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();

                sfd.AddExtension = true;
                sfd.DefaultExt   = "odt";
                sfd.Filter       = this._addinLib.GetString(this.OdfFileType) + this.ExportOdfFileFilter
                                   + this._addinLib.GetString(ODF_FILE_TYPE_OTT) + EXPORT_ODF_FILE_FILTER_OTT
                                   + this._addinLib.GetString(ALL_FILE_TYPE) + this.ExportAllFileFilter;
                sfd.InitialDirectory             = doc.GetString("Path");
                sfd.OverwritePrompt              = true;
                sfd.SupportMultiDottedExtensions = true;
                sfd.Title = this._addinLib.GetString(EXPORT_LABEL);
                string ext = '.' + sfd.DefaultExt;
                sfd.FileName = doc.GetString("FullName").Substring(0, doc.GetString("FullName").LastIndexOf('.')); // +ext;

                // process the chosen documents
                if (System.Windows.Forms.DialogResult.OK == sfd.ShowDialog())
                {
                    // name of the file to create
                    string odfFileName = sfd.FileName;
                    // multi dotted extensions support
                    // Note: sfd.FilterIndex is 1-based
                    if (!odfFileName.ToLower().EndsWith(".odt") && sfd.FilterIndex == 1)
                    {
                        odfFileName += ".odt";
                    }
                    else if (!odfFileName.ToLower().EndsWith(".ott") && sfd.FilterIndex == 2)
                    {
                        odfFileName += ".ott";
                    }
                    // name of the document to convert
                    string sourceFileName = doc.GetString("FullName");
                    // name of the temporary Word12 file created if current file is not already a Word12 document
                    string tempDocxName = null;
                    // store current cursor
                    WdCursorType currentCursor = (WdCursorType)_application.Invoke("System").GetInt32("Cursor");
                    // display hourglass
                    this._application.Invoke("System").SetInt32("Cursor", (int)WdCursorType.wdCursorWait);


                    if (!(doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLDocument ||
                          doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLDocumentMacroEnabled ||
                          doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLTemplate ||
                          doc.GetInt32("SaveFormat") == (int)WdSaveFormat.wdFormatXMLTemplateMacroEnabled))
                    {
                        try
                        {
                            // if file is not currently in Word12 format
                            // 1. Create a copy
                            // 2. Open it and do a "Save as Word12" (copy needed not to perturb current openened document
                            // 3. Convert the Word12 copy to ODF
                            // 4. Remove both temporary created files

                            // duplicate the file to keep current file "as is"
                            string tempCopyName = Path.GetTempFileName() + Path.GetExtension((string)sourceFileName);
                            File.Copy((string)sourceFileName, (string)tempCopyName);

                            //BUG FIX #1743469
                            FileInfo fi = new FileInfo(tempCopyName);
                            if (fi.IsReadOnly)
                            {
                                fi.IsReadOnly = false;
                            }
                            //BUG FIX #1743469

                            // open the duplicated file
                            bool confirmConversions = false;
                            bool readOnly           = false;
                            bool addToRecentFiles   = false;
                            bool isVisible          = false;

                            LateBindingObject newDoc = OpenDocument(tempCopyName, confirmConversions, readOnly, addToRecentFiles, isVisible, false);

                            newDoc.Invoke("Windows").Invoke("Item", 1).SetBool("Visible", false);

                            // generate docx file from the duplicated file (under a temporary file)
                            string outputExtension =
                                tempDocxName = this._addinLib.GetTempPath((string)sourceFileName, ".docx");

                            SaveDocumentAs(newDoc, tempDocxName);

                            // close and remove the duplicated file
                            newDoc.Invoke("Close", WdSaveOptions.wdDoNotSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat, Type.Missing);

                            //BUG FIX #1743469
                            try
                            {
                                File.Delete((string)tempCopyName);
                            }
                            catch (Exception ex)
                            {
                                //If delete does not work, don't stop the rest of the process
                                //The tempFile will be deleted by the system
                                System.Diagnostics.Trace.WriteLine(ex.ToString());
                            }
                            //BUG FIX #1743469

                            // Now the file to be converted is
                            sourceFileName = tempDocxName;
                        }
                        catch (Exception ex)
                        {
                            _application.Invoke("System").SetInt32("Cursor", (int)currentCursor);

                            String lMsg;

                            lMsg = _addinLib.GetString("OdfExportErrorTryDocxFirst");
                            System.Windows.Forms.MessageBox.Show(lMsg, DialogBoxTitle, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop);
                            System.Diagnostics.Trace.WriteLine(ex.ToString());
                            return(false);
                        }
                    }

                    ConversionOptions options = new ConversionOptions();
                    options.InputFullName         = sourceFileName;
                    options.OutputFullName        = odfFileName;
                    options.InputFullNameOriginal = doc.GetString("FullName");
                    options.ConversionDirection   = ConversionDirection.DocxToOdt;
                    options.Generator             = this.GetGenerator();
                    options.DocumentType          = Path.GetExtension(odfFileName).ToUpper().Equals(".OTT") ? DocumentType.Template : DocumentType.Document;
                    options.ShowProgress          = true;
                    options.ShowUserInterface     = true;

                    this._addinLib.OoxToOdf(sourceFileName, odfFileName, options);

                    if (tempDocxName != null && File.Exists((string)tempDocxName))
                    {
                        this._addinLib.DeleteTempPath((string)tempDocxName);
                    }
                    _application.Invoke("System").SetInt32("Cursor", (int)currentCursor);
                }
                return(true);
            }
        }