Nasıl Yapılır: Sunucudaki Çalışma Kitabından Önbelleğe Alınmış Verileri Alma
Excel'i çalıştırmadan, belge düzeyi Office projesinin bir parçası olan Microsoft Office Excel çalışma kitabının önbelleğinden verileri alabilirsiniz. Bu, sunucuda depolanan Excel çalışma kitaplarından verilerin alınabilmesine olanak sağlar.
Uygulama alanı: Bu konudaki bilgiler Excel 2007 ve Excel 2010 uygulamalarının belge düzeyi projelerine yöneliktir. Daha fazla bilgi için bkz. Office Uygulamalarında Kullanılabilir Özellikler ve Proje Türü.
Veriyi alacak kod, çalıştığınız belgeyle ilişkilendirilen Office proje derlemesinin dışında olmalıdır, örn. ASP.NET Web sayfasında, konsol uygulaması veya Windows Forms uygulaması.
Bu konu hakkındaki kod örneğini kullanmak amacıyla adım adım yönergeler için bkz. İzlenecek Yol: Sunucudaki Çalışma Kitabından Önbellekteki Veriye Erişme.
Örnek
Aşağıdaki kod örneğinde, ilk başta AdventureWorksLTDataSet isimli türü belirtilmiş veri kümesinin bir örneği oluşturulur. Sonra, kod Excel çalışma kitabında önbelleğe alınmış, türü belirlenmiş aynı veri kümesinin doldurulan örneğine erişmek için ServerDocument sınıfının CachedData özelliğini kullanır ve yerel veri kümesindeki önbelleğe alınmış veri kümesinden veriyi okur.
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())
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());
}
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();
}
Kodu Derleme
Bu konudaki kod örneği, aşağıdaki uygulamalarla kullanılmak üzere tasarlanmıştır:
Türü belirlenmiş veri kümesini tanımlayan sınıf kitaplığı projesine erişimi olan konsol uygulaması. Kod, konsol uygulamasında çalışır.
Excel belge düzeyi özelleştirmesinin bir parçası olan Excel çalışma kitabı. Çalışma kitabı, aynı veriyi içeren, AdventureWorksLTDataSet isimli önbelleğe alınmış bir veri kümesine sahiptir.
Kodu kullanmak üzere adım adım yönergeler için bkz. İzlenecek Yol: Sunucudaki Çalışma Kitabından Önbellekteki Veriye Erişme.
Ayrıca bkz.
Görevler
İzlenecek Yol: Sunucudaki Çalışma Kitabından Önbellekteki Veriye Erişme
Nasıl Yapılır: Çevrimdışı veya Sunucuda Kullanmak İçin Veriyi Önbelleğe Alma
Nasıl Yapılır: Sunucu Üzerindeki Çalışma Kitabında Önbellekteki Veriyi Değiştirme