如何使用 OpenXML 将值写入 excel 文件中的必需列

How to write values to an required column in excel file using OpenXML

我必须使用 Open XML 将大约 15000 行写入 excel 文件中的指定列。我对此很陌生,所以请帮忙。我在指定要向 excel 添加值的列时遇到困难。我找到了一些代码可以写入 excel 但如何选择指定的列。

static Cell AddCellWithText(string text) 
{ 
    Cell c1 = new Cell(); 
    c1.DataType = CellValues.InlineString;

    InlineString inlineString = new InlineString(); 
    Text t = new Text(); 
    t.Text = text; 
    inlineString.AppendChild(t);

    c1.AppendChild(inlineString);

    return c1; 
}

我自己还没有尝试过,但是查看文档 here 表明以下应该有效。

static Cell AddCellWithText(string text) 
{ 
    Cell c1 = new Cell();
    c1.CellReference = "B2"; //The required position of cell in excel grid
    c1.DataType = CellValues.InlineString;

    InlineString inlineString = new InlineString(); 
    Text t = new Text(); 
    t.Text = text; 
    inlineString.AppendChild(t);

    c1.AppendChild(inlineString);

    return c1; 
}

要使上述工作正常,row.RowIndex 必须与 cell.CellReference 中给出的相应行相匹配。我的意思是,如果 RowIndex = 1,那么您可以将 CellReference 设置为 A1、B1、C1 等,对于 RowIndex = 2,CellReference 必须是 A2、B2、C2 等。