从windows phone 8中的excel文件中读取数据 [英] Read data from excel file in windows phone 8
本文介绍了从windows phone 8中的excel文件中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建一个应用程序,它需要从xlsx文件(excel)读取数据,我在Windows Phone 8项目中使用了数据集,但数据集未激活,我收到一条消息
System.ComponentModel.MarshalByValueComponent'在assembly' System,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'引用
你能帮帮我吗!谢谢!
I creating a app, it need read data from xlsx file (excel), i used Dataset in Windows Phone 8 project, but Dataset not active, i get a message
System.ComponentModel.MarshalByValueComponent' in assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' referenced
Can you help me! Thank!
推荐答案
使用System;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.IO.Compression;
使用System.Xml.Linq;
使用Windows.Storage;
命名空间OpenXMLReader
{
公共静态类ExcelReader
{
public static StorageFile TargetFile {get;组; }
静态列表< string> _sharedStrings;
static List< dictionary>< string,>> _derivedData;
public static List< dictionary>< string,>> DerivedData
{
get
{
return _derivedData;
}
}
静态列表< string> _header;
public static List< string>标题{get {return _header; } $
public static void StartReadFile()
{
ZipArchive z = new ZipArchive(TargetFile.OpenStreamForReadAsync ().Result);
var worksheet = z.GetEntry(xl / worksheets / sheet1.xml);
var sharedString = z.GetEntry(xl / sharedStrings.xml);
//获取共享字符串
_sharedStrings = new List< string>();
使用(var sr = sharedString.Open())
{
XDocument xdoc = XDocument.Load(sr);
_sharedStrings =
(
来自e中的xdoc.Root.Elements()
选择e.Elements()。First()。Value
).ToList();
}
//获取标题
使用(var sr = worksheet.Ope n())
{
XDocument xdoc = XDocument.Load(sr);
//获取第一张数据的元素
XNamespace xmlns =http://schemas.openxmlformats.org/spreadsheetml/2006/main;
XElement sheetData = xdoc.Root.Element(xmlns +sheetData);
_header = new List< string>();
//首先构建标题
var firstRow = sheetData.Elements()。第一个();
//满是c
foreach(第一行中的变量.Elements())
{
// c元素,如果有属性t,则需要咨询sharedStrings
string val = c.Elements()。First()。Value;
if(c.Attribute(t)!= null)
{
_header.Add(_sha) redStrings [Convert.ToInt32(val)]);
}
else
{
_header.Add( val);
}
}
//现在构建内容
_derivedData = new List< dictionary>< string,>>();
foreach(varData in sheetData.Elements())
{
//跳过第1行
if(row.Attribute(r)。值==1)
继续;
字典<字符串,> rowData = new Dictionary< string,>();
int i = 0;
foreach(var c in row.Elements())
{
//下至每个c元素
string val = c.Elements()。First()。Value;
if (c.Attribute(t)!= null)
{
rowData.Add(_header [i],_ sharedStrings [Convert.ToInt32(val)]);
}
其他
{
rowData.Add(_header [i],val);
}
i ++;
}
_derivedData.Add(rowData);
} < br $>
}
string s =;;
} < br $>
}
}
参考: http://social.msdn.microsoft.com/Forums/en-US/4fce4765-2d05-4a2b-8d0a-6219e87f3307/reading-excel-file-using-c-in-winrt -platform [ ^ ]
问候,
Nelge
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Compression;
using System.Xml.Linq;
using Windows.Storage;
namespace OpenXMLReader
{
public static class ExcelReader
{
public static StorageFile TargetFile { get; set; }
static List<string> _sharedStrings;
static List<dictionary><string,>> _derivedData;
public static List<dictionary><string,>> DerivedData
{
get
{
return _derivedData;
}
}
static List<string> _header;
public static List<string> Headers { get { return _header; } }
public static void StartReadFile()
{
ZipArchive z = new ZipArchive(TargetFile.OpenStreamForReadAsync().Result);
var worksheet = z.GetEntry("xl/worksheets/sheet1.xml");
var sharedString = z.GetEntry("xl/sharedStrings.xml");
//get shared string
_sharedStrings = new List<string>();
using (var sr = sharedString.Open())
{
XDocument xdoc = XDocument.Load(sr);
_sharedStrings =
(
from e in xdoc.Root.Elements()
select e.Elements().First().Value
).ToList();
}
//get header
using (var sr = worksheet.Open())
{
XDocument xdoc = XDocument.Load(sr);
//get element to first sheet data
XNamespace xmlns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main";
XElement sheetData = xdoc.Root.Element(xmlns + "sheetData");
_header = new List<string>();
//build header first
var firstRow = sheetData.Elements().First();
//full of c
foreach (var c in firstRow.Elements())
{
//the c element, if have attribute t, will need to consult sharedStrings
string val = c.Elements().First().Value;
if (c.Attribute("t") != null)
{
_header.Add(_sharedStrings[Convert.ToInt32(val)]);
}
else
{
_header.Add(val);
}
}
//build content now
_derivedData = new List<dictionary><string,>>();
foreach (var row in sheetData.Elements())
{
//skip row 1
if (row.Attribute("r").Value == "1")
continue;
Dictionary<string,> rowData = new Dictionary<string,>();
int i = 0;
foreach (var c in row.Elements())
{
//down to each c element
string val = c.Elements().First().Value;
if (c.Attribute("t") != null)
{
rowData.Add(_header[i], _sharedStrings[Convert.ToInt32(val)]);
}
else
{
rowData.Add(_header[i], val);
}
i++;
}
_derivedData.Add(rowData);
}
}
string s = ";";
}
}
}
Refer : http://social.msdn.microsoft.com/Forums/en-US/4fce4765-2d05-4a2b-8d0a-6219e87f3307/reading-excel-file-using-c-in-winrt-platform[^]
Regards,
Nelge
这篇关于从windows phone 8中的excel文件中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文