FastReport中国社区FastReport联系电话 联系电话:023-68661681

如何在报表控件FastReport.NET中连接XLSX 文件作为数据源?

来源:   发布时间:2022-08-15   浏览:次

本文主要介绍如何在FastReport.NET中用一个插件来连接XLSX 文件作为数据源。

FastReport.NET

FastReport.NET官方版下载

若要使用它,必须首先生成项目:

С:\Program Files (x86)\FastReports\FastReport.Net\Extras\Core\FastReport.Data\FastReport.Data.Excel

构建项目后,您需要通过以下两种方式之一将插件添加到应用程序中。

  1. 通过设计器连接插件:
FastReport.NET
  1. 在启动项目时将插件添加为依赖项,并使用以下命令在代码中注册它:
    FastReport.Utils.RegisteredObjects.AddConnection(typeof(ExcelDataConnection));
    要创建与Excel的连接,您需要单击设计器中的“数据”选项卡,然后选择“添加数据源”项。在出现的窗口中,单击“新建连接”按钮。要进行连接,您需要 .XLSX 文件。如果访问文件没有问题,则在单击“下一步”按钮后将显示表列表。连接表时,必须选中表名称左侧的框。之后,您可以完成连接。
FastReport.NET

数据源连接完成后,需要将波段连接到该波段。
FastReport.NET
最终报表将使用从创建的 Excel 连接到的数据。
FastReport.NET
从代码连接到 Excel 的示例:
// Create ExcelDataConnection instance
var connection = new ExcelDataConnection();
// Set connection string 
connection.ConnectionString = @"C:\Matrix With Rows Only.xlsx";
// Initialize all table
connection.CreateAllTables();
// Set name connection 
connection.Name = "NewConnection";
// Create Report instance
var report = new Report();
// Add connection to report
report.Dictionary.Connections.Add(connection);
// Set connection show
connection.Enabled = true;
// Choose all tables and connect it to the report
foreach (TableDataSource table in connection.Tables)
{
 table.Enabled = true;
}

执行此代码,我们可以在设计器中看到与表的新连接。这将显示在可用连接列表中。

FastReport.NET

值得注意的是,“工作表”的名称用作表名称,列的名称用作字段名称。

本次FastReport.NET使用教程就介绍到这里了, 更多产品授权信息点击查看FastReport.NET价格,或者咨询慧都在线客服。

FastReport.NET | 在线试用


FastReport.NET技术QQ群:536197826     欢迎进群一起讨论

本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:FastReport控件中文网 [https://www.fastreportcn.com/]
本文地址:https://www.fastreportcn.com/post/3383.html

联系我们
  • 重庆总部 023-68661681
购买
  • sales@evget.com
合作
  • business@evget.com


在线
客服
微信
QQ 电话
023-68661681
返回
顶部