admin管理员组

文章数量:1206015

I'm trying to replace the value in column A of an excel with the concatenated string from column A and B combined. I'm looping through the rows to do this but I always get that the string is not in the correct format.

Loop is below:

foreach (Row r in rows.Skip(1))
{
    string x = r.Elements<Cell>().ElementAt(0).InnerText;
    string y = r.Elements<Cell>().ElementAt(1).InnerText;

    string xy = x + '_' + y;

    r.Elements<Cell>().ElementAt(0).CellValue = new CellValue(xy);
}

If I replace the last line with either

r.Elements<Cell>().ElementAt(0).CellValue = new CellValue(y);
r.Elements<Cell>().ElementAt(0).CellValue = new CellValue(x);

It works as I'd expected however when joining the strings it doesn't.

I also tried the below:

foreach (Row r in rows.Skip(1))
{
    string x = r.Elements<Cell>().ElementAt(0).InnerText;
    string y = r.Elements<Cell>().ElementAt(1).InnerText;

    string xy = x + '_' + y;

    Cell cell = r.Elements<Cell>().ElementAt(0);
    cell.CellValue = new CellValue(xy);
    cell.DataType = new EnumValue<CellValues>(CellValues.String);
}

This runs but gives me numbers concatenated together rather than the strings I can see when opening the file.

本文标签: cCombining strings in excel Using DocumentFormatOpenXmlStack Overflow