using DevExpress.Spreadsheet;
// ...
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Cell cellA1 = worksheet[0, 0]; // Cell A1
Cell cellB2 = worksheet.Cells["B2"]; // Cell B2
Cell cellC3 = worksheet.Cells[2, 2]; // Cell C3
Cell cellD4 = worksheet.Rows[3][3]; // Cell D4
Cell cellE5 = worksheet.Rows[4]["E"]; // Cell E5
Cell cellF6 = worksheet.Rows["6"][5]; // Cell F6
Cell cellG7 = worksheet.Columns[6][6]; // Cell G7
Cell cellH8 = worksheet.Columns["H"][7]; // Cell H8
Cell cellI9 = worksheet.Columns["I"]["9"]; // Cell I9
// Access a cell from the range of cells.
Cell cellB5 = worksheet.Range["B3:D8"][6]; // Cell B5
Cell cellD6 = worksheet.Range["B3:D8"][3, 2]; // Cell D6
using DevExpress.Spreadsheet;
// ...
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
// A range that includes cells from the top left cell (A1) to the bottom right cell (B5).
Range rangeA1B5 = worksheet["A1:B5"];
// A rectangular range that includes cells from the top left cell (C4) to the bottom right cell (E7).
Range rangeC4E7 = worksheet.Range["C4:E7"];
// The C4:E7 cell range located in the "Sheet3" worksheet.
Range rangeSheet3C4E7 = workbook.Range["Sheet3!C4:E7"];
// A range that contains a single cell (E7).
Range rangeE7 = worksheet.Range["E7"];
// A range that includes the entire column A.
Range rangeColumnA = worksheet.Range["A:A"];
// A range that includes the entire row 5.
Range rangeRow5 = worksheet.Range["5:5"];
// A minimal rectangular range that includes all listed cells: C6, D9 and E7.
Range rangeC6D9E7 = worksheet.Range.Parse("C6:D9:E7");
// A rectangular range whose left column index is 0, top row index is 0,
// right column index is 3 and bottom row index is 2. This is the A1:D3 cell range.
Range rangeA1D3 = worksheet.Range.FromLTRB(0, 0, 3, 2);
// A range that includes the intersection of two ranges: C5:E10 and E9:G13.
// This is the E9:E10 cell range.
Range rangeE9E10 = worksheet.Range["C5:E10 E9:G13"];
// Create a defined name for the D20:G23 cell range.
worksheet.DefinedNames.Add("Range_Name", "Sheet1!$D$20:$G$23");
// Access a range by its defined name.
Range rangeD20G23 = worksheet.Range["Range_Name"];
using DevExpress.Spreadsheet;
// ...
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
// Insert a cell into the C5 position, shifting other cells in the same row to the right.
worksheet.InsertCells(worksheet.Cells["C5"], InsertCellsMode.ShiftCellsDown);
// Insert cells into the location of the H11:I12 range, shifting other cells in the same column down.
worksheet.InsertCells(worksheet.Range["H11:I12"], InsertCellsMode.ShiftCellsRight);
using DevExpress.Spreadsheet;
// ...
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
// Delete the C5 cell, shifting other cells in the same row to the left.
worksheet.DeleteCells(worksheet.Cells["C5"], DeleteMode.ShiftCellsLeft);
// Delete the H11:I12 range of cells, shifting other cells in the same column up.
worksheet.DeleteCells(worksheet.Range["H11:I12"], DeleteMode.ShiftCellsUp);
Worksheet worksheet = workbook.Worksheets[0];
// Create a range.
Range rangeB3D6 = worksheet.Range["B3:D6"];
// Specify the name for the created range.
rangeB3D6.Name = "rangeB3D6";
// Create a new defined name with the specifed range name and absolute reference.
DefinedName definedName = worksheet.DefinedNames.Add("rangeB17D20", "Sheet1!$B$17:$D$20");
// Create a range using the specified defined name.
Range B17D20 = worksheet.Range[definedName.Name];
// Add data of different types to cells.
worksheet.Cells["B1"].Value = DateTime.Now;
worksheet.Cells["B2"].Value = Math.PI;
worksheet.Cells["B3"].Value = "Have a nice day!";
worksheet.Cells["B4"].Value = CellValue.ErrorReference;
worksheet.Cells["B5"].Value = true;
worksheet.Cells["B6"].Value = float.MaxValue;
worksheet.Cells["B7"].Value = 'a';
worksheet.Cells["B8"].Value = Int32.MaxValue;
// Fill all cells in the range with 10.
worksheet.Range["B10:E10"].Value = 10;
//待续
// Create a hyperlink to a web page.
Cell cell = worksheet.Cells["A1"];
worksheet.Hyperlinks.Add(cell, "http://www.devexpress.com/", true, "DevExpress");
// Create a hyperlink to a cell range in a workbook.
Range range = worksheet.Range["C3:D4"];
Hyperlink cellHyperlink = worksheet.Hyperlinks.Add(range, "Sheet2!B2:E7", false, "Select Range");
cellHyperlink.TooltipText = "Click Me";
using DevExpress.Spreadsheet;
//...
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
//Get the system username.
string author = workbook.CurrentAuthor;
//Add a comment to the A1 cell.
Cell cell = worksheet.Cells["A1"];
Comment comment = worksheet.Comments.Add(cell, author, "This is important information for users.");
//Add the author name at the beginning of the comment.
CommentRunCollection runs = comment.Runs;
runs.Insert(0, author + ": \r\n");
runs[0].Font.Bold = true;
//Format the comment text.
runs[1].Font.Color = Color.Red;
runs[1].Font.Name = "Times New Roman";
runs[1].Font.Size = 14;
runs[1].Font.Italic = true;
//Add a new comment run.
runs.Add("\n Never delete this comment!");
runs[2].Font.Color = Color.MidnightBlue;
// Remove all cell information (content, formatting, hyperlinks and comments).
worksheet.Clear(worksheet["C2:D2"]);
// Remove cell content.
worksheet.ClearContents(worksheet["C3"]);
worksheet["D3"].Value = null;
// Remove cell formatting.
worksheet.ClearFormats(worksheet["C4"]);
worksheet["D4"].Style = workbook.Styles.DefaultStyle;
// Remove hyperlinks from cells.
worksheet.ClearHyperlinks(worksheet["C5"]);
Hyperlink hyperlinkD5 = worksheet.Hyperlinks.GetHyperlinks(worksheet["D5"])[0];
worksheet.Hyperlinks.Remove(hyperlinkD5);
// Remove comments from cells.
worksheet.ClearComments(worksheet["C6"]);
Comment commentD6 = worksheet.Comments.GetComments(worksheet["D6"])[0];
worksheet.Comments.Remove(commentD6);
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Columns["A"].WidthInCharacters = 32;
worksheet.Columns["B"].WidthInCharacters = 20;
Style style = workbook.Styles[BuiltInStyleId.Input];
// Specify the content and formatting for a source cell.
worksheet.Cells["A1"].Value = "Source Cell";
Cell sourceCell = worksheet.Cells["B1"];
sourceCell.Formula = "= PI()";
sourceCell.NumberFormat = "0.0000";
sourceCell.Style = style;
sourceCell.Font.Color = Color.Blue;
sourceCell.Font.Bold = true;
sourceCell.Borders.SetOutsideBorders(Color.Black, BorderLineStyle.Thin);
// Copy all information from the source cell to the "B3" cell.
worksheet.Cells["A3"].Value = "Copy All";
worksheet.Cells["B3"].CopyFrom(sourceCell);
// Copy only the source cell content (e.g., text, numbers, formula calculated values) to the "B4" worksheet.Cells["A4"].Value = "Copy Values";
worksheet.Cells["B4"].CopyFrom(sourceCell, PasteSpecial.Values);
// Copy the source cell content (e.g., text, numbers, formula calculated values)
// and number formats to the "B5" cell.
worksheet.Cells["A5"].Value = "Copy Values and Number Formats";
worksheet.Cells["B5"].CopyFrom(sourceCell, PasteSpecial.Values | PasteSpecial.NumberFormats);
// Copy only the formatting information from the source cell to the "B6" cell.
worksheet.Cells["A6"].Value = "Copy Formats";
worksheet.Cells["B6"].CopyFrom(sourceCell, PasteSpecial.Formats);
// Copy all information from the source cell to the "B7" cell except for border settings.
worksheet.Cells["A7"].Value = "Copy All Except Borders";
worksheet.Cells["B7"].CopyFrom(sourceCell, PasteSpecial.All & ~PasteSpecial.Borders);
// Copy information only about borders from the source cell to the "B8" cell.
worksheet.Cells["A8"].Value = "Copy Borders";
worksheet.Cells["B8"].CopyFrom(sourceCell, PasteSpecial.Borders);
// Merge cells contained in the range.
worksheet.MergeCells(worksheet.Range["A1:C5"]);
// Split cells that were previously merged.
worksheet.UnMergeCells(worksheet.Range["A1:C5"]);
foreach (var item in worksheet.Cells.GetMergedRanges()) {
item.UnMerge();
}
// Use constants and calculation operators in a formula. workbook.Worksheets[0].Cells["B2"].Formula = "= (1+5)*6";
workbook.Worksheets[0].Cells["A1"].Formula = "= B1+C1";
// Sum values of cells located in different worksheets.
workbook.Worksheets["Sheet1"].Cells["H15"].Formula = "= Sheet2!C3 + Sheet3!C5";
// Use a relative cell reference in a formula.
workbook.Worksheets[0].Cells["B2"].Formula = "= A1";
// Use an absolute cell reference in a formula.
workbook.Worksheets[0].Cells["C3"].Formula = "= $A$1";
// Use mixed cell references in formulas.
workbook.Worksheets[0].Cells["D4"].Formula = "= A$1";
workbook.Worksheets[0].Cells["E5"].Formula = "= $B2";
// Use 3D cell references in formulas.
workbook.Worksheets[0].Cells["G10"].Formula = "= Sheet1!C3";
workbook.Worksheets[0].Cells["G11"].Formula = "= SUM(Sheet1:Sheet3!C3:C5)";
(FormulaActions.cs)
// Switch on the R1C1 reference style in a workbook.
workbook.DocumentSettings.R1C1ReferenceStyle = true;
// Specify a formula with relative R1C1 references in cell D2
// to add values contained in cells A2 through A11.
worksheet.Cells["D2"].Formula = "=SUM(RC[-3]:R[9]C[-3])";
// Specify a formula with absolute R1C1 references
// to add values contained in cells A2 through A11.
worksheet.Cells["D3"].Formula = "=SUM(R2C1:R11C1)";
// Access the "A2:C5" range of cells in the worksheet.
Range range = worksheet.Range["A2:C5"];
// Specify the name for the created range.
range.Name = "myRange";
// Create a formula that sums up the values of all cells included in the specified named range.
worksheet.Cells["F3"].Formula = "= SUM(myRange)";
Worksheet worksheet1 = workbook.Worksheets["Sheet1"];
Worksheet worksheet2 = workbook.Worksheets["Sheet2"];
// Create a name for a formula that sums up the values of all cells included in the "A1:C3" range // The scope of this name will be limited by the "Sheet1" worksheet.
worksheet1.DefinedNames.Add("Range_Sum", "=SUM(Sheet1!$A$1:$C$3)");
// Create a name for a formula that doubles the value resulting from the "Range_Sum" named formula // make this name available within the entire workbook.
workbook.DefinedNames.Add("Range_DoubleSum", "=2*Sheet1!Range_Sum");
// Create formulas that use other formulas with the specified names.
worksheet2.Cells["C2"].Formula = "=Sheet1!Range_Sum";
worksheet2.Cells["C3"].Formula = "=Range_DoubleSum";
worksheet2.Cells["C4"].Formula = "=Range_DoubleSum + 100";
在公式中使用函数和嵌套函数
// If the number in cell A2 is less than 10, the formula returns "Normal"
// and this text is displayed in cell C2. Otherwise, cell C2 displays "Excess".
worksheet.Cells["C2"].Formula = @"=IF(A2<10, ""Normal"", ""Excess"")";
// Calculate the average value for cell values within the "A2:A7" range.
worksheet.Cells["C3"].Formula = "=AVERAGE(A2:A7)";
// Add the values contained in cells A3 through A5, add the value contained in cell A6,
// and add 100 to that result.
worksheet.Cells["C4"].Formula = "=SUM(A3:A5,A6,100)";
// Use a nested function in a formula.
// Round the sum of the values contained in cells A6 and A7 to two decimal places.
worksheet.Cells["C5"].Formula = "=ROUND(SUM(A6,A7),2)";
// Add the current date to cell C6.
worksheet.Cells["C6"].Formula = "=Today()";
worksheet.Cells["C6"].NumberFormat = "m/d/yy";
// Convert the specified text to uppercase.
worksheet.Cells["C7"].Formula = @"=UPPER(""formula"")";
worksheet.Cells["A2"].Value = 1;
// Use the shared formula in the "A3:A11" range of cells.
worksheet.Range["A3:A11"].Formula = "=SUM(A2+1)";
// Use the shared formula in the "B2:B11" range of cells.
worksheet.Range["B2:B11"].Formula = "=A2+2";
// Create an array formula that multiplies values contained in the cell range A2 through A11
// by the corresponding cells in the range B2 through B11,
// and displays the results in cells C2 through C11.
worksheet.Range["C2:C11"].ArrayFormula = "=A2:A11*B2:B11";
// Create an array formula that multiplies values contained in the cell range C2 through C11 by 2
// and displays the results in cells D2 through D11.
worksheet.Range["D2:D11"].ArrayFormula = "=C2:C11*2";
// Create an array formula that multiplies values contained in the cell range B2 through D11,
// adds the results, and displays the total sum in cell D12.
worksheet.Cells["D12"].ArrayFormula = "=SUM(B2:B11*C2:C11*D2:D11)";
// Re-dimension an array formula range:
// delete the array formula and create a new range with the same formula.
if (worksheet.Cells["C13"].HasArrayFormula) {
string af = worksheet.Cells["C13"].ArrayFormula;
worksheet.Cells["C13"].GetArrayFormulaRange().ArrayFormula = string.Empty;
worksheet.Range["C2:C11"].ArrayFormula = af;