尝试在 C# 中读取 Excel 文件文件流,我得到的是数字而不是文本
Trying to read an Excel file file stream in C#, I get numbers instead of text
我正在尝试使用 DocumentFormat.OpenXml 读取上传的 Excel 文件。当我得到文件 (HttpPosteFileWrapper) 时,我只是想读取单元格并将它们写入文本字符串。 (稍后我会做更多,但我现在只是想习惯 OpenXml。)
我在 Excel 中的数据如下所示:
Field1 - Field2 - Phone - City
IT Department - Emp - 7175551234 - Springfield
HR - Emp - 7175556543 - W Springfield
代码如下所示:
var doc = SpreadsheetDocument.Open(file.InputStream, false);
WorkbookPart workbookPart = doc.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData thisSheet = worksheetPart.Worksheet.Elements<SheetData>().First();
System.Text.StringBuilder text = new System.Text.StringBuilder();
foreach (Row r in thisSheet.Elements<Row>())
{
foreach (Cell c in r.Elements<Cell>())
{
text.Append(c.CellValue.Text + ",");
}
text.AppendLine();
}
它创建的字符串如下所示:
49,51,50,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16,14,15,17,18,19,20,21,22,40,41,42,43,44,45,54,\r\n
52,24,23,25,26,27,7306,33,28,29,30,31,17033,32,34,7175555555,7175551234,7175554321,7175550000,35,36,37,36526,40179,38,39,30,31,17033,32,55,\r\n
53,46,47,48,555,\r\n
好像是数值通过了。是因为我使用了错误的流类型吗?
编辑: 我已经将我的代码更新为现在的样子,但它仍然无法正常工作。我好像没办法看到文字数据
public ActionResult ProfileImport(IEnumerable<HttpPostedFileBase> files)
{
// Build file list
int i = 1;
foreach (var file in files)
{
if (file.ContentLength > 0)
{
var doc = SpreadsheetDocument.Open(file.InputStream, false);
WorkbookPart workbookPart = doc.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData thisSheet = worksheetPart.Worksheet.Elements<SheetData>().First();
System.Text.StringBuilder text = new System.Text.StringBuilder();
foreach (Row r in thisSheet.Elements<Row>())
{
foreach (Cell c in r.Elements<Cell>())
{
string value = c.InnerText;
if (c.DataType != null && c.DataType.Value == CellValues.SharedString) // Check DataType exists
{
var stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>()
.FirstOrDefault(); // Get Table parts from workbookPart
if (stringTable != null)
value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
text.Append(value + ",");
}
else
text.Append(value + ",");
}
text.AppendLine();
}
var outText = text.ToString();
}
}
}
文件第一行的实际数据:
地址描述、地址 1、地址 2、城市、州、邮政编码、国家代码、电子邮件、手机、家庭电话、工作电话、传真、组织名称、部门、职位、开始日期、结束日期、组织地址 1、组织地址 2 组织城市、组织状态、组织邮政代码、组织国家代码、关键字
该行的 Row.InnerText:
“49515001234567891011121316141517181920212240414243444554”
Row.OuterXml:
"<x:row r=\"1\" spans=\"1:33\" s=\"3\" customFormat=\"1\" x14ac:dyDescent=\"0.25\" xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" xmlns:x=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">
<x:c r=\"A1\" s=\"3\" t=\"s\"><x:v>49</x:v></x:c>
<x:c r=\"B1\" s=\"3\" t=\"s\"><x:v>51</x:v></x:c>
<x:c r=\"C1\" s=\"3\" t=\"s\"><x:v>50</x:v></x:c>
<x:c r=\"D1\" s=\"3\" t=\"s\"><x:v>0</x:v></x:c>
<x:c r=\"E1\" s=\"3\" t=\"s\"><x:v>1</x:v></x:c>
<x:c r=\"F1\" s=\"3\" t=\"s\"><x:v>2</x:v></x:c>
<x:c r=\"G1\" s=\"3\" t=\"s\"><x:v>3</x:v></x:c>
<x:c r=\"H1\" s=\"3\" t=\"s\"><x:v>4</x:v></x:c>
<x:c r=\"I1\" s=\"3\" t=\"s\"><x:v>5</x:v></x:c>
<x:c r=\"J1\" s=\"3\" t=\"s\"><x:v>6</x:v></x:c>
<x:c r=\"K1\" s=\"3\" t=\"s\"><x:v>7</x:v></x:c>
<x:c r=\"L1\" s=\"3\" t=\"s\"><x:v>8</x:v></x:c>
<x:c r=\"M1\" s=\"3\" t=\"s\"><x:v>9</x:v></x:c>
<x:c r=\"N1\" s=\"3\" t=\"s\"><x:v>10</x:v></x:c>
<x:c r=\"O1\" s=\"3\" t=\"s\"><x:v>11</x:v></x:c>
<x:c r=\"P1\" s=\"4\" t=\"s\"><x:v>12</x:v></x:c>
<x:c r=\"Q1\" s=\"4\" t=\"s\"><x:v>13</x:v></x:c>
<x:c r=\"R1\" s=\"3\" t=\"s\"><x:v>16</x:v></x:c>
<x:c r=\"S1\" s=\"3\" t=\"s\"><x:v>14</x:v></x:c>
<x:c r=\"T1\" s=\"3\" t=\"s\"><x:v>15</x:v></x:c>
<x:c r=\"U1\" s=\"3\" t=\"s\"><x:v>17</x:v></x:c>
<x:c r=\"V1\" s=\"3\" t=\"s\"><x:v>18</x:v></x:c>
<x:c r=\"W1\" s=\"3\" t=\"s\"><x:v>19</x:v></x:c>
<x:c r=\"X1\" s=\"3\" t=\"s\"><x:v>20</x:v></x:c>
<x:c r=\"Y1\" s=\"3\" t=\"s\"><x:v>21</x:v></x:c>
<x:c r=\"Z1\" s=\"3\" t=\"s\"><x:v>22</x:v></x:c>
<x:c r=\"AA1\" s=\"3\" t=\"s\"><x:v>40</x:v></x:c>
<x:c r=\"AB1\" s=\"3\" t=\"s\"><x:v>41</x:v></x:c>
<x:c r=\"AC1\" s=\"3\" t=\"s\"><x:v>42</x:v></x:c>
<x:c r=\"AD1\" s=\"3\" t=\"s\"><x:v>43</x:v></x:c>
<x:c r=\"AE1\" s=\"3\" t=\"s\"><x:v>44</x:v></x:c>
<x:c r=\"AF1\" s=\"3\" t=\"s\"><x:v>45</x:v></x:c>
<x:c r=\"AG1\" s=\"3\" t=\"s\"><x:v>54</x:v></x:c>
</x:row>"
看起来这些是共享字符串 table 中字符串的索引。在 Excel 文件格式中,字符串数据存储在共享字符串 table 中,然后在单元格级别进行引用。根据文档,如果数据类型是文本,CellValue
returns StringTable
的索引。
对单元格中的数据类型了解不多(根据数据类型有不同的检索方法)。如果它是我认为的那样,它将是一个 SharedString
,您需要将其作为 SharedStringTablePart 检索,如 MSDN 页面所示:
https://msdn.microsoft.com/en-us/library/hh298534%28v=office.14%29.aspx?f=255&MSPPError=-2147217396
您的代码将如下所示:
foreach (Cell c in r.Elements<Cell>())
{
string value = c.InnerText;
if (c.DataType.Value == CellValues.SharedString)
{
var stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>()
.FirstOrDefault();
if (stringTable != null)
value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
text.Append(value + ",");
}
else
text.Append(value + ",");
text.AppendLine();
}
我遇到了同样的问题并找到了解决方案,您只需添加此方法,您将获得所需的确切文本,而不是数字:
private string ReadExcelCell(Cell cell, WorkbookPart workbookPart)
{
var cellValue = cell.CellValue;
var text = (cellValue == null) ? cell.InnerText : cellValue.Text;
if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString))
{
text = workbookPart.SharedStringTablePart.SharedStringTable
.Elements<SharedStringItem>().ElementAt(
Convert.ToInt32(cell.CellValue.Text)).InnerText;
}
return (text ?? string.Empty).Trim();
}
我正在尝试使用 DocumentFormat.OpenXml 读取上传的 Excel 文件。当我得到文件 (HttpPosteFileWrapper) 时,我只是想读取单元格并将它们写入文本字符串。 (稍后我会做更多,但我现在只是想习惯 OpenXml。)
我在 Excel 中的数据如下所示:
Field1 - Field2 - Phone - City
IT Department - Emp - 7175551234 - Springfield
HR - Emp - 7175556543 - W Springfield
代码如下所示:
var doc = SpreadsheetDocument.Open(file.InputStream, false);
WorkbookPart workbookPart = doc.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData thisSheet = worksheetPart.Worksheet.Elements<SheetData>().First();
System.Text.StringBuilder text = new System.Text.StringBuilder();
foreach (Row r in thisSheet.Elements<Row>())
{
foreach (Cell c in r.Elements<Cell>())
{
text.Append(c.CellValue.Text + ",");
}
text.AppendLine();
}
它创建的字符串如下所示:
49,51,50,0,1,2,3,4,5,6,7,8,9,10,11,12,13,16,14,15,17,18,19,20,21,22,40,41,42,43,44,45,54,\r\n
52,24,23,25,26,27,7306,33,28,29,30,31,17033,32,34,7175555555,7175551234,7175554321,7175550000,35,36,37,36526,40179,38,39,30,31,17033,32,55,\r\n
53,46,47,48,555,\r\n
好像是数值通过了。是因为我使用了错误的流类型吗?
编辑: 我已经将我的代码更新为现在的样子,但它仍然无法正常工作。我好像没办法看到文字数据
public ActionResult ProfileImport(IEnumerable<HttpPostedFileBase> files)
{
// Build file list
int i = 1;
foreach (var file in files)
{
if (file.ContentLength > 0)
{
var doc = SpreadsheetDocument.Open(file.InputStream, false);
WorkbookPart workbookPart = doc.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData thisSheet = worksheetPart.Worksheet.Elements<SheetData>().First();
System.Text.StringBuilder text = new System.Text.StringBuilder();
foreach (Row r in thisSheet.Elements<Row>())
{
foreach (Cell c in r.Elements<Cell>())
{
string value = c.InnerText;
if (c.DataType != null && c.DataType.Value == CellValues.SharedString) // Check DataType exists
{
var stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>()
.FirstOrDefault(); // Get Table parts from workbookPart
if (stringTable != null)
value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
text.Append(value + ",");
}
else
text.Append(value + ",");
}
text.AppendLine();
}
var outText = text.ToString();
}
}
}
文件第一行的实际数据:
地址描述、地址 1、地址 2、城市、州、邮政编码、国家代码、电子邮件、手机、家庭电话、工作电话、传真、组织名称、部门、职位、开始日期、结束日期、组织地址 1、组织地址 2 组织城市、组织状态、组织邮政代码、组织国家代码、关键字
Row.InnerText:
“49515001234567891011121316141517181920212240414243444554”
Row.OuterXml:
"<x:row r=\"1\" spans=\"1:33\" s=\"3\" customFormat=\"1\" x14ac:dyDescent=\"0.25\" xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" xmlns:x=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">
<x:c r=\"A1\" s=\"3\" t=\"s\"><x:v>49</x:v></x:c>
<x:c r=\"B1\" s=\"3\" t=\"s\"><x:v>51</x:v></x:c>
<x:c r=\"C1\" s=\"3\" t=\"s\"><x:v>50</x:v></x:c>
<x:c r=\"D1\" s=\"3\" t=\"s\"><x:v>0</x:v></x:c>
<x:c r=\"E1\" s=\"3\" t=\"s\"><x:v>1</x:v></x:c>
<x:c r=\"F1\" s=\"3\" t=\"s\"><x:v>2</x:v></x:c>
<x:c r=\"G1\" s=\"3\" t=\"s\"><x:v>3</x:v></x:c>
<x:c r=\"H1\" s=\"3\" t=\"s\"><x:v>4</x:v></x:c>
<x:c r=\"I1\" s=\"3\" t=\"s\"><x:v>5</x:v></x:c>
<x:c r=\"J1\" s=\"3\" t=\"s\"><x:v>6</x:v></x:c>
<x:c r=\"K1\" s=\"3\" t=\"s\"><x:v>7</x:v></x:c>
<x:c r=\"L1\" s=\"3\" t=\"s\"><x:v>8</x:v></x:c>
<x:c r=\"M1\" s=\"3\" t=\"s\"><x:v>9</x:v></x:c>
<x:c r=\"N1\" s=\"3\" t=\"s\"><x:v>10</x:v></x:c>
<x:c r=\"O1\" s=\"3\" t=\"s\"><x:v>11</x:v></x:c>
<x:c r=\"P1\" s=\"4\" t=\"s\"><x:v>12</x:v></x:c>
<x:c r=\"Q1\" s=\"4\" t=\"s\"><x:v>13</x:v></x:c>
<x:c r=\"R1\" s=\"3\" t=\"s\"><x:v>16</x:v></x:c>
<x:c r=\"S1\" s=\"3\" t=\"s\"><x:v>14</x:v></x:c>
<x:c r=\"T1\" s=\"3\" t=\"s\"><x:v>15</x:v></x:c>
<x:c r=\"U1\" s=\"3\" t=\"s\"><x:v>17</x:v></x:c>
<x:c r=\"V1\" s=\"3\" t=\"s\"><x:v>18</x:v></x:c>
<x:c r=\"W1\" s=\"3\" t=\"s\"><x:v>19</x:v></x:c>
<x:c r=\"X1\" s=\"3\" t=\"s\"><x:v>20</x:v></x:c>
<x:c r=\"Y1\" s=\"3\" t=\"s\"><x:v>21</x:v></x:c>
<x:c r=\"Z1\" s=\"3\" t=\"s\"><x:v>22</x:v></x:c>
<x:c r=\"AA1\" s=\"3\" t=\"s\"><x:v>40</x:v></x:c>
<x:c r=\"AB1\" s=\"3\" t=\"s\"><x:v>41</x:v></x:c>
<x:c r=\"AC1\" s=\"3\" t=\"s\"><x:v>42</x:v></x:c>
<x:c r=\"AD1\" s=\"3\" t=\"s\"><x:v>43</x:v></x:c>
<x:c r=\"AE1\" s=\"3\" t=\"s\"><x:v>44</x:v></x:c>
<x:c r=\"AF1\" s=\"3\" t=\"s\"><x:v>45</x:v></x:c>
<x:c r=\"AG1\" s=\"3\" t=\"s\"><x:v>54</x:v></x:c>
</x:row>"
看起来这些是共享字符串 table 中字符串的索引。在 Excel 文件格式中,字符串数据存储在共享字符串 table 中,然后在单元格级别进行引用。根据文档,如果数据类型是文本,CellValue
returns StringTable
的索引。
对单元格中的数据类型了解不多(根据数据类型有不同的检索方法)。如果它是我认为的那样,它将是一个 SharedString
,您需要将其作为 SharedStringTablePart 检索,如 MSDN 页面所示:
https://msdn.microsoft.com/en-us/library/hh298534%28v=office.14%29.aspx?f=255&MSPPError=-2147217396
您的代码将如下所示:
foreach (Cell c in r.Elements<Cell>())
{
string value = c.InnerText;
if (c.DataType.Value == CellValues.SharedString)
{
var stringTable = workbookPart.GetPartsOfType<SharedStringTablePart>()
.FirstOrDefault();
if (stringTable != null)
value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
text.Append(value + ",");
}
else
text.Append(value + ",");
text.AppendLine();
}
我遇到了同样的问题并找到了解决方案,您只需添加此方法,您将获得所需的确切文本,而不是数字:
private string ReadExcelCell(Cell cell, WorkbookPart workbookPart)
{
var cellValue = cell.CellValue;
var text = (cellValue == null) ? cell.InnerText : cellValue.Text;
if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString))
{
text = workbookPart.SharedStringTablePart.SharedStringTable
.Elements<SharedStringItem>().ElementAt(
Convert.ToInt32(cell.CellValue.Text)).InnerText;
}
return (text ?? string.Empty).Trim();
}