当前位置:   article > 正文

Devexpress Spreadsheet 中文教程(2)_devexpress spreadsheet 添加二维码

devexpress spreadsheet 添加二维码

从Worksheet中访问Cell

  1. using DevExpress.Spreadsheet;
  2. // ...
  3. Workbook workbook = new Workbook();
  4. Worksheet worksheet = workbook.Worksheets[0];
  5. Cell cellA1 = worksheet[0, 0]; // Cell A1
  6. Cell cellB2 = worksheet.Cells["B2"]; // Cell B2
  7. Cell cellC3 = worksheet.Cells[2, 2]; // Cell C3
  8. Cell cellD4 = worksheet.Rows[3][3]; // Cell D4
  9. Cell cellE5 = worksheet.Rows[4]["E"]; // Cell E5
  10. Cell cellF6 = worksheet.Rows["6"][5]; // Cell F6
  11. Cell cellG7 = worksheet.Columns[6][6]; // Cell G7
  12. Cell cellH8 = worksheet.Columns["H"][7]; // Cell H8
  13. Cell cellI9 = worksheet.Columns["I"]["9"]; // Cell I9
  14. // Access a cell from the range of cells.
  15. Cell cellB5 = worksheet.Range["B3:D8"][6]; // Cell B5
  16. Cell cellD6 = worksheet.Range["B3:D8"][3, 2]; // Cell D6

访问一系列Cell(Range)

  1. using DevExpress.Spreadsheet;
  2. // ...
  3. Workbook workbook = new Workbook();
  4. Worksheet worksheet = workbook.Worksheets[0];
  5. // A range that includes cells from the top left cell (A1) to the bottom right cell (B5).
  6. Range rangeA1B5 = worksheet["A1:B5"];
  7. // A rectangular range that includes cells from the top left cell (C4) to the bottom right cell (E7).
  8. Range rangeC4E7 = worksheet.Range["C4:E7"];
  9. // The C4:E7 cell range located in the "Sheet3" worksheet.
  10. Range rangeSheet3C4E7 = workbook.Range["Sheet3!C4:E7"];
  11. // A range that contains a single cell (E7).
  12. Range rangeE7 = worksheet.Range["E7"];
  13. // A range that includes the entire column A.
  14. Range rangeColumnA = worksheet.Range["A:A"];
  15. // A range that includes the entire row 5.
  16. Range rangeRow5 = worksheet.Range["5:5"];
  17. // A minimal rectangular range that includes all listed cells: C6, D9 and E7.
  18. Range rangeC6D9E7 = worksheet.Range.Parse("C6:D9:E7");
  19. // A rectangular range whose left column index is 0, top row index is 0,
  20. // right column index is 3 and bottom row index is 2. This is the A1:D3 cell range.
  21. Range rangeA1D3 = worksheet.Range.FromLTRB(0, 0, 3, 2);
  22. // A range that includes the intersection of two ranges: C5:E10 and E9:G13.
  23. // This is the E9:E10 cell range.
  24. Range rangeE9E10 = worksheet.Range["C5:E10 E9:G13"];
  25. // Create a defined name for the D20:G23 cell range.
  26. worksheet.DefinedNames.Add("Range_Name", "Sheet1!$D$20:$G$23");
  27. // Access a range by its defined name.
  28. Range rangeD20G23 = worksheet.Range["Range_Name"];

如何插入Cell或者Cell区域

  1. using DevExpress.Spreadsheet;
  2. // ...
  3. Workbook workbook = new Workbook();
  4. Worksheet worksheet = workbook.Worksheets[0];
  5. // Insert a cell into the C5 position, shifting other cells in the same row to the right.
  6. worksheet.InsertCells(worksheet.Cells["C5"], InsertCellsMode.ShiftCellsDown);
  7. // Insert cells into the location of the H11:I12 range, shifting other cells in the same column down.
  8. worksheet.InsertCells(worksheet.Range["H11:I12"], InsertCellsMode.ShiftCellsRight);

删除Cell或者Cell区域

  1. using DevExpress.Spreadsheet;
  2. // ...
  3. Workbook workbook = new Workbook();
  4. Worksheet worksheet = workbook.Worksheets[0];
  5. // Delete the C5 cell, shifting other cells in the same row to the left.
  6. worksheet.DeleteCells(worksheet.Cells["C5"], DeleteMode.ShiftCellsLeft);
  7. // Delete the H11:I12 range of cells, shifting other cells in the same column up.
  8. worksheet.DeleteCells(worksheet.Range["H11:I12"], DeleteMode.ShiftCellsUp);

给区域(Range of Cells)命名

  1. Worksheet worksheet = workbook.Worksheets[0];
  2. // Create a range.
  3. Range rangeB3D6 = worksheet.Range["B3:D6"];
  4. // Specify the name for the created range.
  5. rangeB3D6.Name = "rangeB3D6";
  6. // Create a new defined name with the specifed range name and absolute reference.
  7. DefinedName definedName = worksheet.DefinedNames.Add("rangeB17D20", "Sheet1!$B$17:$D$20");
  8. // Create a range using the specified defined name.
  9. Range B17D20 = worksheet.Range[definedName.Name];


修改Cell获取Cell区域值

  1. // Add data of different types to cells.
  2. worksheet.Cells["B1"].Value = DateTime.Now;
  3. worksheet.Cells["B2"].Value = Math.PI;
  4. worksheet.Cells["B3"].Value = "Have a nice day!";
  5. worksheet.Cells["B4"].Value = CellValue.ErrorReference;
  6. worksheet.Cells["B5"].Value = true;
  7. worksheet.Cells["B6"].Value = float.MaxValue;
  8. worksheet.Cells["B7"].Value = 'a';
  9. worksheet.Cells["B8"].Value = Int32.MaxValue;
  10. // Fill all cells in the range with 10.
  11. worksheet.Range["B10:E10"].Value = 10;

为Cells添加公式

//待续

为单元格添加超链接

  1. // Create a hyperlink to a web page.
  2. Cell cell = worksheet.Cells["A1"];
  3. worksheet.Hyperlinks.Add(cell, "http://www.devexpress.com/", true, "DevExpress");
  4. // Create a hyperlink to a cell range in a workbook.
  5. Range range = worksheet.Range["C3:D4"];
  6. Hyperlink cellHyperlink = worksheet.Hyperlinks.Add(range, "Sheet2!B2:E7", false, "Select Range");
  7. cellHyperlink.TooltipText = "Click Me";

为单元格添加批注

  1. using DevExpress.Spreadsheet;
  2. //...
  3. Workbook workbook = new Workbook();
  4. Worksheet worksheet = workbook.Worksheets[0];
  5. //Get the system username.
  6. string author = workbook.CurrentAuthor;
  7. //Add a comment to the A1 cell.
  8. Cell cell = worksheet.Cells["A1"];
  9. Comment comment = worksheet.Comments.Add(cell, author, "This is important information for users.");
  10. //Add the author name at the beginning of the comment.
  11. CommentRunCollection runs = comment.Runs;
  12. runs.Insert(0, author + ": \r\n");
  13. runs[0].Font.Bold = true;
  14. //Format the comment text.
  15. runs[1].Font.Color = Color.Red;
  16. runs[1].Font.Name = "Times New Roman";
  17. runs[1].Font.Size = 14;
  18. runs[1].Font.Italic = true;
  19. //Add a new comment run.
  20. runs.Add("\n Never delete this comment!");
  21. runs[2].Font.Color = Color.MidnightBlue;

清除单元格内容,格式、超链接以及批注

  1. // Remove all cell information (content, formatting, hyperlinks and comments).
  2. worksheet.Clear(worksheet["C2:D2"]);
  3. // Remove cell content.
  4. worksheet.ClearContents(worksheet["C3"]);
  5. worksheet["D3"].Value = null;
  6. // Remove cell formatting.
  7. worksheet.ClearFormats(worksheet["C4"]);
  8. worksheet["D4"].Style = workbook.Styles.DefaultStyle;
  9. // Remove hyperlinks from cells.
  10. worksheet.ClearHyperlinks(worksheet["C5"]);
  11. Hyperlink hyperlinkD5 = worksheet.Hyperlinks.GetHyperlinks(worksheet["D5"])[0];
  12. worksheet.Hyperlinks.Remove(hyperlinkD5);
  13. // Remove comments from cells.
  14. worksheet.ClearComments(worksheet["C6"]);
  15. Comment commentD6 = worksheet.Comments.GetComments(worksheet["D6"])[0];
  16. worksheet.Comments.Remove(commentD6);

如何仅复制单元格Data、单元格Style或者Data与Style

  1. Worksheet worksheet = workbook.Worksheets[0];
  2. worksheet.Columns["A"].WidthInCharacters = 32;
  3. worksheet.Columns["B"].WidthInCharacters = 20;
  4. Style style = workbook.Styles[BuiltInStyleId.Input];
  5. // Specify the content and formatting for a source cell.
  6. worksheet.Cells["A1"].Value = "Source Cell";
  7. Cell sourceCell = worksheet.Cells["B1"];
  8. sourceCell.Formula = "= PI()";
  9. sourceCell.NumberFormat = "0.0000";
  10. sourceCell.Style = style;
  11. sourceCell.Font.Color = Color.Blue;
  12. sourceCell.Font.Bold = true;
  13. sourceCell.Borders.SetOutsideBorders(Color.Black, BorderLineStyle.Thin);
  14. // Copy all information from the source cell to the "B3" cell.
  15. worksheet.Cells["A3"].Value = "Copy All";
  16. worksheet.Cells["B3"].CopyFrom(sourceCell);
  17. // Copy only the source cell content (e.g., text, numbers, formula calculated values) to the "B4" worksheet.Cells["A4"].Value = "Copy Values";
  18. worksheet.Cells["B4"].CopyFrom(sourceCell, PasteSpecial.Values);
  19. // Copy the source cell content (e.g., text, numbers, formula calculated values)
  20. // and number formats to the "B5" cell.
  21. worksheet.Cells["A5"].Value = "Copy Values and Number Formats";
  22. worksheet.Cells["B5"].CopyFrom(sourceCell, PasteSpecial.Values | PasteSpecial.NumberFormats);
  23. // Copy only the formatting information from the source cell to the "B6" cell.
  24. worksheet.Cells["A6"].Value = "Copy Formats";
  25. worksheet.Cells["B6"].CopyFrom(sourceCell, PasteSpecial.Formats);
  26. // Copy all information from the source cell to the "B7" cell except for border settings.
  27. worksheet.Cells["A7"].Value = "Copy All Except Borders";
  28. worksheet.Cells["B7"].CopyFrom(sourceCell, PasteSpecial.All & ~PasteSpecial.Borders);
  29. // Copy information only about borders from the source cell to the "B8" cell.
  30. worksheet.Cells["A8"].Value = "Copy Borders";
  31. worksheet.Cells["B8"].CopyFrom(sourceCell, PasteSpecial.Borders);

合并单元格或者取消合并单元格

合并

  1. // Merge cells contained in the range.
  2. worksheet.MergeCells(worksheet.Range["A1:C5"]);

取消合并

  1. // Split cells that were previously merged.
  2. worksheet.UnMergeCells(worksheet.Range["A1:C5"]);

取消所有单元格合并

  1. foreach (var item in worksheet.Cells.GetMergedRanges()) {
  2. item.UnMerge();
  3. }

  1. </pre></p><p></p><h1>单元格内使用常量公式</h1><pre class="csharp" name="code">// Use constants and calculation operators in a formula.
  2. workbook.Worksheets[0].Cells["B2"].Formula = "= (1+5)*6";
workbook.Worksheets[0].Cells["A1"].Formula = "= B1+C1";
  1. // Sum values of cells located in different worksheets.
  2. workbook.Worksheets["Sheet1"].Cells["H15"].Formula = "= Sheet2!C3 + Sheet3!C5";
  1. // Use a relative cell reference in a formula.
  2. workbook.Worksheets[0].Cells["B2"].Formula = "= A1";
  1. // Use an absolute cell reference in a formula.
  2. workbook.Worksheets[0].Cells["C3"].Formula = "= $A$1";
  1. // Use mixed cell references in formulas.
  2. workbook.Worksheets[0].Cells["D4"].Formula = "= A$1";
  3. workbook.Worksheets[0].Cells["E5"].Formula = "= $B2";
  1. // Use 3D cell references in formulas.
  2. workbook.Worksheets[0].Cells["G10"].Formula = "= Sheet1!C3";
  3. workbook.Worksheets[0].Cells["G11"].Formula = "= SUM(Sheet1:Sheet3!C3:C5)";


  1. (FormulaActions.cs)
  2. // Switch on the R1C1 reference style in a workbook.
  3. workbook.DocumentSettings.R1C1ReferenceStyle = true;
  4. // Specify a formula with relative R1C1 references in cell D2
  5. // to add values contained in cells A2 through A11.
  6. worksheet.Cells["D2"].Formula = "=SUM(RC[-3]:R[9]C[-3])";
  7. // Specify a formula with absolute R1C1 references
  8. // to add values contained in cells A2 through A11.
  9. worksheet.Cells["D3"].Formula = "=SUM(R2C1:R11C1)";

在公式中使用命名的区域

  1. // Access the "A2:C5" range of cells in the worksheet.
  2. Range range = worksheet.Range["A2:C5"];
  3. // Specify the name for the created range.
  4. range.Name = "myRange";
  5. // Create a formula that sums up the values of all cells included in the specified named range.
  6. worksheet.Cells["F3"].Formula = "= SUM(myRange)";



为公式命名

  1. Worksheet worksheet1 = workbook.Worksheets["Sheet1"];
  2. Worksheet worksheet2 = workbook.Worksheets["Sheet2"];
  3. // 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.
  4. worksheet1.DefinedNames.Add("Range_Sum", "=SUM(Sheet1!$A$1:$C$3)");
  5. // 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.
  6. workbook.DefinedNames.Add("Range_DoubleSum", "=2*Sheet1!Range_Sum");
  7. // Create formulas that use other formulas with the specified names.
  8. worksheet2.Cells["C2"].Formula = "=Sheet1!Range_Sum";
  9. worksheet2.Cells["C3"].Formula = "=Range_DoubleSum";
  10. worksheet2.Cells["C4"].Formula = "=Range_DoubleSum + 100";

在公式中使用函数和嵌套函数

  1. // If the number in cell A2 is less than 10, the formula returns "Normal"
  2. // and this text is displayed in cell C2. Otherwise, cell C2 displays "Excess".
  3. worksheet.Cells["C2"].Formula = @"=IF(A2<10, ""Normal"", ""Excess"")";
  4. // Calculate the average value for cell values within the "A2:A7" range.
  5. worksheet.Cells["C3"].Formula = "=AVERAGE(A2:A7)";
  6. // Add the values contained in cells A3 through A5, add the value contained in cell A6,
  7. // and add 100 to that result.
  8. worksheet.Cells["C4"].Formula = "=SUM(A3:A5,A6,100)";
  9. // Use a nested function in a formula.
  10. // Round the sum of the values contained in cells A6 and A7 to two decimal places.
  11. worksheet.Cells["C5"].Formula = "=ROUND(SUM(A6,A7),2)";
  12. // Add the current date to cell C6.
  13. worksheet.Cells["C6"].Formula = "=Today()";
  14. worksheet.Cells["C6"].NumberFormat = "m/d/yy";
  15. // Convert the specified text to uppercase.
  16. worksheet.Cells["C7"].Formula = @"=UPPER(""formula"")";

Create Shared Formulas

  1. worksheet.Cells["A2"].Value = 1;
  2. // Use the shared formula in the "A3:A11" range of cells.
  3. worksheet.Range["A3:A11"].Formula = "=SUM(A2+1)";
  4. // Use the shared formula in the "B2:B11" range of cells.
  5. worksheet.Range["B2:B11"].Formula = "=A2+2";

创建数组公式

  1. // Create an array formula that multiplies values contained in the cell range A2 through A11
  2. // by the corresponding cells in the range B2 through B11,
  3. // and displays the results in cells C2 through C11.
  4. worksheet.Range["C2:C11"].ArrayFormula = "=A2:A11*B2:B11";
  5. // Create an array formula that multiplies values contained in the cell range C2 through C11 by 2
  6. // and displays the results in cells D2 through D11.
  7. worksheet.Range["D2:D11"].ArrayFormula = "=C2:C11*2";
  8. // Create an array formula that multiplies values contained in the cell range B2 through D11,
  9. // adds the results, and displays the total sum in cell D12.
  10. worksheet.Cells["D12"].ArrayFormula = "=SUM(B2:B11*C2:C11*D2:D11)";
  11. // Re-dimension an array formula range:
  12. // delete the array formula and create a new range with the same formula.
  13. if (worksheet.Cells["C13"].HasArrayFormula) {
  14. string af = worksheet.Cells["C13"].ArrayFormula;
  15. worksheet.Cells["C13"].GetArrayFormulaRange().ArrayFormula = string.Empty;
  16. worksheet.Range["C2:C11"].ArrayFormula = af;



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/251167
推荐阅读
相关标签
  

闽ICP备14008679号