C# and XML spreadsheet simple question

久未见 提交于 2019-12-24 12:35:04

问题


Hello guys I have 1675 line Code. Want to when user click button textbox.text insert into this xml spreadsheet and then create on desktop. does it possible? How can I insert text in current xml spreadsheet row?

For example:

  ` <Cell ss:StyleID="s38"/>
    <Cell ss:StyleID="s39">
    <Data ss:Type="String">ვაშლიჯვარი</Data>
    </Cell>
    <Cell ss:StyleID="s36"/>`

Please help me, advice something its very important for me or give some good tutorials.


回答1:


You can open the Excel XML file using LINQ to XML. You can the modify the XML DOM as you require. You will have to correlate cells in the Excel worksheet with XML elements. You can then save the modified XML DOM to create a new and modified Excel XML file.

Here is a small example to get you going:

var xDocument = XDocument.Load("EL-Zednadebi.xml");
// XML elements are in the "ss" namespace.
XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
var rows = xDocument.Root.Element(ss + "Worksheet")
  .Element(ss + "Table").Elements(ss + "Row");
// Row 12.
var row = rows.ElementAt(11);
var cells = row.Elements(ss + "Cell");
// Column U.
var cell = cells.ElementAt(20);
var data = cell.Element(ss + "Data");
// Replace the text in the cell.
data.Value = "Martin Liversage";
xDocument.Save("EL-Zednadebi-2.xml");

This code assumes the following simplified XML:

<Workbook>
  ...
  <Worksheet>
    <Table>
       ...
       <Row> <-- Row at index 11
         ...
         <Cell> <-- Column at index 20
           <Data>ვაშლიჯვარი</Data>
         <Cell>
         ...
       </Row>
       ...
    </Table>
    ...
  </Worksheet>
  ...
</Workbook>


来源:https://stackoverflow.com/questions/4184849/c-sharp-and-xml-spreadsheet-simple-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!