我正在尝试将一个新列附加到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
}
}
}
任何有关如何解决此问题的帮助都会非常有帮助!谢谢!
答案 0 :(得分: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>);
}