Example #1
0
        //</Snippet8>


        //<Snippet9>
        private void ModifyCachedString(string documentPath)
        {
            int            runtimeVersion  = 0;
            ServerDocument serverDocument1 = null;

            try
            {
                runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

                if (runtimeVersion != 3)
                {
                    MessageBox.Show("This document does not have a Visual Studio Tools for " +
                                    "Office customization, or it has a customization that was created with " +
                                    "a version of the runtime that is incompatible with this version of the " +
                                    "ServerDocument class.");
                    return;
                }

                if (ServerDocument.IsCacheEnabled(documentPath))
                {
                    //<Snippet11>
                    //<Snippet12>
                    serverDocument1 = new ServerDocument(documentPath);
                    CachedDataHostItem hostItem1 =
                        serverDocument1.CachedData.HostItems["ExcelWorkbook1.Sheet1"];
                    CachedDataItem dataItem1 = hostItem1.CachedData["CachedString"];
                    //</Snippet12>

                    if (dataItem1 != null &&
                        Type.GetType(dataItem1.DataType) == typeof(string))
                    {
                        dataItem1.SerializeDataInstance("This is the new cached string value.");
                        serverDocument1.Save();
                    }
                    //</Snippet11>
                }
                else
                {
                    MessageBox.Show("The specified document does not have cached data.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
            }
            catch (UnknownCustomizationFileException)
            {
                System.Windows.Forms.MessageBox.Show("The specified document has a file " +
                                                     "extension that is not supported by Visual Studio Tools for Office.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }
            }
        }
Example #2
0
        static private void InitializeCachedDataSet()
        {
            //<Snippet2>
            //<Snippet3>
            AdventureWorksDataSet.AdventureWorksLTDataSet productDataSet =
                new AdventureWorksDataSet.AdventureWorksLTDataSet();
            AdventureWorksDataSet.AdventureWorksLTDataSetTableAdapters.ProductTableAdapter productTableAdapter =
                new AdventureWorksDataSet.AdventureWorksLTDataSetTableAdapters.ProductTableAdapter();

            string workbookPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.MyDocuments) +
                                  @"\AdventureWorksReport\bin\Debug\AdventureWorksReport.xlsx";
            ServerDocument serverDocument1 = null;

            //</Snippet3>

            //<Snippet4>
            try
            {
                productTableAdapter.Fill(productDataSet.Product);
                Console.WriteLine("The local dataset is filled.");

                serverDocument1 = new ServerDocument(workbookPath);
                CachedDataHostItem dataHostItem1 =
                    serverDocument1.CachedData.HostItems["AdventureWorksReport.Sheet1"];
                CachedDataItem dataItem1 = dataHostItem1.CachedData["adventureWorksLTDataSet"];

                // Initialize the worksheet dataset with the local dataset.
                if (dataItem1 != null)
                {
                    dataItem1.SerializeDataInstance(productDataSet);
                    serverDocument1.Save();
                    Console.WriteLine("The data is saved to the data cache.");
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("The data object is not found in the data cache.");
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("The specified workbook does not exist.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }

                Console.WriteLine("\n\nPress Enter to close the application.");
                Console.ReadLine();
            }
            //</Snippet4>
            //</Snippet2>
        }