我已经预先定义了excel格式我需要将数据传递给excel。我能够获取特定的表。但是不知道如何将数据传递给单元格。
var excelDocument = new ExcelDocument();
var fileName = Guid.NewGuid();
string filePath = HttpContext.Current.Server.MapPath("~/Uploads/TemplateFiles/test.xlsx");
using (SpreadsheetDocument document =
SpreadsheetDocument.Open(filePath, false))
{
WorkbookPart workbookPart = document.WorkbookPart;
Workbook workbook = document.WorkbookPart.Workbook;
string sheetName = workbookPart.Workbook.Descendants<Sheet>().ElementAt(1).Name;
IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Census Template for Import");
if (sheets.Count() == 0)
{
// The specified worksheet does not exist.
return null;
}
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.First().Id);
SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
var excelRows = sheetData.Descendants<DocumentFormat.OpenXml.Spreadsheet.Row>().ToList();
int rowindex = 10;
foreach (var item in census)
{
//how to write the data in cell
rowindex++;
}
worksheetPart.Worksheet.Save();
workbookPart.Workbook.Save();
document.Close();
//worksheetPart.Worksheet.Save();
}
return filePath;
答案 0 :(得分:2)
无法判断您的新文件是创建还是追加到现有文件中,但是:
spreadSheet.WorkbookPart.WorksheetParts.First().Worksheet.First().AppendChild(new Row());
sheet.First().Last().AppendChild(new Cell() { CellValue = new CellValue("test") });
应该适用于这两种情况,但新单元格将放在第一张表格中的最后一个活动行上。
答案 1 :(得分:2)
当您知道行索引和列索引时,如果单元格不存在,则获取单元格或添加新单元格的方法。
请注意:
RowIndex
的属性Row
CellReference
的属性Cell
应在创建单元格期间初始化如果RowIndex
或CellReference
为null,则抛出NullReferenceException。
private Cell InsertCell(uint rowIndex, uint columnIndex, Worksheet worksheet)
{
Row row = null;
var sheetData = worksheet.GetFirstChild<SheetData>();
// Check if the worksheet contains a row with the specified row index.
row = sheetData.Elements<Row>().FirstOrDefault(r => r.RowIndex == rowIndex);
if (row == null)
{
row = new Row() { RowIndex = rowIndex };
sheetData.Append(row);
}
// Convert column index to column name for cell reference.
var columnName = GetExcelColumnName(columnIndex);
var cellReference = columnName + rowIndex; // e.g. A1
// Check if the row contains a cell with the specified column name.
var cell = row.Elements<Cell>()
.FirstOrDefault(c => c.CellReference.Value == cellReference);
if (cell == null)
{
cell = new Cell() { CellReference = cellReference };
if (row.ChildElements.Count < columnIndex)
row.AppendChild(cell);
else
row.InsertAt(cell, (int)columnIndex);
}
return cell;
}
Here您会找到GetExcelColumnName()
方法的代码。
答案 2 :(得分:0)
我遇到了同样的问题和本文How to: Insert text into a cell in a spreadsheet document (Open XML SDK)。我想你需要在你的工作表中插入一个新的Cell对象,然后将指定的数据(假设它是一个字符串或者它已经被强制转换为字符串)插入到该单元格中。
答案 3 :(得分:0)
似乎您定义了rowindex = 10,有两种添加行的方法。 如果第10行是excel中的最后一行,那么您可以简单地添加新行,例如:
foreach (var item in census)
{
//how to write the data in cell
Row row = new Row();
row.RowIndex = (UInt32)rowindex;
Cell cell = new Cell()
{
DataType = CellValues.String,
CellValue = new CellValue("value")
};
row.Append(cell);
sheetData.Append(row);
rowindex++;
}
如果第10行之后有行,则必须使用insert,然后手动更改第10行之后的行和单元格 索引到正确的索引值,例如:
foreach (var item in census)
{
//how to write the data in cell
Row refRow = GetRow(sheetData, rowIndex);
++rowIndex;
Cell cell1 = new Cell() { CellReference = "A" + rowIndex };
CellValue cellValue1 = new CellValue();
cellValue1.Text = "";
cell1.Append(cellValue1);
Row newRow = new Row()
{
RowIndex = rowIndex
};
newRow.Append(cell1);
for (int i = (int)rowIndex; i <= sheetData.Elements<Row>().Count(); i++)
{
var row = sheetData.Elements<Row>().Where(r => r.RowIndex.Value == i).FirstOrDefault();
row.RowIndex++;
foreach (Cell c in row.Elements<Cell>())
{
string refer = c.CellReference.Value;
int num = Convert.ToInt32(Regex.Replace(refer, @"[^\d]*", ""));
num++;
string letters = Regex.Replace(refer, @"[^A-Z]*", "");
c.CellReference.Value = letters + num;
}
}
sheetData.InsertAfter(newRow, refRow);
rowindex++;
}
static Row GetRow(SheetData wsData, UInt32 rowIndex)
{
var row = wsData.Elements<Row>().
Where(r => r.RowIndex.Value == rowIndex).FirstOrDefault();
if (row == null)
{
row = new Row();
row.RowIndex = rowIndex;
wsData.Append(row);
}
return row;
}
这是一个原型。您可能需要更改一些代码或变量名称以适合您的项目。
参考文献: