创建和管理 Microsoft Excel 文件的表格。
创建表
电子表格的优点之一是它们允许您创建不同类型的列表,例如电话列表、任务列表、交易列表、资产或负债。多个用户可以一起使用、创建和维护各种列表。
Aspose.Cells 支持创建和管理列表。
列表对象的优点
将数据列表转换为实际列表对象时有很多优点
- 自动包含新行和新列。
- 列表底部的总计行可以轻松添加以显示 SUM、AVERAGE、COUNT 等。
- 添加到右侧的列会自动合并到 List 对象中。
- 基于行和列的图表将自动展开。
- 分配给行和列的命名范围将自动扩展。
- 该列表受到保护,不会意外删除行和列。
使用 Microsoft Excel 创建列表对象
- 选择用于创建列表对象的数据范围
- 这将显示“创建列表”对话框。
- 为数据实现列表对象并指定合计行(选择数据, 然后列表, 其次是总行数).
使用 Aspose.Cells API
Aspose.Cells提供了一个类,工作簿,代表一个 Microsoft Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。
工作表由工作表班级。这工作表类提供了广泛的属性和方法来管理工作表。创建一个列表对象在工作表中,使用列表对象的集合属性工作表班级。每个列表对象实际上是列表对象集合类,它进一步提供了添加添加 List 对象并为列表指定单元格范围的方法。
Aspose.Cells根据指定范围的单元格,创建List对象。使用属性(例如,显示总计, 列表列等)的列表对象控制列表的类。
在下面给出的示例中,我们创建了相同的列表对象使用我们在上一节中使用 Microsoft Excel 创建的 Aspose.Cells API。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create a Workbook object. | |
// Open a template excel file. | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Get the List objects collection in the first worksheet. | |
Aspose.Cells.Tables.ListObjectCollection listObjects = workbook.Worksheets[0].ListObjects; | |
// Add a List based on the data source range with headers on. | |
listObjects.Add(1, 1, 7, 5, true); | |
// Show the total row for the List. | |
listObjects[0].ShowTotals = true; | |
// Calculate the total of the last (5th ) list column. | |
listObjects[0].ListColumns[4].TotalsCalculation = Aspose.Cells.Tables.TotalsCalculation.Sum; | |
// Save the excel file. | |
workbook.Save(dataDir + "output.xls"); |
格式化表格
要管理和分析一组相关数据,可以将一系列单元格转换为列表对象(也称为 Excel 表格)。表是一系列行和列,其中包含独立于其他行和列中的数据管理的相关数据。默认情况下,表格中的每一列都在标题行中启用了筛选,以便您可以快速筛选或排序列表对象数据。您可以将总计行(列表中的特殊行,提供对处理数字数据有用的聚合函数选择)添加到列表对象,该列表对象为每个总计行单元格提供聚合函数的下拉列表。 Aspose.Cells 提供用于创建和管理列表(或表格)的选项。
格式化列表对象
Aspose.Cells提供了一个类,工作簿,代表一个 Microsoft Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。
工作表由工作表班级。这工作表类提供了广泛的属性和方法来管理工作表。创建一个列表对象在工作表中,使用列表对象的集合属性工作表班级。每个列表对象实际上是列表对象集合类,它进一步提供了添加添加 List 对象并指定它应包含的单元格范围的方法。根据指定的单元格范围,一个列表对象由 Aspose.Cells 在工作表中创建。使用属性(例如,表格样式类型) 的列表对象类来根据您的要求格式化表格。
下面的示例将样本数据添加到工作表,添加列表对象并为其应用默认样式。列表对象Microsoft Excel 2007/2010 支持样式。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Create a workbook. | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the default(first) worksheet | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Obtaining Worksheet's cells collection | |
Cells cells = sheet.Cells; | |
// Setting the value to the cells | |
Aspose.Cells.Cell cell = cells["A1"]; | |
cell.PutValue("Employee"); | |
cell = cells["B1"]; | |
cell.PutValue("Quarter"); | |
cell = cells["C1"]; | |
cell.PutValue("Product"); | |
cell = cells["D1"]; | |
cell.PutValue("Continent"); | |
cell = cells["E1"]; | |
cell.PutValue("Country"); | |
cell = cells["F1"]; | |
cell.PutValue("Sale"); | |
cell = cells["A2"]; | |
cell.PutValue("David"); | |
cell = cells["A3"]; | |
cell.PutValue("David"); | |
cell = cells["A4"]; | |
cell.PutValue("David"); | |
cell = cells["A5"]; | |
cell.PutValue("David"); | |
cell = cells["A6"]; | |
cell.PutValue("James"); | |
cell = cells["A7"]; | |
cell.PutValue("James"); | |
cell = cells["A8"]; | |
cell.PutValue("James"); | |
cell = cells["A9"]; | |
cell.PutValue("James"); | |
cell = cells["A10"]; | |
cell.PutValue("James"); | |
cell = cells["A11"]; | |
cell.PutValue("Miya"); | |
cell = cells["A12"]; | |
cell.PutValue("Miya"); | |
cell = cells["A13"]; | |
cell.PutValue("Miya"); | |
cell = cells["A14"]; | |
cell.PutValue("Miya"); | |
cell = cells["A15"]; | |
cell.PutValue("Miya"); | |
cell = cells["B2"]; | |
cell.PutValue(1); | |
cell = cells["B3"]; | |
cell.PutValue(2); | |
cell = cells["B4"]; | |
cell.PutValue(3); | |
cell = cells["B5"]; | |
cell.PutValue(4); | |
cell = cells["B6"]; | |
cell.PutValue(1); | |
cell = cells["B7"]; | |
cell.PutValue(2); | |
cell = cells["B8"]; | |
cell.PutValue(3); | |
cell = cells["B9"]; | |
cell.PutValue(4); | |
cell = cells["B10"]; | |
cell.PutValue(4); | |
cell = cells["B11"]; | |
cell.PutValue(1); | |
cell = cells["B12"]; | |
cell.PutValue(1); | |
cell = cells["B13"]; | |
cell.PutValue(2); | |
cell = cells["B14"]; | |
cell.PutValue(2); | |
cell = cells["B15"]; | |
cell.PutValue(2); | |
cell = cells["C2"]; | |
cell.PutValue("Maxilaku"); | |
cell = cells["C3"]; | |
cell.PutValue("Maxilaku"); | |
cell = cells["C4"]; | |
cell.PutValue("Chai"); | |
cell = cells["C5"]; | |
cell.PutValue("Maxilaku"); | |
cell = cells["C6"]; | |
cell.PutValue("Chang"); | |
cell = cells["C7"]; | |
cell.PutValue("Chang"); | |
cell = cells["C8"]; | |
cell.PutValue("Chang"); | |
cell = cells["C9"]; | |
cell.PutValue("Chang"); | |
cell = cells["C10"]; | |
cell.PutValue("Chang"); | |
cell = cells["C11"]; | |
cell.PutValue("Geitost"); | |
cell = cells["C12"]; | |
cell.PutValue("Chai"); | |
cell = cells["C13"]; | |
cell.PutValue("Geitost"); | |
cell = cells["C14"]; | |
cell.PutValue("Geitost"); | |
cell = cells["C15"]; | |
cell.PutValue("Geitost"); | |
cell = cells["D2"]; | |
cell.PutValue("Asia"); | |
cell = cells["D3"]; | |
cell.PutValue("Asia"); | |
cell = cells["D4"]; | |
cell.PutValue("Asia"); | |
cell = cells["D5"]; | |
cell.PutValue("Asia"); | |
cell = cells["D6"]; | |
cell.PutValue("Europe"); | |
cell = cells["D7"]; | |
cell.PutValue("Europe"); | |
cell = cells["D8"]; | |
cell.PutValue("Europe"); | |
cell = cells["D9"]; | |
cell.PutValue("Europe"); | |
cell = cells["D10"]; | |
cell.PutValue("Europe"); | |
cell = cells["D11"]; | |
cell.PutValue("America"); | |
cell = cells["D12"]; | |
cell.PutValue("America"); | |
cell = cells["D13"]; | |
cell.PutValue("America"); | |
cell = cells["D14"]; | |
cell.PutValue("America"); | |
cell = cells["D15"]; | |
cell.PutValue("America"); | |
cell = cells["E2"]; | |
cell.PutValue("China"); | |
cell = cells["E3"]; | |
cell.PutValue("India"); | |
cell = cells["E4"]; | |
cell.PutValue("Korea"); | |
cell = cells["E5"]; | |
cell.PutValue("India"); | |
cell = cells["E6"]; | |
cell.PutValue("France"); | |
cell = cells["E7"]; | |
cell.PutValue("France"); | |
cell = cells["E8"]; | |
cell.PutValue("Germany"); | |
cell = cells["E9"]; | |
cell.PutValue("Italy"); | |
cell = cells["E10"]; | |
cell.PutValue("France"); | |
cell = cells["E11"]; | |
cell.PutValue("U.S."); | |
cell = cells["E12"]; | |
cell.PutValue("U.S."); | |
cell = cells["E13"]; | |
cell.PutValue("Brazil"); | |
cell = cells["E14"]; | |
cell.PutValue("U.S."); | |
cell = cells["E15"]; | |
cell.PutValue("U.S."); | |
cell = cells["F2"]; | |
cell.PutValue(2000); | |
cell = cells["F3"]; | |
cell.PutValue(500); | |
cell = cells["F4"]; | |
cell.PutValue(1200); | |
cell = cells["F5"]; | |
cell.PutValue(1500); | |
cell = cells["F6"]; | |
cell.PutValue(500); | |
cell = cells["F7"]; | |
cell.PutValue(1500); | |
cell = cells["F8"]; | |
cell.PutValue(800); | |
cell = cells["F9"]; | |
cell.PutValue(900); | |
cell = cells["F10"]; | |
cell.PutValue(500); | |
cell = cells["F11"]; | |
cell.PutValue(1600); | |
cell = cells["F12"]; | |
cell.PutValue(600); | |
cell = cells["F13"]; | |
cell.PutValue(2000); | |
cell = cells["F14"]; | |
cell.PutValue(500); | |
cell = cells["F15"]; | |
cell.PutValue(900); | |
// Adding a new List Object to the worksheet | |
Aspose.Cells.Tables.ListObject listObject = sheet.ListObjects[sheet.ListObjects.Add("A1", "F15", true)]; | |
// Adding Default Style to the table | |
listObject.TableStyleType = Aspose.Cells.Tables.TableStyleType.TableStyleMedium10; | |
// Show Total | |
listObject.ShowTotals = true; | |
// Set the Quarter field's calculation type | |
listObject.ListColumns[1].TotalsCalculation = Aspose.Cells.Tables.TotalsCalculation.Count; | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xlsx"); |