If somebody like the post and its helpful in your work then, add comments.

Monday, August 3, 2009

SpreadsheetML: Updating a range in the Excel Sheet

The below code update the cell rane in the excel sheet.

#region Approch 1

#region Calculate Sum of Cell Range

private static void CalculateSumOfCellRange(string docName, string worksheetName, string resultCell)
{
// Open the document for editing.
using (SpreadsheetDocument document = SpreadsheetDocument.Open(docName, true))
{
IEnumerable sheets = document.WorkbookPart.Workbook.Descendants().Where(s => s.Name == worksheetName);
if (sheets.Count() == 0)
{
// The specified worksheet does not exist.
return;
}

WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.First().Id);
Worksheet worksheet = worksheetPart.Worksheet;

#region Insert Cell Values
Cell result1 = InsertCellInWorksheet(GetColumnName("B4"), GetRowIndex(resultCell), worksheetPart);
Cell result2 = InsertCellInWorksheet(GetColumnName("C4"), GetRowIndex(resultCell), worksheetPart);
Cell result3 = InsertCellInWorksheet(GetColumnName("D4"), GetRowIndex(resultCell), worksheetPart);
Cell result4 = InsertCellInWorksheet(GetColumnName("E4"), GetRowIndex(resultCell), worksheetPart);
Cell result5 = InsertCellInWorksheet(GetColumnName("F4"), GetRowIndex(resultCell), worksheetPart);
Cell result6 = InsertCellInWorksheet(GetColumnName("G4"), GetRowIndex(resultCell), worksheetPart);
Cell resultSum = InsertCellInWorksheet(GetColumnName("J4"), GetRowIndex(resultCell), worksheetPart);

result1.DataType = new EnumValue(CellValues.Number);
result1.CellValue = new CellValue("1000");

result1.DataType = new EnumValue(CellValues.Number);
result1.CellValue = new CellValue("20000");

result2.DataType = new EnumValue(CellValues.Number);
result2.CellValue = new CellValue("100000");

result3.DataType = new EnumValue(CellValues.Number);
result3.CellValue = new CellValue("145000");

result4.DataType = new EnumValue(CellValues.Number);
result4.CellValue = new CellValue("301200000");

result5.DataType = new EnumValue(CellValues.Number);
result5.CellValue = new CellValue("440000");

result6.DataType = new EnumValue(CellValues.Number);
result6.CellValue = new CellValue("400000");

resultSum.CellFormula = new CellFormula("=sum(B4:G4)");
#endregion


worksheetPart.Worksheet.Save();
document.Close();
}
}

#endregion

#region Get Row Index in Sheet
// Given a cell name, parses the specified cell to get the row index.
private static uint GetRowIndex(string cellName)
{
// Create a regular expression to match the row index portion the cell name.
Regex regex = new Regex(@"\d+");
Match match = regex.Match(cellName);

return uint.Parse(match.Value);
}

#endregion

#region Get Column Name in Sheet
// Given a cell name, parses the specified cell to get the column name.
private static string GetColumnName(string cellName)
{
// Create a regular expression to match the column name portion of the cell name.
Regex regex = new Regex("[A-Za-z]+");
Match match = regex.Match(cellName);

return match.Value;
}

#endregion

#region Insert a Cell into a Worksheet
// Given a column name, a row index, and a WorksheetPart, inserts a cell into the worksheet.
// If the cell already exists, returns it.
private static Cell InsertCellInWorksheet(string columnName, uint rowIndex, WorksheetPart worksheetPart)
{
Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild();
string cellReference = columnName + rowIndex;

// If the worksheet does not contain a row with the specified row index, insert one.
Row row;
if (sheetData.Elements().Where(r => r.RowIndex == rowIndex).Count() != 0)
{
row = sheetData.Elements().Where(r => r.RowIndex == rowIndex).First();
}
else
{
row = new Row() { RowIndex = rowIndex };
sheetData.Append(row);
}

// If there is not a cell with the specified column name, insert one.
if (row.Elements().Where(c => c.CellReference.Value == columnName + rowIndex).Count() > 0)
{
return row.Elements().Where(c => c.CellReference.Value == cellReference).First();
}
else
{
// Cells must be in sequential order according to CellReference. Determine where to insert the new cell.
Cell refCell = null;
foreach (Cell cell in row.Elements())
{
if (string.Compare(cell.CellReference.Value, cellReference, true) > 0)
{
refCell = cell;
break;
}
}

Cell newCell = new Cell() { CellReference = cellReference };
row.InsertBefore(newCell, refCell);

worksheet.Save();
return newCell;
}
}
#endregion

#endregion

I leverage the msdn link for to update cell range in the excel sheet.

No comments:

Post a Comment