从 UWP 读取和写入 Excel [英] Reading and writing on Excel from UWP

查看:23
本文介绍了从 UWP 读取和写入 Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用包含宏的现有 Excel 文件,我需要写入一些数据,激活一些宏,然后从我的 UWP 应用程序的 excel 文件中读取结果.为此,我需要避免使用任何商业图书馆,获得所需结果的最佳方法是什么?

I need to use an existing Excel file that contain macros, i need to write some data, activate some macros then read the result from the excel file from my UWP application. I need to avoid any commercial library for that, what is the best way to get the needed result ?

谢谢

推荐答案

实际上使用了第三方库,比如 syncfusion 实现这可能是一个好方法.由于您避免使用任何可能需要自己编写库的库,因为据我所知,目前没有开放库根据 这个主题.

Actually using a third party library like syncfusion to implement this may be a good way. As you avoid any library that you may need to write a library by yourself since as far as I known currently there is no open library according to this thread.

excel 文件格式 .xlsx 可能是一个 .zip 包,你应该能够解压它,你会发现很多 .xml 文件夹内的格式文件.在 uwp 中,我们有用于读取 xml 文件的 API.所以你可以对这些xml文件进行读写操作.

The excel file format .xlsx could be a .zip package that you should be able to decompression it and you will find a lot of .xml format files inside the folder. In uwp we have APIs for reading the xml files. So you can do reading and writing manipulation with these xml files.

有关如何在 uwp 应用程序中解压缩文件,您应该能够使用 ZipArchive 类.比如解压一个excel文件,得到一张sheet可能是这样的:

For how to decompression a file in uwp app you should be able use ZipArchive class. For example decompression an excel file and getting one sheet may be as follows:

private async void btnopenfile_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker opener = new FileOpenPicker();
    opener.ViewMode = PickerViewMode.Thumbnail;
    opener.FileTypeFilter.Add(".xlsx");
    opener.FileTypeFilter.Add(".xlsm");
    StorageFile file = await opener.PickSingleFileAsync();
    if (file != null)
    {             
        using (var fileStream = await file.OpenReadAsync())
        {
            using (ZipArchive archive = new ZipArchive(fileStream.AsStream(), ZipArchiveMode.Read))
            {
                worksheet = this.GetSheet(archive, "sheet1");
            }
        }
    }
}

private XmlDocument GetSheet(ZipArchive archive, string sheetName)
{
    XmlDocument sheet = new XmlDocument();
    ZipArchiveEntry archiveEntry = archive.GetEntry("xl/worksheets/" + sheetName + ".xml");

    using (var archiveEntryStream = archiveEntry.Open())
    {
        using (StreamReader reader = new StreamReader(archiveEntryStream))
        {
            string xml = reader.ReadToEnd();

            txtresult.Text = xml;
            sheet.LoadXml(xml);
        }
    }

    return sheet;
}

关于如何读写xml文件可以参考XmlDocument 官方示例.比如你可能需要通过如下代码读取一个节点(该方法不是直接读取sheet中的值而是直接读取值地址):

For how to read and write xml files you can reference the XmlDocument official sample. For example you may need read one node by code as follows(This method is not directly read the value in the sheet but the value address):

  private string ReadCell(XmlDocument worksheet, string cellAddress)
  {
      string value = string.Empty;
      XmlElement row = worksheet.SelectSingleNodeNS("//x:c[@r='" + cellAddress + "']", "xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"") as XmlElement;
      if (row != null)
      {
          value = row.InnerText;
      }
      return value;
  }

这个线程 你可以参考.更多细节功能请自行开发.

There is a complete reading sample on this thread you can reference. More detail features please develop by yourself.

这篇关于从 UWP 读取和写入 Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆