Spreadsheets
This section provides how-to topics for working with spreadsheet documents using the Open XML SDK 2.0 for Microsoft Office.
How to: Calculate the Sum of a Range of Cells in a Spreadsheet Document
How to: Create a Spreadsheet Document by Providing a Filename
How to: Delete Text from a Cell in a Spreadsheet Document
How to: Get a Column Heading in a Spreadsheet Document
How to: Get Worksheet Information from an Open XML Package
How to: Insert a Chart into a Spreadsheet Document
How to: Insert a New Worksheet into a Spreadsheet Document
How to: Insert Text into a Cell in a Spreadsheet Document
How to: Merge Two Adjacent Cells in a Spreadsheet Document
How to: Open a Spreadsheet Document for Read-only Access
How to: Open a Spreadsheet Document from a Stream
How to: Parse and Read a Large Spreadsheet Document
How to: Retrieve a Dictionary of All Named Ranges in a Spreadsheet Document
How to: Retrieve a List of the Hidden Rows or Columns in a Spreadsheet Document
How to: Retrieve a List of the Hidden Worksheets in a Spreadsheet Document
How to: Retrieve the Values of Cells in a Spreadsheet Document
I Adopted an example that explains what you need to create a simple excel file (open xml format)
The original source of example is here
How to add/manipulate multiple worksheets in workbook using DocumentFormat
http://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/thread/cf14e989-e52c-4af0-a260-22a6c6ad4ddd/
Note: This code generates 3 sheets, it is easy if you want to have more or less sheets in your excel file
Sample Code
// Adds child parts and generates content of the specified part.
public void CreateWorkbookPart(WorkbookPart part) {
// Increment Sheet Id (Excel 2010 uses rld for id prefix) rld1, rld2 ...
WorksheetPart worksheetPart1 = part.AddNewPart<WorksheetPart>("rId1");
GenerateWorksheetPartContent(worksheetPart1);
WorksheetPart worksheetPart2 = part.AddNewPart<WorksheetPart>("rId2");
GenerateWorksheetPartContent(worksheetPart2);
WorksheetPart worksheetPart3 = part.AddNewPart<WorksheetPart>("rId3");
GenerateWorksheetPartContent(worksheetPart3);
GeneratePartContent(part);
}
// Generates content of part.
private void GeneratePartContent(WorkbookPart workbookPart) {
Workbook workbook = new Workbook();
Sheets sheets = new Sheets();
// Increment SheetId and Id (Excel 2010 uses rld for id prefix) rld1, rld2 ...
Sheet sheet1 = new Sheet() { Name = "Sheet1", SheetId = 1, Id = "rId1" };
Sheet sheet2 = new Sheet() { Name = "Sheet2", SheetId = 2, Id = "rId2" };
Sheet sheet3 = new Sheet() { Name = "Sheet3", SheetId = 3, Id = "rId3" };
sheets.Append(sheet1);
sheets.Append(sheet2);
sheets.Append(sheet3);
workbook.Append(sheets);
workbookPart.Workbook = workbook;
}
// Generates content of worksheetPart2.
private void GenerateWorksheetPartContent(WorksheetPart worksheetPart) {
Worksheet worksheet = new Worksheet();
SheetData sheetData = new SheetData();
// Change RowIndex for rows 1,2,3,4
Row row = new Row() { RowIndex = 1 };
// Change CellReference for next cells A2, A3 ... B1, B2 ...
// Change DataType to match your data type and
// cellValue.Text below to match type mentioned here
// e.g. for CellValues.Number, cellValue.Text = "12.34"
Cell cell = new Cell() { CellReference = "A1", DataType = CellValues.String };
CellValue cellValue = new CellValue();
// Cell Text
cellValue.Text = "Some Text";
cell.Append(cellValue);
row.Append(cell);
sheetData.Append(row);
worksheet.Append(sheetData);
worksheetPart.Worksheet = worksheet;
}
int createExcelFile(string FileName) {
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Create(FileName, SpreadsheetDocumentType.Workbook)) {
WorkbookPart workbookpart = spreadSheet.AddWorkbookPart();
CreateWorkbookPart(workbookpart);
workbookpart.Workbook.Save();
}
}
- 7/14/2011
- Kevin M Shanahan
- 7/14/2011
- Kevin M Shanahan
- 3/8/2011
- EricStob
This is the only documentation that you're able to provide to work with this things?
Did you ever try to use it or what?
Are you able to write two f****ing more words about how to use it or what?
- 2/2/2011
- Stefano Paluello
This is extremely confusing.
What is the difference between a Sheet, a Worksheet, and a WorksheetPart?
How do I make a Workbook with two worksheets in it?
- 1/7/2011
- EricStob
- 7/15/2010
- jorgeesteban