0

我正在尝试在 excel 文件的开头附加一个新列,然后向这些单元格添加值(1,2,3,4,5...),它将用作我的数据库的主 ID。我在尝试将数据插入列时遇到了麻烦,尽管我认为这很容易。这是我目前拥有的:

private void cleanUpCollection(string file)
  {
      using (SpreadsheetDocument doc =
              SpreadsheetDocument.Open(file, false))
      {
          Worksheet worksheet =
              (Worksheet)doc.WorkbookPart.GetPartById("rId1");

          Range rng = worksheet.get_Range("A1", Missing.Value);

          rng.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
                                  XlInsertFormatOrigin.xlFormatFromRightOrBelow);

          WorksheetPart worksheetpart =
                 (WorksheetPart)doc.WorkbookPart.GetPartById("rId1");
          foreach (var row in worksheetpart.Rows())
          {
             // My assumption was to go down each row and insert into the first column
          }
      }



  }

有关如何解决此问题的任何帮助都将非常有帮助!谢谢你!

4

1 回答 1

0

尝试这个:

var sheetData = workSheet.Worksheet.GetFirstChild<SheetData>();
Column firstColumn = sheetData.Elements<Column>().First();
var newCol = firstColumn.CloneNode(true) as Column;
firstColumn.InsertBeforeSelf<Column>(newCol);

foreach(Cell cell in newCol.Elements<Cell>())
{
    cell.Cellvalue = new CellValue(<Value>);
}
于 2013-07-29T14:08:39.980 回答