试图获取行值 [英] Trying to get row values

查看:80
本文介绍了试图获取行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将xlsx文件上传到我的azure网站,并在某些预定义的位置从电子表格中获取一些值。  我需要遍历一组行并对它们进行操作。  我想做的第一件事就是从A4单元格中获取
的值。  这就是所谓的团队名称,应该是一些文字。  相反,我得到的值为9. 我不知道值9的来源。  我试图以3种不同的方式确定单元格值,
并没有成功。  我尝试使用的代码如下。  单元格A4的值是"Pilot Flying J-1"。然而代码总是返回值9.我不能在世界上的哪个地方使用"9"。是来自。   I
共有9列数据(A - I),但这是我能找到匹配的唯一内容。  任何建议表示赞赏。  


WorkbookPart wbPart = document.WorkbookPart;

Sheet theSheet = wbPart.Workbook.Descendants< Sheet>()。

  &NBSP; Where(s => s.Name == sheetName).FirstOrDefault();

//如果没有工作表则抛出异常。

if(theSheet == null)b
{

  &NBSP;抛出新的ArgumentException(" sheetName");
$
}

//检索对工作表部分的引用。

WorksheetPart wsPart =

  &NBSP; (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
$


IEnumerable< Row> rows = wsPart.Worksheet.Descendants< Row>();



var rowPoint = 3;

var rowStart = rows.ElementAt(rowPoint ); $
var teamName = String.Empty;

var lastName = String.Empty;

var firstName = String.Empty;

var emailAddress = String.Empty;

var cellNumber = String.Empty;

var scoreKeeper = String.Empty;

var index = String.Empty;

teamName = rowStart.ElementAt(0).InnerText.Trim();


我的电子表格的一个子集如下所示。












< td height ="58"width ="131"style ="height:43.5pt;宽度:99pt">团队名称





事件:
上午和下午的活动将分开设置。
飞行员飞行J - 1




ASPInsider - 作者 - 否则我是输家。

解决方案

Hello Wally,


Cell有一个DataType属性,用于指示单元格内数据的类型。对于数字和日期类型,DataType属性的值为null。如果值为string,则为DataType property是CellValues.SharedString。如果DataType属性
为null,则代码返回单元格的值(它是一个数值)。否则,您需要以另一种方式获取值。


您可以从以下链接获得更多信息



如何:检索电子表格文档中的单元格值(Open XML SDK)


这是例如。

 // teamName = rowStart.ElementAt(0).InnerText.Trim(); 
Cell cel = rowStart.Descendants< Cell>()。FirstOrDefault();
if(cel.DataType!= null)
{
if(cel.DataType.Value == CellValues.SharedString)
{
var stringTable =
wbPart.GetPartsOfType< SharedStringTablePart>()
.FirstOrDefault();
if(stringTable!= null)
{
int value =(int.Parse)(cel.InnerText);
teamName =
stringTable.SharedStringTable
.ElementAt(值).InnerText;
}
}
}
其他
{
teamName = rowStart.ElementAt(0).InnerText;
}

问候,


Celeste



I am trying to upload an xlsx file to my azure website and to get some values out of the spreadsheet in some predefined locations.  I need to iterate through a set of rows and  operate on them.  The first thing that I would like to do is get a value out of cell A4.  This is what is called a team name, and that should be some text.  Instead, I am getting the value of 9.  I have no idea where the value 9 is coming from.  I have tried to determine the cell value 3 different ways, and have had no success.  The code I am trying to use is below.  The value of cell A4 is "Pilot Flying J - 1" yet the code is always returning the value 9.  I can't fine where in the world the "9" is coming from.  I have 9 total columns of data (A - I), but this is the only thing that I can find that matches.  Any suggestions are appreciated.  

WorkbookPart wbPart = document.WorkbookPart;
Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().
    Where(s => s.Name == sheetName).FirstOrDefault();
// Throw an exception if there is no sheet.
if (theSheet == null)
{
    throw new ArgumentException("sheetName");
}
// Retrieve a reference to the worksheet part.
WorksheetPart wsPart =
    (WorksheetPart)(wbPart.GetPartById(theSheet.Id));

IEnumerable<Row> rows = wsPart.Worksheet.Descendants<Row>();

var rowPoint = 3;
var rowStart = rows.ElementAt(rowPoint);
var teamName = String.Empty;
var lastName = String.Empty;
var firstName = String.Empty;
var emailAddress = String.Empty;
var cellNumber = String.Empty;
var scoreKeeper = String.Empty;
var index = String.Empty;
teamName = rowStart.ElementAt(0).InnerText.Trim();

A subset of my spreadsheet looks something like the below.

Event:
Morning and Afternoon Events are to be setup separate.
Team Name
Pilot Flying J - 1


ASPInsider - Author - Otherwise I am a loser.

解决方案

Hello Wally,

The Cell has a DataType property that indicates the type of the data within the cell. The value of the DataType property is null for numeric and date types. If the value is string, it's DataType property is CellValues.SharedString. If the DataType property is null, the code returns the value of the cell (it is a numeric value). Otherwise, you need get the value with another way.

You could get more info from below link

How to: Retrieve the values of cells in a spreadsheet document (Open XML SDK)

Here is the example.

                //teamName = rowStart.ElementAt(0).InnerText.Trim();
                Cell cel = rowStart.Descendants<Cell>().FirstOrDefault();
                if (cel.DataType != null)
                {
                    if (cel.DataType.Value == CellValues.SharedString)
                    {
                        var stringTable =
                                wbPart.GetPartsOfType<SharedStringTablePart>()
                                .FirstOrDefault();
                        if (stringTable != null)
                        {
                           int value = (int.Parse)(cel.InnerText);
                            teamName =
                                stringTable.SharedStringTable
                                .ElementAt(value).InnerText;
                        }
                   }
                }
                else
                {
                    teamName = rowStart.ElementAt(0).InnerText;
                }

Regards,

Celeste


这篇关于试图获取行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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