gpt4 book ai didi

c# VS Express 2012 excel xml 读取和列表

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:21 25 4
gpt4 key购买 nike

我开始使用 C# 编程。为此,我可以使用 Visual Studio Express 2012。我正在尝试创建应用程序,该应用程序将从特定列(但未指定该列中的条目数)的 xml 电子表格 2003 导入数据,并且它将列出每个单元格中的文本(所有单元格都在该列中)。我已经阅读了一些关于它的主题,比如这个: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/4fce4765-2d05-4a2b-8d0a-6219e87f3307/reading-excel-file-using-c-in-winrt-platform?forum=winappswithcsharp

但大多数答案都与 Visual Studio 2012 相关,而不是 express 版本,因此我仅限于库和扩展。当我尝试使用这些解决方案时,大多数都无法在我的 VS Express 2012 中使用,因为它们缺少一些东西。

这个程序对我有用,它返回一个特定单元格的值。我该如何更改它,以便它读取该列中的每个单元格,将每个值分配给一个表(或者可能是变量),这样我就可以处理这些内容并可能在以后随机化顺序?

namespace UnitTest{    public class TestCode    {        //ReadExcelCellTest        public static void Main()        {            XDocument document = XDocument.Load(@"C:\Projekt2\File1.xml");            XNamespace workbookNameSpace = @"urn:schemas-microsoft-com:office:spreadsheet";

// Get worksheet
var query = from w in document.Elements(workbookNameSpace + "Workbook").Elements(workbookNameSpace + "Worksheet")
where w.Attribute(workbookNameSpace + "Name").Value.Equals("Sheet1")
select w;
List<XElement> foundWoksheets = query.ToList<XElement>();
if (foundWoksheets.Count() <= 0) { throw new ApplicationException("Worksheet Settings could not be found"); }
XElement worksheet = query.ToList<XElement>()[0];

// Get the row for "Seat"
query = from d in worksheet.Elements(workbookNameSpace + "Table").Elements(workbookNameSpace + "Row").Elements(workbookNameSpace + "Cell").Elements(workbookNameSpace + "Data")
where d.Value.Equals("StateID")
select d;
List<XElement> foundData = query.ToList<XElement>();
if (foundData.Count() <= 0) { throw new ApplicationException("Row 'StateID' could not be found"); }
XElement row = query.ToList<XElement>()[0].Parent.Parent;

// Get value cell of Etl_SPIImportLocation_ImportPath setting
XElement cell = row.Elements().ToList<XElement>()[1];

// Get the value "Leon"
string cellValue = cell.Elements(workbookNameSpace + "Data").ToList<XElement>()[0].Value;

Console.WriteLine(cellValue);
}
}

最佳答案

我相信任何版本的 Visual Studio 都允许您使用开源库: http://closedxml.codeplex.com/

文档链接:http://closedxml.codeplex.com/wikipage?title=Finding%20and%20extracting%20the%20data&referringTitle=Documentation

读取 Excel 单元格值应该会容易得多。

How can I change it, so it will read every cell from that column, assign every value to a table (or maybe variable) so I can work with this content and maybe randomize order later?

希望closedxml能帮助您完成这个任务。

关于c# VS Express 2012 excel xml 读取和列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21190948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com