我有一个非常大的Excel文件,我需要在每行附加100个新单元格。 因为OpenXML DOM方法给我一个OutOfMemory Exception,所以我需要为我的项目使用OpenXmlWriter。
有人可以告诉我如何使用OpenXmlWriter将单元格附加到行中?
这是我使用DOM方法的代码:
int nRowCounter = 0;
foreach (Row row in sheetData.Elements<Row>())
{
// skip first row
nRowCounter++;
if (nRowCounter == 1)
continue;
string uniqueID = row.Elements<Cell>().First().InnerText;
string[] branchenOfId = crmConnector.GetBranchenFromCrm(uniqueID, "");
if (listSelectedBranchen.Any() && !branchenOfId.Intersect(listSelectedBranchen).Any() && nBranchenIndex == 1)
{
rowsToRemove.Add(row.RowIndex.Value);
continue;
}
string cellValue = "";
if (branchenOfId.Contains(strName))
cellValue = strName;
row.Append(new Cell() { DataType = CellValues.String, CellValue = new CellValue(cellValue) });
}
答案 0 :(得分:0)
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(filename, true))
{
WorkbookPart workBookPart = spreadsheetDocument.WorkbookPart;
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
//You can retrieve a specific sheet also by the following code
IEnumerable<Sheet> sheets = workbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Sheet Name");
//find the first row in the sheet data
Row row1 = sheetData.GetFirstChild<Row>();
//create a new cell
//I is the column and 1 is the Row Index
// I assume you know how to get the last column of any row.
//Just get the next alphabet letter and add the row index to make a CellReference.
//e.g. If last cell of the Row is H, you can get the next letter i-e I
Cell cell = new Cell() { CellReference = "I1" };
CellFormula cellformula = new CellFormula();
cellformula.Text = "IF(A2 = A1,1,0)";
cell.Append(cellformula);
//append the cell to the row
row1.Append(cell);
}
另请参阅this文档链接