[英]Reading an uploaded Excel file
I am building a quick proof of concept project to show the ability to parse an excel file. 我正在构建一个概念快速证明项目,以展示解析excel文件的能力。 Based on Microsoft documentation ( How to parse and read a large spreadsheet document ), it seems the sdk of choice is
Open Xml
. 根据Microsoft文档( 如何解析和阅读大型电子表格文档 ),似乎首选的SDK是
Open Xml
。
The proof of concept gives a basic form for a user to upload a file (.xlsx). 概念验证为用户上传文件(.xlsx)提供了一种基本形式。 The controller reads the file and spits back the content.
控制器读取文件并吐回内容。 I am struggling to grab the value of the cell, instead, it seems like I am only able to get some sort of identifier or reference to the text.
我正在努力获取单元格的值,相反,似乎只能获得某种标识符或对文本的引用。 Here is my code with some examples
这是我的代码和一些例子
View 视图
@using(Html.BeginForm("Index", "Home", FormMethod.Post, new{ enctype="multipart/form-data" } ))
{
<input type="file" name="file"/>
<input type="submit"/>
}
<br/>
@Html.Raw(ViewBag.Text)
Action 行动
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
ViewBag.Text = "";
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(file.InputStream, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
string text;
foreach (Row r in sheetData.Elements<Row>())
{
foreach (Cell c in r.Elements<Cell>())
{
text = c.CellValue.Text;
ViewBag.Text += text + ", ";
}
ViewBag.Text += "<br />";
}
}
return this.View();
}
Excel File Excel文件
| | Hello |
你好 World |
世界|
| | World |
世界| Hello |
你好
Output 产量
0, 1, 0、1
1, 0, 1、0,
As you can see, the 0 represents "Hello" and the 1 represents "World". 如您所见,0代表“ Hello”,而1代表“ World”。 I've tested this with a larger data set and have confirmed that identical words have the same value when printed to the screen.
我已经使用较大的数据集对此进行了测试,并确认当打印到屏幕上时相同的单词具有相同的值。
The example is pretty much copy/pasted from the MS website. 该示例几乎是从MS网站复制/粘贴的。 I've tried accessing other
c.CellValue
properties, such as InnerText
and InnerXml
only to get the same results. 我尝试访问其他
c.CellValue
属性,例如InnerText
和InnerXml
只是为了获得相同的结果。 What am I doing wrong? 我究竟做错了什么? Is
Open XML
is good SDK to use for this purpose? Open XML
是用于此目的的良好SDK吗?
Use this method to get the exact value of the cell instead of numbers: 使用此方法获取单元格的确切值,而不是数字:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.