مشاركة عبر


كيفية القيام بما يلي: تغيير بيانات مخزنة مؤقـتًا في مصنف على خادم

ينطبق على

تنطبق المعلومات الموجودة في هذا الموضوع فقط على أنواع المشاريع وإصدارات Microsoft Office التالية: لمزيد من المعلومات، راجع الميزات المتوفرة بواسطة تطبيقات Office و نوع المشروع.

نوع المشروع

  • مشروعات على مستوى المستند

إصدار Microsoft Office

  • Excel 2007

  • Excel 2010

يمكنك إدراج البيانات في ذاكرة التخزين المؤقت لمصنف Microsoft Office Excel و التى هى جزء من مشروع Office على مستوى المستند دون تشغيل Excel. هذا يجعل من الممكن إدراج بيانات في مصنفات Excel التي قد تم تخزينها على الخادم .

يجب أن تكون التعليمات البرمجية لاسترداد البيانات خارج تجميع مشروع Office الأساسى المقترن بالمستند الذي تعمل معه ، على سبيل المثال في صفحة ويب ASP.NET أو على تطبيق وحدة التحكم أو تطبيقات Windows Forms.

للحصول على إرشادات خطوة بخطوة لاستخدام مثال التعليمات البرمجية في هذا الموضوع ، راجع الإرشادات التفصيلية: تغيير بيانات مخزنة مؤقـتًا في مصنف على خادم.

مثال

يقوم مثال التعليمات البرمجية التالي أولاً بإنشاء مثيل من مجموعة البيانات المكتوبة باسم AdventureWorksLTDataSet. بعد ذلك، تستخدم التعليمات البرمجية الفئة ServerDocument للوصول إلى مثيل مملوء من نفس مجموعة البيانات المكتوبة التي تم بالفعل تخزينها مؤقتاً في مصنف Excel، ثم قراءة البيانات من مجموعة البيانات المخزنة مؤقتاً إلى مجموعة البيانات المحلية. وأخيراً، تقوم التعليمات البرمجية بتعديل القيمة ListPrice في كل صف من مجموعة البيانات المحلية و من ثم تكتب البيانات التى قد تم تغييرها مرة أخرى في مجموعة البيانات المخزنة مؤقتاً عن طريق استخدام خاصيةXml .

Dim productDataSet As New AdventureWorksDataSet.AdventureWorksLTDataSet()
Dim workbookPath As String = System.Environment.GetFolderPath( _
    Environment.SpecialFolder.MyDocuments) & _
    "\AdventureWorksReport\bin\Debug\AdventureWorksReport.xlsx"
Dim serverDocument1 As ServerDocument = Nothing

Try
    serverDocument1 = New ServerDocument(workbookPath)
    Dim dataHostItem1 As CachedDataHostItem = _
        serverDocument1.CachedData.HostItems("AdventureWorksReport.Sheet1")
    Dim dataItem1 As CachedDataItem = dataHostItem1.CachedData("AdventureWorksLTDataSet")

    If dataItem1 IsNot Nothing Then
        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.
        Dim schemaReader As New System.IO.StringReader(dataItem1.Schema)
        Dim xmlReader As 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.
        Dim row As AdventureWorksDataSet.AdventureWorksLTDataSet.ProductRow
        For Each row In productDataSet.Product.Rows
            If row.ProductCategoryID < 20 Then
                row.ListPrice = row.ListPrice + row.ListPrice * 0.1
            Else
                row.ListPrice = row.ListPrice - row.ListPrice * 0.1
            End If
        Next row

        ' Write the modified local dataset to the worksheet dataset using the DiffGram format.
        Dim stringIn As New System.Text.StringBuilder()
        Dim stringOut As 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.")
    End If
Catch ex As System.IO.FileNotFoundException
    Console.WriteLine("The specified workbook does not exist.")
Catch ex As System.Xml.XmlException
    Console.WriteLine("The data object has invalid XML information.")
Finally
    If Not (serverDocument1 Is Nothing) Then
        serverDocument1.Close()
    End If
    Console.WriteLine(vbLf & vbLf & "Press Enter to close the application.")
    Console.ReadLine()
End Try
AdventureWorksDataSet.AdventureWorksLTDataSet productDataSet =
    new AdventureWorksDataSet.AdventureWorksLTDataSet();
string workbookPath = System.Environment.GetFolderPath(
    Environment.SpecialFolder.MyDocuments) +
    @"\AdventureWorksReport\bin\Debug\AdventureWorksReport.xlsx";
ServerDocument serverDocument1 = null;

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();
}

التحويل البرمجي للتعليمات البرمجية

مثال التعليمات البرمجية في هذا الموضوع تم تصميمه للاستخدام في التطبيقات التالية:

  • تطبيق وحدة تحكم الذي يملك حق الوصول إلى مشروع مكتبة الفئة الذي يقوم بتعريف مجموعة بيانات مكتوبة. يتم تشغيل التعليمات البرمجية في تطبيق وحدة التحكم.

  • مصنف Excel الذي هو جزء من تخصيص على مستوى المستند لـ Excel. يحتوي المصنف على مجموعة بيانات مخزنة مؤقتاً تسمى AdventureWorksLTDataSet تحتوي على بعض البيانات.

للحصول على إرشادات خطوة بخطوة حول استخدام التعليمة البرمجية راجع الإرشادات التفصيلية: تغيير بيانات مخزنة مؤقـتًا في مصنف على خادم.

راجع أيضًا:

المهام

الإرشادات التفصيلية: تغيير بيانات مخزنة مؤقـتًا في مصنف على خادم

كيفية القيام بما يلي: تخزين البيانات مؤقتًا للاستخدام دون اتصال أو على خادم

كيفية القيام بما يلي: استرداد البيانات المخزنة مؤقتاً من مصنف على خادم

كيفية القيام بما يلي: إدراج بيانات في مصنف على خادم

المبادئ

الوصول إلى البيانات في المستندات على الخادم

DiffGrams (ADO.NET)