通过UWP在Excel上进行读写 [英] Reading and writing on Excel from UWP
问题描述
我需要使用一个包含宏的现有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.
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
官方示例.例如,您可能需要按以下方式通过代码读取一个节点(此方法不是直接读取工作表中的值,而是值地址):
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屋!