我花了很多天尝试在论坛/ samples / github上找到的所有内容,我想做什么: - 我在Sql Server中有一个表 - 我的单页应用程序中有一个报表设计器(Stimulsoft reports.js) - 我需要用表的行填充报表(以便用户可以预览报表)。
目前,报告模板是使用以下代码在服务器端(asp.net核心)创建的:
[HttpPost]
public override async Task Post([FromBody] Report[] elements)
{
var report = new StiReport();
var ds = GetDocumentDataSet();
report.RegData(AppName, ds);
report.Dictionary.Synchronize();
foreach (var element in elements)
element.Layout = report.SaveToJsonString();
return await base.Post(elements);
}
private DataSet GetDocumentDataSet()
{
DataSet ds = new DataSet();
using (var connection = Use().GetConnection())
{
using (var command = connection.CreateCommand())
{
var table = new DataTable($"{nameof(Document)}");
command.CommandText = $"select top 1 * from {nameof(Document)}";
connection.Open();
table.Load(command.ExecuteReader());
ds.Tables.Add(table);
}
}
return ds;
}
此代码将表格,列与其类型放在词典中,它可以正常工作。客户端,当我使用此代码加载报告时:
report.load(dataReport.Layout);
我可以在词典中看到这一点:http://imgur.com/a/SJ4Yr
那很酷,我现在只提供数据,服务器端我这样得到数据:
public async Task ReportSearch()
{
var rows = new List();
var columns = new List();
var types = new List();
using (var connection = Use().GetConnection())
using (var command = connection.CreateCommand())
{
command.CommandText = $"select * from {nameof(Document)}";
connection.Open();
var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
var values = new object[reader.FieldCount];
reader.GetValues(values);
rows.Add(values);
}
foreach (DataRow c in reader.GetSchemaTable().Rows)
{
columns.Add((string)c["ColumnName"]);
types.Add(((Type)c["DataType"]).FullName);
}
}
return new // As seen in your github sample
{
columns = columns,
notice = "",
types = types,
rows = rows,
success = true
};
}
在客户端,我尝试以这种方式填写:
const report = new Stimulsoft.Report.StiReport();
if (dataReport.Layout != null && dataReport.Layout != "") {
report.load(dataReport.Layout);
}
$.ajax({
type: "Post",
url: "api/Search/ReportSearch",
dataType: "json",
contentType: "application/json",
success: (data) => {
var dataSet = new Stimulsoft.System.Data.DataSet(appName);
dataSet.readJson(JSON.stringify(data));
report.regData(dataSet.dataSetName, dataSet.dataSetName, dataSet);
report.dictionary.synchronize();
designer.report = report;
}
});
但我得到了这个:(:http://imgur.com/a/FQ9kQ
相信我,我已经尝试了所有我找到的东西。我更愿意将数据作为表(对象数组的数组)而不是json对象的数组发送,因为每个对象具有相同的属性,因此第一个方法比第二个方法更简单,传输/更快解析。
有人可以帮忙吗?
答案 0 :(得分:0)
您应该使用GitHub行(74-86)中的样本:
if (requestParams.Connection.Type == StiConnectionType.MSSQL)
{
string connectionString = requestParams.Connection.ConnectionString;
string queryString = requestParams.Connection.QueryString;
StiDataResult result = new StiDataResult()
{
Columns = new string[2] { "Column1", "Column2" },
Types = new string[2] { "number", "string" },
Rows = new string[3][] { new string[2] { "1", "Row1" }, new string[2] { "2", "Row2" }, new string[2] { "3", "Row3" } }
};
return StiNetCoreViewer.GetReportDataResult(this, result);
}