[英]How to add new row with cell data and styles in Excel using OpenXML and C#
[英]Excel C# OpenXML : how can i get the position of the cell in excel after adding new row,(Row row=new Row()); without using excel Interpol or Macro
我的問題是關於在不使用Excel Interpol或Macro的情況下獲取單元格在excel中的位置。 只是C#和OpenXML。 位置類似(A3)。
當您擁有單元格對象時,請執行以下操作以獲取字符串“ A1”:
myCell.CellReference.Value
如果要通過引用查找單元格,請執行以下操作:
public static Cell getCellByReference(string cellReference, Worksheet ws)
{
return ws.Descendants<Cell>().Where(c => c.CellReference.Value == cellReference).FirstOrDefault();
}
Cell myCell = getCellByReference("A1", actualWorksheet)
有一本很有幫助的電子書,名為《 Open XML Explained》 ,它是在不久前(2007年?)寫的。 它解釋了OpenXML規范的許多復雜細節。
電子書說,有兩種確定單元格位置的方法:具有CellRefernece屬性或不具有CellReference屬性。
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> (from p.60)
<sheetData>
<row r="5">
<c r="E5">
<v>1234</v>
</c>
</row>
</sheetData>
</worksheet>
這是較簡單的情況。 當您讀取單元格數據時,可能會像上面的Cell和Row標簽的r
屬性中那樣存在一個CellReference屬性。 如果是這樣,則此CellReferernce屬性是形式為“ A1”或“ G32”的字符串,其中字母表示列,數字表示行。 上面的單元格具有“ E5”的CellReference,它對應於電子表格的E列第5行。 您將需要讀取此值以確定單元格的位置。 這是一個SO問答 ,可以幫助您解析CellReference字符串並獲取列索引以幫助確定位置。
<worksheet xmlns="http://.../spreadsheetml/2006/main"> (see p. 58)
<sheetData>
<row>
<c>
<v>42</v>
</c>
</row>
</sheetData>
</worksheet>
像上面的xml中一樣,不能保證CellRefence屬性。 如果不存在CellReference,則需要通過文件中數據的位置來確定位置。 因此,第一行數據對應於電子表格的第一行。 與單元格在一行中的位置相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.