在uwp中使用excel文件

时间:2018-05-20 18:10:03

标签: vb.net uwp

我的问题是 - 我无法从excel文件中读取数据。我正在使用spreadsheetgear库。我正在为uwp platfom写作(我需要显示来自excel文件的用户数据)。可能有人帮忙吗? (gv1 = gridview)。或者我应该使用其他图书馆?可能是已经使用过电子表格的任何人都可以提供帮助。

Public Async Function open_excel() As Task
        Dim Values As String()
        Dim filepicker As New FileOpenPicker With {.ViewMode = PickerViewMode.List, .SuggestedStartLocation = PickerLocationId.Downloads}
        filepicker.FileTypeFilter.Add(".xls")
        filepicker.FileTypeFilter.Add(".xlsx")
        Dim storefile As StorageFile = Await filepicker.PickSingleFileAsync
        Dim workbook As IWorkbook = Factory.GetWorkbook(storefile.Path)
        Dim worksheet As IWorksheet = workbook.Worksheets(0)
        gv1.ItemsSource = worksheet.Cells

1 个答案:

答案 0 :(得分:0)

spreadsheetgear文档中,如果要从文件中打开Excel,则需要创建workbookSet,然后调用OpenFromStream方法。我已经测试过,它在我这边工作。

StorageFile file = await openPicker.PickSingleFileAsync();
 if (file != null)
 {
     using (var steam = await file.OpenAsync(FileAccessMode.ReadWrite))
     {
         var workbookSet = Factory.GetWorkbookSet(CultureInfo.CurrentUICulture);
         var workbook = workbookSet.Workbooks.OpenFromStream(steam.AsStream());
         var worksheet = workbook.Worksheets[0];
         var Used = worksheet.UsedRange;
         var items = Used.Value;
     }
 }
 else
 {
     return;
 }