使用exceldatareader从excel文件中获取数据并将其粘贴到xml文件中?

时间:2018-02-24 15:45:28

标签: c# exceldatareader

我有一些格式为

的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Publishing DTD with OASIS Tables v1.0 20120330//EN" "JATS-journalpublishing-oasis-article1.dtd">
<article article-type="proceedings" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:oasis="http://www.niso.org/standards/z39-96/ns/oasis-exchange/table">
<front>
<journal-meta>
<journal-id journal-id-type="publisher-id"/>
<journal-title-group>
<journal-title>Eleventh International Conference on Correlation Optics</journal-title>
</journal-title-group>
<issn pub-type="epub">0277-786X</issn>
<publisher>
<publisher-name>SPIE</publisher-name>
</publisher>
</journal-meta>
<article-meta>
<article-id pub-id-type="doi">@@</article-id>
<title-group>
<article-title>Comparison of absorption spectra of adenocarcinoma and squamous cell carcinoma cervical tissue</article-title>
</title-group>
<pub-date>
<year>2013</year>
</pub-date>
<volume>9066</volume>
<issue>90661T</issue>
<isbn content-type="conf-proc">978-1-5106-0374-5</isbn>
<fpage>90661T-1</fpage>
<lpage>90661T-7</lpage>
</article-meta>
</front>

我有xlsx文件,如下所示

Filename        Pages       DOI             Publisher   Year    Price
01022018_1020   14    10.1111/12.2033558    Springer    2017    $15.13 
01022018_1021   7     10.1111/12.2033559    Elsevier    2018    $15.25 
01022018_1130   30    10.1113/20.3023658    Elsevier    2017    $19.25 
01022018_1301   10    10.1113/20.3023777    Wiley       2015    $11.50 

我想为每个文件(按文件名搜索)获取字段 DOI 发布商的相应值,并将其更新为xml文件,即文件名称 01022018_1021.xml 然后获得相应的 DOI 发布商 10.1111 / 12.2033559 Elsevier < / em>并将它们放在<publisher-name>Elsevier</publisher-name><article-id pub-id-type="doi">10.1111/12.2033559</article-id>

等节点中

我之前从未使用过exceldatareader,但我尝试过这样的

var xmlFiles=Directory.EnumerateFiles(@"D:\test\testobject","*.xml",SearchOption.AllDirectories);
Parallel.ForEach(xmlFiles,xmlFile=>{
    var name=Path.GetFileName(xmlFile);

    FileStream stream = File.Open(@"C:\Don\Downloads\Download-Sample-File-xlsx.xlsx", FileMode.Open, FileAccess.Read);
    IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

    DataSet result = excelReader.AsDataSet(new ExcelDataSetConfiguration() {
    ConfigureDataTable = (_) => new ExcelDataTableConfiguration() {
    UseHeaderRow = true
    }
    });

    DataTable dt = result.Tables[0];

    foreach (DataRow row in dt.Rows)
        {
        var x = (string)row["Publisher"];
        var y = (string)row["DOI"];
        /*how do I get the required values
        var pub_name="...";
        var doi="...";
        */
        XDocument doc=XDocument.Load(xmlFile);
        doc.Descendants("pub-name").First().Value=pub_name;
        doc.Descendants("article-id").First().Value=doi;
        doc.Save(xmlFile);

        }
    excelReader.Close();
});

Console.WriteLine("Done");
Console.ReadLine();

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

看起来你已经拥有了这些价值。

reference URL:- [https://www.androidhive.info/2011/11/android-sqlite-database-tutorial/][1]

编辑:我没有理解需要找到合适的行。

doc.Descendants("pub-name").First().Value=x;
doc.Descendants("article-id").First().Value=y;