65.9K
CodeProject 正在变化。 阅读更多。
Home

The Dew Review - Aspose.Cells for .NET

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2014 年 9 月 26 日

CPOL

6分钟阅读

viewsIcon

22800

去年六月,我评测了 Aspose.Email for .NET 组件,并演示了如何处理 PST 文件中的电子邮件或通过 IMAP 处理。好了,我又回来评测另一个 Aspose 套件了。这次是 Aspose.Cells for .NET。

引言

又到了进行开发组件评测的时候了。去年六月,我评测了 Aspose.Email for .NET 组件,并演示了如何处理 PST 文件中的电子邮件或通过 IMAP 处理。好了,我又回来评测另一个 Aspose 套件了。这次是 Aspose.Cells for .NET

Aspose.Cells 提供对数百种 Excel 文件功能的完全访问,无需安装或分发 Microsoft Excel。

Aspose.Cells 功能

功能的完整列表,请访问 Aspose.Cells 网站。这里是其中一些亮点的摘要。

文件操作

Aspose.Cells 支持如此多的文件格式,包括 Excel (XLS, XLSM 等)、HTML、CSV、制表符分隔和 PDF。开发人员可以打开/保存文件、加密已保存的文件、将 Excel 文档转换为 PDF,甚至可以将工作表保存为 SVG 文件。您还可以操作现有 Excel 文件的文件属性。

需要将工作簿的一部分捕获到图像文件中吗?有可用的 API 可以将图表和整个工作表导出为图像,然后可以在 .NET 中像任何其他位图一样保存或操作。

工作表、行和列

Aspose.Cells 具有工作表 API,可以向工作簿添加和删除工作表。使用这些 API 还可以将新工作表添加到现有的 PDF 文件中。在工作表中,开发人员可以操作滚动条可见性、选项卡、缩放因子以及冻结和拆分工作表窗格。

您还可以利用用于处理工作表上行和列的 API。您的应用程序可以插入、删除、复制、隐藏/取消隐藏以及调整行高/列宽。

数据和格式

Aspose.Cells 可以从多种不同来源导入数据到工作表中,包括

  • 数组和 ArrayList
  • 自定义 .NET 对象和集合
  • DataTable / DataView / DataColumn
  • 手动数据录入

一旦数据进入工作表,就可以对其进行排序、访问和搜索。

Aspose.Cells 包含一个公式引擎,它可以与现有电子表格中嵌入的公式一起运行,或者与运行时创建的新公式一起运行。

想象一下您可以在 Excel 中的单元格中执行的任何类型的数据格式设置,您都可以使用这些 API 来完成……字体、颜色、文本格式……您说得出来,它都能做到。

表格和图表

使用 Aspose.Cells 可以轻松地将列表格式化为工作表上的表格。一旦成为表格的一部分,数据就可以进行样式设置、格式化、分组和汇总。

图表是 Excel 和 Aspose.Cells 中最强大的功能之一。我将在下面的示例应用程序中展示图表 API 的示例。我的应用程序中未展示的其他功能包括 3D 格式设置、在图表中插入控件(标签、图片、文本框)以及 Excel 数据透视表

跨平台支持

Aspose.Cells 库不仅功能众多,而且您还可以从所有这些平台访问这些功能

  • .NET Framework
  • PHP
  • Python
  • Mono

示例应用程序 – 数据、数据透视表和图表,哇!

和上次一样,我在开始使用 Aspose 开发应用程序时,立即打开了“示例仪表板”,以了解如何实现我的应用程序所需的功能。

其他 NuGet 包

我使用了几个其他的 NuGet 包来简化我的应用程序开发。

MVVM Light – 这是我首选的 MVVM 辅助框架。它使 WPF 和其他 XAML 应用程序中的绑定和消息传递变得轻而易举。

CsvHelper – 此包提供了处理 CSV 文件的便捷 API。因为我想将数据从 CSV 文件导入到 .NET 集合中。我本可以使用 Aspose.Cells 直接导入 CSV 数据,但我想看看该库如何与集合配合使用,以防我的数据来自其他来源,例如 REST 服务或其他 API。

应用程序

应用程序本身相对简单。我首先从一个 CSV 文件加载一些数据,该文件包含站点和作者列表以及他们在一个季度内获得的访问次数。我使用 CsvHelper 将此加载到 List<AuthorSummary> 集合中,然后将集合导入到我在 Aspose.Cells 中创建的内存中工作簿的一个工作表中。

// The path to the documents directory.
_dataDir = Path.GetFullPath("../../../Data/");
 
// Create directory if it is not already present.
bool IsExists = Directory.Exists(_dataDir);
if (!IsExists)
    Directory.CreateDirectory(_dataDir);
 
//Instantiating a Workbook object
_workbook = new Workbook();
 
//Obtaining the reference of the default first worksheet by passing its sheet index
Worksheet worksheet = _workbook.Worksheets[0];
 
var authorSummaries = new List<AuthorSummary>();
 
using (var csv = new CsvReader(new StreamReader("SitesChartSample.csv"), new CsvHelper.Configuration.CsvConfiguration { HasHeaderRecord = true, Delimiter = "," }))
{
    while (csv.Read())
    {
        var authorSummary = new AuthorSummary
                            {
                                Site = csv.GetField<string>(0),
                                Author = csv.GetField<string>(1),
                                Visits = csv.GetField<int>(2),
                                Quarter = csv.GetField<string>(3)
                            };
 
        authorSummaries.Add(authorSummary);
    }
}
 
var options = new ImportTableOptions { InsertRows = true };
 
worksheet.Cells.ImportCustomObjects(authorSummaries, 1, 0, options);

下一步是创建一个数据透视表。我希望更好地可视化每个站点在每个季度获得的访问次数,同时仍然可以看到按作者的细分。我使用 Aspose.Cells 添加一个新的 PivotTable 对象,并将其指向用于作为源数据单元格范围。然后,我指定要用作数据透视表行、列和数据的字段。

Aspose.Cells.Pivot.PivotTableCollection pivotTables = worksheet.PivotTables;
int index = pivotTables.Add("=A2:D26", "F2", "VisitsBySiteAndQuarter");
 
//Accessing the instance of the newly added PivotTable
Aspose.Cells.Pivot.PivotTable pivotTable = pivotTables[index];
 
//Unshowing grand totals for rows.
pivotTable.RowGrand = false;
 
//Dragging the first field to the row area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 0);
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Row, 1);
 
//Dragging the second field to the column area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Column, 3);
 
//Dragging the third field to the data area.
pivotTable.AddFieldToArea(Aspose.Cells.Pivot.PivotFieldType.Data, 2);
 
pivotTable.CalculateData();

最后一步是基于数据透视表中的摘要数据创建一个图表。我使用 Aspose.Cells 向工作表添加一个新的 Chart 对象,然后添加要包含在图表中的每个数据系列。

//Adding a chart to the worksheet
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 2, 12, 14, 18);
 
//Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
 
//Adding SeriesCollections (chart data sources) to the chart
chart.NSeries.Add("H10:K10", false);
chart.NSeries[0].Name = "C#";
chart.NSeries.Add("H16:K16", false);
chart.NSeries[1].Name = "VB";
chart.NSeries.Add("H20:K20", false);
chart.NSeries[2].Name = "F#";
chart.NSeries.Add("H31:K31", false);
chart.NSeries[3].Name = "JavaScript";

chart.Title.Text = "Quarterly Visits By Site";

我最后添加的一些内容是样式/格式设置。我想看看我能多轻松地更改图表的外观。大部分代码都摘自 Aspose.Cells 安装的“示例仪表板”应用程序中的一个示例。

private void SetChartAppearance(Aspose.Cells.Charts.Chart chart)
{
    //Setting the foreground color of the plot area
    chart.PlotArea.Area.ForegroundColor = Color.Blue;
 
    //Setting the foreground color of the chart area
    chart.ChartArea.Area.ForegroundColor = Color.Yellow;
 
    //Setting the foreground color of the 1st SeriesCollection area
    chart.NSeries[0].Area.ForegroundColor = Color.Red;
 
    //Setting the foreground color of the area of the 1st SeriesCollection point
    chart.NSeries[0].Points[0].Area.ForegroundColor = Color.Cyan;
 
    //Filling the area of the 2nd SeriesCollection with a gradient
    chart.NSeries[3].Area.FillFormat.SetOneColorGradient(Color.Lime, 1, Aspose.Cells.Drawing.GradientStyleType.Horizontal, 1);
 
    //Get the CellsColor of SolidFill
    CellsColor cc = chart.NSeries[0].Area.FillFormat.SolidFill.CellsColor;
 
    //Create a theme in Accent style
    cc.ThemeColor = new ThemeColor(ThemeColorType.Accent6, 0.6);
 
    //Apply the them to the series
    chart.NSeries[0].Area.FillFormat.SolidFill.CellsColor = cc;
}

这是图表导出为图像并在我的 WPF 窗体上显示时的输出

好吧,我不是平面设计师。毕竟我是一名开发人员。

导出图表和保存文件

当 WPF 窗体加载并且 ViewModel 为图表准备数据时,它会将图表导出为 Bitmap 对象,该对象会被转换为 BitmapSource,然后可以绑定到窗体上的 WPF Image 控件。我在 StackOverflow 上找到了这段方便的图像转换代码。

ChartImage = LoadBitmap(chart.ToImage());

[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);
 
private static BitmapSource LoadBitmap(Bitmap image)
{
    IntPtr pointer = image.GetHbitmap();
    BitmapSource bitmapSource;
 
    try
    {
        bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(pointer,
           IntPtr.Zero, Int32Rect.Empty,
           BitmapSizeOptions.FromEmptyOptions());
    }
    finally
    {
        DeleteObject(pointer);
    }
 
    return bitmapSource;
}

窗体上的两个按钮绑定到命令,用于将工作簿另存为 Excel 文件(.xls)或 PDF 文档。执行每个操作的代码非常相似且直观。我还添加了一些代码,以便在保存后打开每个文件,以便我可以看到结果。

private void SavePDF()
{
    //Save in Pdf format
    _workbook.Save(_dataDir + "book1.pdf", SaveFormat.Pdf);
 
    Process.Start(_dataDir + "book1.pdf");
}
 
private void SaveXLS()
{
    //Saving the Excel file
    _workbook.Save(_dataDir + "book1.xls");
 
    Process.Start(_dataDir + "book1.xls");
}

该项目的完整源代码可以在 此处找到。试用版的 Aspose.Cells 产品功能齐全,但会在创建的所有文件中添加水印。

摘要

如果您正在寻找一个能够快速启动您的电子表格应用程序的库,Aspose.Cells 应该位列您的首选。我发现 API 非常直观,文档和示例也非常详尽和全面。我非常享受第二次使用 Aspose 产品的体验。

编码愉快!

材料联系披露:我免费收到了上述一个或多个产品或服务,希望能在我的博客上提及。无论如何,我只推荐我个人使用并且认为我的读者会喜欢的产品或服务。我根据美国联邦贸易委员会的 16 CFR 第 255 部分:“广告中使用代言和推荐证言的指南”披露这一点。

© . All rights reserved.