ClosedXML表未更新

时间:2018-02-17 09:11:21

标签: c# closedxml

我正在尝试使用ClosedXML更新C#中的工作表,但似乎工作表没有更新。

public string FeedAndFetchValueFromThirdSheet(List<string> listValueColl, IXLWorksheet worksheetThird)
{
    int posTemp = worksheetThird.RowsUsed().Count(); // Value here is 1

    string value = "";
    foreach (var obj in listValueColl)
    {
        posTemp++;
        worksheetThird.Cell(posTemp, 1).InsertData(obj);
    }
    int posUpdated = worksheetThird.RowsUsed().Count(); //After updating the sheet the value still remain 1
    value = "A"+ (posTemp - listValueColl.Count()) +":A" + posTemp;
    return value;
}

1 个答案:

答案 0 :(得分:1)

ClosedXML的InsertData()方法使用任何IList<T>作为输入,而不是字符串或类似对象。 因此,只需使用List<string>string[]数组作为要插入的数据的容器。

更新的方法:

public string FeedAndFetchValueFromThirdSheet(List<string> listValueColl, IXLWorksheet worksheetThird)
{
    int posTemp = worksheetThird.RowsUsed().Count(); // Value here is 1

    string value = "";
    foreach (var obj in listValueColl)
    {
        posTemp++;


        // Use IList (simple array, list, etc.) as container for data, 
        // that you want to insert.
        string[] rowDataToInsert = { obj };

        // Insert created array (not a string).
        worksheetThird.Cell(posTemp, 1).InsertData(rowDataToInsert);


    }
    int posUpdated = worksheetThird.RowsUsed().Count(); //After updating the sheet the value still remain 1
    value = "A" + (posTemp - listValueColl.Count()) + ":A" + posTemp;
    return value;

}