Example #1
0
        //</Snippet9>


        //<Snippet10>
        private void ReadCachedStringValue(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))
                {
                    serverDocument1 = new ServerDocument(documentPath);
                    CachedDataHostItem hostItem1 =
                        serverDocument1.CachedData.HostItems["ExcelWorkbook1.Sheet1"];
                    CachedDataItem dataItem1 = hostItem1.CachedData["CachedString"];

                    if (dataItem1 != null &&
                        Type.GetType(dataItem1.DataType) == typeof(string))
                    {
                        using (System.IO.StringReader stringReader =
                                   new System.IO.StringReader(dataItem1.Xml))
                        {
                            System.Xml.Serialization.XmlSerializer serializer =
                                new System.Xml.Serialization.XmlSerializer(typeof(string));
                            string cachedString = serializer.Deserialize(stringReader) as string;
                            MessageBox.Show("The value of CachedString is: " + cachedString);
                        }
                    }
                }
                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
        //</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 #3
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>
        }
Example #4
0
        static private void ReadCachedDataSet()
        {
            //<Snippet9>
            //<Snippet10>
            AdventureWorksDataSet.AdventureWorksLTDataSet productDataSet =
                new AdventureWorksDataSet.AdventureWorksLTDataSet();
            string workbookPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.MyDocuments) +
                                  @"\AdventureWorksReport\bin\Debug\AdventureWorksReport.xlsx";
            ServerDocument serverDocument1 = null;

            //</Snippet10>

            //<Snippet11>
            try
            {
                serverDocument1 = new ServerDocument(workbookPath);
                CachedDataHostItem dataHostItem1 =
                    serverDocument1.CachedData.HostItems["AdventureWorksReport.Sheet1"];
                CachedDataItem dataItem1 = dataHostItem1.CachedData["adventureWorksLTDataSet"];

                if (dataItem1 != null)
                {
                    Console.WriteLine("Before reading data from the cache dataset, the local dataset has " +
                                      "{0} rows.", productDataSet.Product.Rows.Count.ToString());

                    // Read the cached data from the worksheet dataset into the local dataset.
                    System.IO.StringReader schemaReader = new System.IO.StringReader(dataItem1.Schema);
                    System.IO.StringReader xmlReader    = new System.IO.StringReader(dataItem1.Xml);
                    productDataSet.ReadXmlSchema(schemaReader);
                    productDataSet.ReadXml(xmlReader);

                    Console.WriteLine("After reading data from the cache dataset, the local dataset has " +
                                      "{0} rows.", productDataSet.Product.Rows.Count.ToString());
                }
                else
                {
                    Console.WriteLine("The data object is not found in the data cache.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("The specified workbook does not exist.");
            }
            catch (System.Xml.XmlException)
            {
                Console.WriteLine("The data object has invalid XML information.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }

                Console.WriteLine("\n\nPress Enter to close the application.");
                Console.ReadLine();
            }
            //</Snippet11>
            //</Snippet9>
        }
Example #5
0
        static private void ModifyCachedDataSet()
        {
            //<Snippet5>
            //<Snippet6>
            AdventureWorksDataSet.AdventureWorksLTDataSet productDataSet =
                new AdventureWorksDataSet.AdventureWorksLTDataSet();
            string workbookPath = System.Environment.GetFolderPath(
                Environment.SpecialFolder.MyDocuments) +
                                  @"\AdventureWorksReport\bin\Debug\AdventureWorksReport.xlsx";
            ServerDocument serverDocument1 = null;

            //</Snippet6>

            //<Snippet7>
            try
            {
                serverDocument1 = new ServerDocument(workbookPath);
                CachedDataHostItem dataHostItem1 =
                    serverDocument1.CachedData.HostItems["AdventureWorksReport.Sheet1"];
                CachedDataItem dataItem1 = dataHostItem1.CachedData["adventureWorksLTDataSet"];

                if (dataItem1 != null)
                {
                    Console.WriteLine("Before reading data from the cache dataset, the local dataset has " +
                                      "{0} rows.", productDataSet.Product.Rows.Count.ToString());

                    // Read the cached data from the worksheet dataset into the local dataset.
                    System.IO.StringReader schemaReader = new System.IO.StringReader(dataItem1.Schema);
                    System.IO.StringReader xmlReader    = new System.IO.StringReader(dataItem1.Xml);
                    productDataSet.ReadXmlSchema(schemaReader);
                    productDataSet.ReadXml(xmlReader);

                    Console.WriteLine("After reading data from the cache dataset, the local dataset has " +
                                      "{0} rows.", productDataSet.Product.Rows.Count.ToString());

                    // Modify the prices of each product in the local dataset.
                    foreach (AdventureWorksDataSet.AdventureWorksLTDataSet.ProductRow row in
                             productDataSet.Product.Rows)
                    {
                        if (row.ProductCategoryID < 20)
                        {
                            row.ListPrice = row.ListPrice + (row.ListPrice * (Decimal).10);
                        }
                        else
                        {
                            row.ListPrice = row.ListPrice - (row.ListPrice * (Decimal).10);
                        }
                    }

                    // Write the modified local dataset to the worksheet dataset using the DiffGram format.
                    System.Text.StringBuilder stringIn  = new System.Text.StringBuilder();
                    System.IO.StringWriter    stringOut = new System.IO.StringWriter(stringIn);
                    productDataSet.WriteXml(stringOut, System.Data.XmlWriteMode.DiffGram);
                    dataItem1.Xml = stringIn.ToString();

                    serverDocument1.Save();
                    Console.WriteLine("The product prices have been modified.");
                }
                else
                {
                    Console.WriteLine("The data object is not found in the data cache.");
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("The specified workbook does not exist.");
            }
            catch (System.Xml.XmlException)
            {
                Console.WriteLine("The data object has invalid XML information.");
            }
            finally
            {
                if (serverDocument1 != null)
                {
                    serverDocument1.Close();
                }

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