Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!
Posted

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900