Devexpress Spreadsheet 中文教程(1)

以下操作均需引用DLL:
DevExpress.Docs.v15.2.dll、

DevExpress.Data.v15.2.dll
命名空间:
DevExpress.Spreadsheet
否则代码不可用喔
2016-08-02  添加

1、从文件打开:

workbook.LoadDocument("Documents\\Document.xlsx", DocumentFormat.OpenXml);

2、从文件流打开:

using System.IO;
using DevExpress.Spreadsheet;
// ...
Workbook workbook = new Workbook();
// Load a workbook from the stream.
using (FileStream stream = new FileStream("Documents\\Document.xlsx", FileMode.workbook.LoadDocument(stream, DocumentFormat.OpenXml);
}

如何将文档保存到文件

1、另存为文件:

workbook.SaveDocument("Documents\\SavedDocument.xlsx", DocumentFormat.OpenXml);

2、保存到文件流:

using System.IO;
using DevExpress.Spreadsheet;
// ...
Workbook workbook = new Workbook();
// ...
// Save the modified document to the stream.
using (FileStream stream = new FileStream("Documents\\SavedDocument.xlsx",
FileMode.Create, FileAccess.ReadWrite)) {
workbook.SaveDocument(stream, DocumentFormat.OpenXml);

如何将Workbook保存为PDF格式

(ExportActions.cs)
using (FileStream pdfFileStream = new FileStream("Documents\\Document_PDF.pdf", FileMode.Create))
{
workbook.ExportToPdf(pdfFileStream);
}

如何将将Workbook保存为HTML格式

Worksheet worksheet = workbook.Worksheets["Grouping"]; workbook.Worksheets.ActiveWorksheet = worksheet; HtmlDocumentExporterOptions options = new HtmlDocumentExporterOptions(); // Specify the part of the document to be exported to HTML. options.SheetIndex = worksheet.Index; options.Range = "B2:G7"; // Export the active worksheet to a stream as HTML with the specified options. using (FileStream htmlStream = new FileStream("OutputWorksheet.html", FileMode.Create)) { workbook.ExportToHtml(htmlStream, options); } System.Diagnostics.Process.Start("OutputWorksheet.html");

创建一个新的Workbook

Workbook workbook = new Workbook();

访问一个Worksheet

using DevExpress.Spreadsheet;
// ...
Workbook workbook = new Workbook();
// Access a collection of worksheets.
WorksheetCollection worksheets = workbook.Worksheets;
// Access a worksheet by its index.
Worksheet worksheet1 = workbook.Worksheets[0];
// Access a worksheet by its name.
Worksheet worksheet2 = workbook.Worksheets["Sheet2"];

设置活动Worksheet

workbook.Worksheets.ActiveWorksheet = workbook.Worksheets["Sheet2"];

添加一个新的Worksheet

// Add a new worksheet to the workbook. The worksheet will be inserted into the end of the existing // under the name "SheetN", where N is a number following the largest number used in worksheet names workbook.Worksheets.Add();
// Add a new worksheet under the specified name.
workbook.Worksheets.Add().Name = "TestSheet1";
workbook.Worksheets.Add("TestSheet2");
// Add a new worksheet to the specified position in the collection of worksheets.
workbook.Worksheets.Insert(1, "TestSheet3");
workbook.Worksheets.Insert(3);


如何删除Worksheet

// Delete the "Sheet2" worksheet from the workbook.
workbook.Worksheets.Remove(workbook.Worksheets["Sheet2"]);
// Delete the first worksheet from the workbook.
workbook.Worksheets.RemoveAt(0);

如何给Worksheet重命名

// Change the name of the second worksheet.
workbook.Worksheets[1].Name = "Renamed Sheet";

如何从本Workbook里面复制Worksheet

// Add a new worksheet to a workbook.
workbook.Worksheets.Add("Sheet1_Copy");
// Copy all information (content and formatting) to the newly created worksheet
// from the "Sheet1" worksheet.
workbook.Worksheets["Sheet1_Copy"].CopyFrom(workbook.Worksheets["Sheet1"]);

如何从Workbook之间复制Worksheet

Workbook sourceWorkbook = new Workbook();
// Add a new worksheet.
sourceWorkbook.Worksheets.Add();
// Modify the second worksheet of the source workbook to be copied.
sourceWorkbook.Worksheets[1].Cells["A1"].Value = "A worksheet to be copied";
sourceWorkbook.Worksheets[1].Cells["A1"].Font.Color = Color.ForestGreen;
// Copy the second worksheet of the source workbook into the first worksheet of another workbook.
workbook.Worksheets[0].CopyFrom(sourceWorkbook.Worksheets[1]);

Worksheet工作簿内顺序的移动

// Move the first worksheet to the position of the last worksheet within the workbook.
workbook.Worksheets[0].Move(workbook.Worksheets.Count - 1);


// Move the first worksheet to the position of the last worksheet within the workbook.
workbook.Worksheets[0].Move(workbook.Worksheets.Count - 1);Devexpress Spreadsheet 中文教程(1)_第1张图片


如何隐藏或显示Worksheet


// Hide the worksheet under the "Sheet2" name and prevent end-users from unhiding it via user interface.
// To make this worksheet visible again, use the Worksheet.Visible property.
workbook.Worksheets["Sheet2"].VisibilityType = WorksheetVisibilityType.VeryHidden;
// Hide the "Sheet3" worksheet.
// In this state a worksheet can be unhidden via user interface.
workbook.Worksheets["Sheet3"].Visible = false;

Devexpress Spreadsheet 中文教程(1)_第2张图片

如何隐藏或显示网格线(Gridlines)

workbook.Worksheets[0].ActiveView.ShowGridlines = false;


隐藏和显示列行标题

// Hide row and column headings in the first worksheet.
workbook.Worksheets[0].ActiveView.ShowHeadings = false;

设置页面方向

// Set the page orientation to Landscape.
workbook.Worksheets[0].ActiveView.Orientation = PageOrientation.Landscape;

设置页边距

// Select a unit of measure used within the workbook.
workbook.Unit = DevExpress.Office.DocumentUnit.Inch;
// Access page margins.
Margins pageMargins = workbook.Worksheets[0].ActiveView.Margins;
// Specify page margins.
pageMargins.Left = 1;
pageMargins.Top = 1.5F;
pageMargins.Right = 1;
pageMargins.Bottom = 0.8F;
// Specify header and footer margins.
pageMargins.Header = 1;
pageMargins.Footer = 0.4F;

设置纸张尺寸

// Select the page's paper size.
workbook.Worksheets[0].ActiveView.PaperKind = System.Drawing.Printing.PaperKind.A4;

放大或缩小Worksheet

// Zoom in the worksheet view.
workbook.Worksheets[0].ActiveView.Zoom = 150;
Devexpress Spreadsheet 中文教程(1)_第3张图片


如何访问列(Row)或者行(Column)

Rows

using DevExpress.Spreadsheet;
// ...
Workbook workbook = new Workbook();
// Access a collection of rows.
RowCollection rows = workbook.Worksheets[0].Rows;
// Access the first row by its index in the collection of rows.
Row firstRow_byIndex = rows[0];
// Access the first row by its unique name.
Row firstRow_byName = rows["1"];

Column

using DevExpress.Spreadsheet;
// ...
Workbook workbook = new Workbook();
// Access a collection of columns.
ColumnCollection columns = workbook.Worksheets[0].Columns;
// Access the first column by its index in the collection of columns.
Column firstColumn_byIndex = columns[0];
// Access the first column by its unique name.
Column firstColumn_byName = columns["A"];
Devexpress Spreadsheet 中文教程(1)_第4张图片



如何在Worksheet内新增行或列

Row

// Insert a new row 3.
worksheet.Rows["3"].Insert();
// Insert a new row into the worksheet at the 5the position.
worksheet.Rows.Insert(4);
// Insert five rows into the worksheet at the 9th position.
worksheet.Rows.Insert(8, 5);
// Insert two rows above the "L15:M16" cell range.
worksheet.InsertCells(worksheet.Range["L15:M16"], InsertCellsMode.EntireRow);

Column

// Insert a new column C.
worksheet.Columns["C"].Insert();
// Insert a new column into the worksheet at the 5th position.
worksheet.Columns.Insert(4);
// Insert three columns into the worksheet at the 7th position.
worksheet.Columns.Insert(6, 3);
// Insert two columns to the left of the "L15:M16" cell range.
worksheet.InsertCells(worksheet.Range["L15:M16"], InsertCellsMode.EntireColumn

如何从Worksheet中删除Rows或者Column

Rows

// Delete the 2nd row from the worksheet.
worksheet.Rows[1].Delete();
// Delete the 3rd row from the worksheet.
worksheet.Rows.Remove(2);
// Delete three rows from the worksheet starting from the 10th row.
worksheet.Rows.Remove(9, 3);
// Delete a row that contains the "B2"cell.
worksheet.DeleteCells(worksheet.Cells["B2"], DeleteMode.EntireRow);

Column

// Delete the 2nd column from the worksheet.
worksheet.Columns[1].Delete();
// Delete the 3rd column from the worksheet.
worksheet.Columns.Remove(2);
// Delete three columns from the worksheet starting from the 10th column.
worksheet.Columns.Remove(9, 3);
// Delete a column that contains the "B2"cell.
worksheet.DeleteCells(worksheet.Cells["B2"], DeleteMode.EntireColumn);

如何复制Row或者Column

// Copy all data from the 2nd row to the 5th row.
worksheet.Rows["5"].CopyFrom(worksheet.Rows["2"]);
// Copy only borders from the "B" column to the "E" column.
worksheet.Columns["E"].CopyFrom(worksheet.Columns["B"], PasteSpecial.Borders);

隐藏或显示Row\Column

Worksheet worksheet = workbook.Worksheets[0];
// Hide the 8th row of the worksheet.
worksheet.Rows[7].Visible = false;
// Hide the 4th column of the worksheet.
worksheet.Columns[3].Visible = false;

指定行高或列宽

// Set the height of the 3rd row to 50 points.
workbook.Unit = DevExpress.Office.DocumentUnit.Point;
worksheet.Rows[2].Height = 50;
// Set the height of the row that contains the "C5" cell to 2 inches.
workbook.Unit = DevExpress.Office.DocumentUnit.Inch;
worksheet.Cells["C5"].RowHeight = 2;
// Set the height of the 7th row to the height of the 3rd row.
worksheet.Rows["7"].Height = worksheet.Rows["3"].Height;
// Set the default row height to 30 points.
workbook.Unit = DevExpress.Office.DocumentUnit.Point;
worksheet.DefaultRowHeight = 30;

或者

// Set the "B" column width to 30 characters of the default font that is worksheet.Columns["B"].WidthInCharacters = 30;
// Set the "C" column width to 15 millimeters.
workbook.Unit = DevExpress.Office.DocumentUnit.Millimeter;
worksheet.Columns["C"].Width = 15;
// Set the width of the column that contains the "E15" cell to 100 points.
workbook.Unit = DevExpress.Office.DocumentUnit.Point;
worksheet.Cells["E15"].ColumnWidth = 100;
// Set the width of all columns that contain the "F4:H7" cell range (the worksheet.Range["F4:H7"].ColumnWidth = 70;
// Set the "J" column width to the "B" column width value.
worksheet.Columns["J"].Width = worksheet.Columns["B"].Width;
// Copy the "C" column width value and assign it to the "K" column width.
worksheet.Columns["K"].CopyFrom(worksheet.Columns["C"], PasteSpecial.ColumnWidths);
// Set the default column width to 40 pixels.
worksheet.DefaultColumnWidthInPixels = 40;
























你可能感兴趣的:(Excel二次开发,C#,DevExpress,C#,WinFrom,C#,WPF)