尝试导出快速报告报告时找不到元数据文件 'Microsoft.CSharp' 错误
Getting a Metadata file 'Microsoft.CSharp' could not be found error upon trying to export a fastreport report
尝试使用 fastreport 开源版本准备报告并将其导出为 pdf
使用 Microsoft Sql Server 数据库作为它的数据源
我的代码似乎连接到数据库并能够填充数据源,所以问题很可能出在创建报告本身的代码中,但我不确切地看到它可能发生在哪里
异常发生在report.Prepare();
以及它之后的那一行
using FastReport;
using FastReport.Data;
using FastReport.Export.Image;
using FastReport.Export.PdfSimple;
using FastReport.Format;
using FastReport.Table;
using FastReport.Utils;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows;
namespace KursovayaAvtoparkAvtobusov
{
/// <summary>
/// Interaction logic for ReportExport.xaml
/// </summary>
public partial class ReportExport : Window
{
private static string outFolder = @"..\..\..\out\";
private static string inFolder = @"..\..\..\in\";
public ReportExport()
{
InitializeComponent();
}
private void SimpleRepExport_Click(object sender, RoutedEventArgs e)
{
Report report = new Report();
DataSet ds = new DataSet();
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["KursovayaAvtoparkAvtobusov"].ToString();
sqlConnection.Open();
var select = "SELECT * FROM Employees; SELECT* FROM Maintenance; SELECT* FROM Marshuti; SELECT* FROM Prodazhi";
var commandBuilder = new SqlCommand(select, sqlConnection);
commandBuilder.ExecuteNonQuery();
var adapter = new SqlDataAdapter(commandBuilder);
adapter.TableMappings.Add("Table", "Employees");
adapter.TableMappings.Add("Table1", "Maintenance");
adapter.TableMappings.Add("Table2", "Marshuti");
adapter.TableMappings.Add("Table3", "Prodazhi");
adapter.Fill(ds);
MessageBox.Show(ds.Tables[0].TableName);
MessageBox.Show(ds.Tables[1].TableName);
MessageBox.Show(ds.Tables[2].TableName);
MessageBox.Show(ds.Tables[3].TableName);
report.RegisterData(ds.Tables["Employees"], "Employees");
// enable it to use in a report
report.GetDataSource("Employees").Enabled = true;
// create A4 page with all margins set to 1cm
ReportPage page1 = new ReportPage();
page1.Name = "Page1";
report.Pages.Add(page1);
// create a ReportTitle band
page1.ReportTitle = new ReportTitleBand();
page1.ReportTitle.Name = "ReportTitle1";
// set its height to 1.5cm
page1.ReportTitle.Height = Units.Centimeters * 1.5f;
// create group header
GroupHeaderBand group1 = new GroupHeaderBand();
group1.Name = "GroupHeader1";
group1.Height = Units.Centimeters * 1;
// set group condition
group1.Condition = "[Employees.Surname].Substring(0, 1)";
// add group to the page.Bands collection
page1.Bands.Add(group1);
// create group footer
group1.GroupFooter = new GroupFooterBand();
group1.GroupFooter.Name = "GroupFooter1";
group1.GroupFooter.Height = Units.Centimeters * 1;
// create DataBand
DataBand data1 = new DataBand();
data1.Name = "Data1";
data1.Height = Units.Centimeters * 0.5f;
// set data source
data1.DataSource = report.GetDataSource("Employees");
// connect databand to a group
group1.Data = data1;
// create "Text" objects
// report title
TextObject text1 = new TextObject();
text1.Name = "Text1";
// set bounds
text1.Bounds = new RectangleF(0, 0,
Units.Centimeters * 19, Units.Centimeters * 1);
// set text
text1.Text = "Employees";
// set appearance
text1.HorzAlign = HorzAlign.Center;
text1.Font = new Font("Tahoma", 14);
// add it to ReportTitle
page1.ReportTitle.Objects.Add(text1);
// group
TextObject text2 = new TextObject();
text2.Name = "Text2";
text2.Bounds = new RectangleF(0, 0,
Units.Centimeters * 2, Units.Centimeters * 1);
text2.Text = "[[Employees.Name].Substring(0, 1)]";
text2.Font = new Font("Tahoma", 10);
// add it to the GroupHeader
group1.Objects.Add(text2);
// data band
TextObject text3 = new TextObject();
text3.Name = "Text3";
text3.Bounds = new RectangleF(0, 0,
Units.Centimeters * 10, Units.Centimeters * 0.5f);
text3.Text = "[Employees.Patronym]";
text3.Font = new Font("Tahoma", 8);
// add it to DataBand
data1.Objects.Add(text3);
report.Prepare();
PDFSimpleExport pdf = new PDFSimpleExport();
// Save the report
report.Export(pdf, "ExportedPDF.pdf");
}
}
}
文件中的源代码
异常详情
FastReport.Utils.CompilerException
HResult=0x80131500
Message=(0,0): Error CS0006: Metadata file 'Microsoft.CSharp' could not be found
Source=FastReport
StackTrace:
at FastReport.Code.AssemblyDescriptor.InternalCompile()
at FastReport.Code.AssemblyDescriptor.Compile()
at FastReport.Report.Prepare(Boolean append)
at KursovayaAvtoparkAvtobusov.ReportExport.SimpleRepExport_Click(Object sender, RoutedEventArgs e) in C:\Users\AndreySavich\source\repos\KursovayaAvtoparkAvtobusov\KursovayaAvtoparkAvtobusov\ReportExport.xaml.cs:line 117
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at KursovayaAvtoparkAvtobusov.App.Main()
预期行为将导出为 pdf 文件报告,可以在 pdf 查看器中查看
而且我设法修复了它
基本上该错误的原因是使用了错误类型的 sqlconnection
重新编写了代码
这个
using FastReport;
using FastReport.Data;
using FastReport.Export.Image;
using FastReport.Export.PdfSimple;
using FastReport.Format;
using FastReport.Table;
using FastReport.Utils;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows;
namespace KursovayaAvtoparkAvtobusov
{
/// <summary>
/// Interaction logic for ReportExport.xaml
/// </summary>
public partial class ReportExport : Window
{
private static string outFolder = @"..\..\..\out\";
private static string inFolder = @"..\..\..\in\";
public ReportExport()
{
InitializeComponent();
}
private void SimpleRepExport_Click(object sender, RoutedEventArgs e)
{
Report report = new Report();
DataSet ds = new DataSet();
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["KursovayaAvtoparkAvtobusov"].ToString();
sqlConnection.Open();
var select = "SELECT * FROM Employees; SELECT* FROM Maintenance; SELECT* FROM Marshuti; SELECT* FROM Prodazhi";
var commandBuilder = new SqlCommand(select, sqlConnection);
commandBuilder.ExecuteNonQuery();
var adapter = new SqlDataAdapter(commandBuilder);
adapter.TableMappings.Add("Table", "Employees");
adapter.TableMappings.Add("Table1", "Maintenance");
adapter.TableMappings.Add("Table2", "Marshuti");
adapter.TableMappings.Add("Table3", "Prodazhi");
adapter.Fill(ds);
MessageBox.Show(ds.Tables[0].TableName);
MessageBox.Show(ds.Tables[1].TableName);
MessageBox.Show(ds.Tables[2].TableName);
MessageBox.Show(ds.Tables[3].TableName);
report.RegisterData(ds.Tables["Employees"], "Employees");
// enable it to use in a report
report.GetDataSource("Employees").Enabled = true;
// create A4 page with all margins set to 1cm
ReportPage page1 = new ReportPage();
page1.Name = "Page1";
report.Pages.Add(page1);
// create a ReportTitle band
page1.ReportTitle = new ReportTitleBand();
page1.ReportTitle.Name = "ReportTitle1";
// set its height to 1.5cm
page1.ReportTitle.Height = Units.Centimeters * 1.5f;
// create group header
GroupHeaderBand group1 = new GroupHeaderBand();
group1.Name = "GroupHeader1";
group1.Height = Units.Centimeters * 1;
// set group condition
group1.Condition = "[Employees.Surname].Substring(0, 1)";
// add group to the page.Bands collection
page1.Bands.Add(group1);
// create group footer
group1.GroupFooter = new GroupFooterBand();
group1.GroupFooter.Name = "GroupFooter1";
group1.GroupFooter.Height = Units.Centimeters * 1;
// create DataBand
DataBand data1 = new DataBand();
data1.Name = "Data1";
data1.Height = Units.Centimeters * 0.5f;
// set data source
data1.DataSource = report.GetDataSource("Employees");
// connect databand to a group
group1.Data = data1;
// create "Text" objects
// report title
TextObject text1 = new TextObject();
text1.Name = "Text1";
// set bounds
text1.Bounds = new RectangleF(0, 0,
Units.Centimeters * 19, Units.Centimeters * 1);
// set text
text1.Text = "Employees";
// set appearance
text1.HorzAlign = HorzAlign.Center;
text1.Font = new Font("Tahoma", 14);
// add it to ReportTitle
page1.ReportTitle.Objects.Add(text1);
// group
TextObject text2 = new TextObject();
text2.Name = "Text2";
text2.Bounds = new RectangleF(0, 0,
Units.Centimeters * 2, Units.Centimeters * 1);
text2.Text = "[[Employees.Name].Substring(0, 1)]";
text2.Font = new Font("Tahoma", 10);
// add it to the GroupHeader
group1.Objects.Add(text2);
// data band
TextObject text3 = new TextObject();
text3.Name = "Text3";
text3.Bounds = new RectangleF(0, 0,
Units.Centimeters * 10, Units.Centimeters * 0.5f);
text3.Text = "[Employees.Patronym]";
text3.Font = new Font("Tahoma", 8);
// add it to DataBand
data1.Objects.Add(text3);
report.Prepare();
PDFSimpleExport pdf = new PDFSimpleExport();
// Save the report
report.Export(pdf, "ExportedPDF.pdf");
}
}
}
至此
using FastReport;
using FastReport.Data;
using FastReport.Export.Image;
using FastReport.Export.PdfSimple;
using FastReport.Format;
using FastReport.Table;
using FastReport.Utils;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows;
namespace KursovayaAvtoparkAvtobusov
{
/// <summary>
/// Interaction logic for ReportExport.xaml
/// </summary>
public partial class ReportExport : Window
{
private static string outFolder = @"..\..\..\out\";
private static string inFolder = @"..\..\..\in\";
public ReportExport()
{
InitializeComponent();
}
/*private void Fucttardmethod()
{
RegisteredObjects.AddConnection(typeof(MsSqlDataConnection));
Report report = new Report();
MsSqlDataConnection sqlConnection = new MsSqlDataConnection();
sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["KursovayaAvtoparkAvtobusov"].ToString();
sqlConnection.CreateAllTables();
report.Report.Dictionary.Connections.Add(sqlConnection);
var select = "SELECT * FROM Employees; SELECT* FROM Maintenance; SELECT* FROM Marshuti; SELECT* FROM Prodazhi";
report.GetDataSource("Employees").Enabled = true;
report.GetDataSource("Maintenance").Enabled = true;
report.GetDataSource("Marshuti").Enabled = true;
report.GetDataSource("Prodazhi").Enabled = true;
report.Prepare();
PDFSimpleExport pdf = new PDFSimpleExport();
// Save the report
report.Export(pdf, "ExportedPDF.pdf");
}*/
private void SimpleRepExport_Click(object sender, RoutedEventArgs e)
{
RegisteredObjects.AddConnection(typeof(MsSqlDataConnection));
Report report = new Report();
MsSqlDataConnection connection = new MsSqlDataConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["KursovayaAvtoparkAvtobusov"].ToString();
connection.CreateAllTables();
report.Dictionary.Connections.Add(connection);
report.RegisterData(connection.DataSet, "mssql");
report.GetDataSource("Employees").Enabled = true;
ReportPage page = new ReportPage();
report.Pages.Add(page);
page.CreateUniqueName();
page.TopMargin = 10.0f;
page.LeftMargin = 10.0f;
page.RightMargin = 10.0f;
page.BottomMargin = 10.0f;
DataBand data = new DataBand();
data.CreateUniqueName();
page.AddChild(data);
data.DataSource = report.GetDataSource("Employees"); //adding a table to a bend
data.Height = Units.Centimeters * 5f;
// Creating report title
page.ReportTitle = new ReportTitleBand();
page.ReportTitle.CreateUniqueName();
page.ReportTitle.Height = 4.0f * Units.Centimeters;
TextObject titleText = new TextObject();
titleText.CreateUniqueName();
titleText.Left = 1.0f * Units.Centimeters;
titleText.Top = 1.0f * Units.Centimeters;
titleText.Width = 17.0f * Units.Centimeters;
titleText.Height = 2.0f * Units.Centimeters;
titleText.HorzAlign = HorzAlign.Center;
titleText.VertAlign = VertAlign.Center;
titleText.Font = new Font("Times New Roman", 32.0f);
titleText.TextColor = Color.Black;
titleText.FillColor = Color.Wheat;
titleText.Border.Color = Color.Black;
titleText.Border.Lines = BorderLines.All;
titleText.Border.Width = 4.0f;
titleText.Text = "Avtopark Report";
page.ReportTitle.Objects.Add(titleText);
// Header object
TextObject headerText = new TextObject();
headerText.CreateUniqueName();
headerText.Bounds = new RectangleF(0.0f, 2.7f * Units.Centimeters,
19.0f * Units.Centimeters, 1.0f * Units.Centimeters);
headerText.HorzAlign = HorzAlign.Center;
headerText.VertAlign = VertAlign.Center;
headerText.Font = new Font("Times New Roman", 16.0f);
headerText.TextColor = Color.Black;
headerText.FillColor = Color.Wheat;
headerText.Border.Color = Color.Black;
headerText.Border.Lines = BorderLines.All;
headerText.Border.Width = 4.0f;
headerText.Text = "Employees" + " Table";
page.ReportTitle.Objects.Add(headerText);
// Text bands
TextObject bandText = new TextObject();
bandText.CreateUniqueName();
bandText.HorzAlign = HorzAlign.Center;
bandText.Bounds = new RectangleF(0.0f * Units.Centimeters, 0.25f,
1.0f * Units.Centimeters, 0.5f * Units.Centimeters);
bandText.Border.Lines = BorderLines.All;
bandText.Text = "[Employees.Num]";
data.AddChild(bandText);
TextObject text1 = new TextObject();
text1.Parent = data;
text1.CreateUniqueName();
text1.Bounds = new RectangleF(1 * Units.Centimeters, 0, Units.Centimeters * 3, Units.Centimeters * 0.5f);
text1.Text = "[Employees.Surname]"; //table and its field
text1.Border.Lines = BorderLines.All;
TextObject text2 = new TextObject();
text2.Parent = data;
text2.CreateUniqueName();
text2.Bounds = new RectangleF(4 * Units.Centimeters, 0, Units.Centimeters * 20, Units.Centimeters * 0.5f);
text2.Text = "[Employees.Name]"; //table and its field
text2.Border.Lines = BorderLines.All;
TextObject text3 = new TextObject();
text3.Parent = data;
text3.CreateUniqueName();
text3.Bounds = new RectangleF(10 * Units.Centimeters, 0, Units.Centimeters * 15, Units.Centimeters * 0.5f);
text3.Text = "[Employees.Patronym]"; //table and its field
text3.Border.Lines = BorderLines.All;
TextObject text4 = new TextObject();
text4.Parent = data;
text4.CreateUniqueName();
text4.Bounds = new RectangleF(13 * Units.Centimeters, 0, Units.Centimeters * 5, Units.Centimeters * 0.5f);
text4.Text = "[Employees.Employed_Since]"; //table and its field
text4.Border.Lines = BorderLines.All;
TextObject text5 = new TextObject();
text5.Parent = data;
text5.CreateUniqueName();
text5.Bounds = new RectangleF(15 * Units.Centimeters, 0, Units.Centimeters * 5, Units.Centimeters * 0.5f);
text5.Text = "[Employees.Job]"; //table and its field
text5.Border.Lines = BorderLines.All;
TextObject text6 = new TextObject();
text6.Parent = data;
text6.CreateUniqueName();
text6.Bounds = new RectangleF(18 * Units.Centimeters, 0, Units.Centimeters * 5, Units.Centimeters * 0.5f);
text6.Text = "[Employees.Internship]"; //table and its field
text6.Border.Lines = BorderLines.All;
// Footer
page.PageFooter = new PageFooterBand();
page.PageFooter.CreateUniqueName();
page.PageFooter.Height = 0.5f * Units.Centimeters;
TextObject footerText = new TextObject();
footerText.CreateUniqueName();
footerText.HorzAlign = HorzAlign.Right;
footerText.VertAlign = VertAlign.Center;
footerText.Bounds = new RectangleF(0.0f, 0.0f,
19.0f * Units.Centimeters, 0.5f * Units.Centimeters);
footerText.TextColor = Color.Black;
footerText.FillColor = Color.Wheat;
titleText.Border.Color = Color.Black;
titleText.Border.Lines = BorderLines.All;
titleText.Border.Width = 4.0f;
footerText.Text = "Page [Page]";
page.PageFooter.Objects.Add(footerText);
// Exporting the report
report.Prepare();
PDFSimpleExport pdf = new PDFSimpleExport();
// Save the report
report.Export(pdf, "ExportedPDF.pdf");
}
}
}
尝试使用 fastreport 开源版本准备报告并将其导出为 pdf 使用 Microsoft Sql Server 数据库作为它的数据源 我的代码似乎连接到数据库并能够填充数据源,所以问题很可能出在创建报告本身的代码中,但我不确切地看到它可能发生在哪里
异常发生在report.Prepare(); 以及它之后的那一行
using FastReport;
using FastReport.Data;
using FastReport.Export.Image;
using FastReport.Export.PdfSimple;
using FastReport.Format;
using FastReport.Table;
using FastReport.Utils;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows;
namespace KursovayaAvtoparkAvtobusov
{
/// <summary>
/// Interaction logic for ReportExport.xaml
/// </summary>
public partial class ReportExport : Window
{
private static string outFolder = @"..\..\..\out\";
private static string inFolder = @"..\..\..\in\";
public ReportExport()
{
InitializeComponent();
}
private void SimpleRepExport_Click(object sender, RoutedEventArgs e)
{
Report report = new Report();
DataSet ds = new DataSet();
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["KursovayaAvtoparkAvtobusov"].ToString();
sqlConnection.Open();
var select = "SELECT * FROM Employees; SELECT* FROM Maintenance; SELECT* FROM Marshuti; SELECT* FROM Prodazhi";
var commandBuilder = new SqlCommand(select, sqlConnection);
commandBuilder.ExecuteNonQuery();
var adapter = new SqlDataAdapter(commandBuilder);
adapter.TableMappings.Add("Table", "Employees");
adapter.TableMappings.Add("Table1", "Maintenance");
adapter.TableMappings.Add("Table2", "Marshuti");
adapter.TableMappings.Add("Table3", "Prodazhi");
adapter.Fill(ds);
MessageBox.Show(ds.Tables[0].TableName);
MessageBox.Show(ds.Tables[1].TableName);
MessageBox.Show(ds.Tables[2].TableName);
MessageBox.Show(ds.Tables[3].TableName);
report.RegisterData(ds.Tables["Employees"], "Employees");
// enable it to use in a report
report.GetDataSource("Employees").Enabled = true;
// create A4 page with all margins set to 1cm
ReportPage page1 = new ReportPage();
page1.Name = "Page1";
report.Pages.Add(page1);
// create a ReportTitle band
page1.ReportTitle = new ReportTitleBand();
page1.ReportTitle.Name = "ReportTitle1";
// set its height to 1.5cm
page1.ReportTitle.Height = Units.Centimeters * 1.5f;
// create group header
GroupHeaderBand group1 = new GroupHeaderBand();
group1.Name = "GroupHeader1";
group1.Height = Units.Centimeters * 1;
// set group condition
group1.Condition = "[Employees.Surname].Substring(0, 1)";
// add group to the page.Bands collection
page1.Bands.Add(group1);
// create group footer
group1.GroupFooter = new GroupFooterBand();
group1.GroupFooter.Name = "GroupFooter1";
group1.GroupFooter.Height = Units.Centimeters * 1;
// create DataBand
DataBand data1 = new DataBand();
data1.Name = "Data1";
data1.Height = Units.Centimeters * 0.5f;
// set data source
data1.DataSource = report.GetDataSource("Employees");
// connect databand to a group
group1.Data = data1;
// create "Text" objects
// report title
TextObject text1 = new TextObject();
text1.Name = "Text1";
// set bounds
text1.Bounds = new RectangleF(0, 0,
Units.Centimeters * 19, Units.Centimeters * 1);
// set text
text1.Text = "Employees";
// set appearance
text1.HorzAlign = HorzAlign.Center;
text1.Font = new Font("Tahoma", 14);
// add it to ReportTitle
page1.ReportTitle.Objects.Add(text1);
// group
TextObject text2 = new TextObject();
text2.Name = "Text2";
text2.Bounds = new RectangleF(0, 0,
Units.Centimeters * 2, Units.Centimeters * 1);
text2.Text = "[[Employees.Name].Substring(0, 1)]";
text2.Font = new Font("Tahoma", 10);
// add it to the GroupHeader
group1.Objects.Add(text2);
// data band
TextObject text3 = new TextObject();
text3.Name = "Text3";
text3.Bounds = new RectangleF(0, 0,
Units.Centimeters * 10, Units.Centimeters * 0.5f);
text3.Text = "[Employees.Patronym]";
text3.Font = new Font("Tahoma", 8);
// add it to DataBand
data1.Objects.Add(text3);
report.Prepare();
PDFSimpleExport pdf = new PDFSimpleExport();
// Save the report
report.Export(pdf, "ExportedPDF.pdf");
}
}
}
文件中的源代码
异常详情
FastReport.Utils.CompilerException
HResult=0x80131500
Message=(0,0): Error CS0006: Metadata file 'Microsoft.CSharp' could not be found
Source=FastReport
StackTrace:
at FastReport.Code.AssemblyDescriptor.InternalCompile()
at FastReport.Code.AssemblyDescriptor.Compile()
at FastReport.Report.Prepare(Boolean append)
at KursovayaAvtoparkAvtobusov.ReportExport.SimpleRepExport_Click(Object sender, RoutedEventArgs e) in C:\Users\AndreySavich\source\repos\KursovayaAvtoparkAvtobusov\KursovayaAvtoparkAvtobusov\ReportExport.xaml.cs:line 117
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at KursovayaAvtoparkAvtobusov.App.Main()
预期行为将导出为 pdf 文件报告,可以在 pdf 查看器中查看
而且我设法修复了它 基本上该错误的原因是使用了错误类型的 sqlconnection 重新编写了代码 这个
using FastReport;
using FastReport.Data;
using FastReport.Export.Image;
using FastReport.Export.PdfSimple;
using FastReport.Format;
using FastReport.Table;
using FastReport.Utils;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows;
namespace KursovayaAvtoparkAvtobusov
{
/// <summary>
/// Interaction logic for ReportExport.xaml
/// </summary>
public partial class ReportExport : Window
{
private static string outFolder = @"..\..\..\out\";
private static string inFolder = @"..\..\..\in\";
public ReportExport()
{
InitializeComponent();
}
private void SimpleRepExport_Click(object sender, RoutedEventArgs e)
{
Report report = new Report();
DataSet ds = new DataSet();
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["KursovayaAvtoparkAvtobusov"].ToString();
sqlConnection.Open();
var select = "SELECT * FROM Employees; SELECT* FROM Maintenance; SELECT* FROM Marshuti; SELECT* FROM Prodazhi";
var commandBuilder = new SqlCommand(select, sqlConnection);
commandBuilder.ExecuteNonQuery();
var adapter = new SqlDataAdapter(commandBuilder);
adapter.TableMappings.Add("Table", "Employees");
adapter.TableMappings.Add("Table1", "Maintenance");
adapter.TableMappings.Add("Table2", "Marshuti");
adapter.TableMappings.Add("Table3", "Prodazhi");
adapter.Fill(ds);
MessageBox.Show(ds.Tables[0].TableName);
MessageBox.Show(ds.Tables[1].TableName);
MessageBox.Show(ds.Tables[2].TableName);
MessageBox.Show(ds.Tables[3].TableName);
report.RegisterData(ds.Tables["Employees"], "Employees");
// enable it to use in a report
report.GetDataSource("Employees").Enabled = true;
// create A4 page with all margins set to 1cm
ReportPage page1 = new ReportPage();
page1.Name = "Page1";
report.Pages.Add(page1);
// create a ReportTitle band
page1.ReportTitle = new ReportTitleBand();
page1.ReportTitle.Name = "ReportTitle1";
// set its height to 1.5cm
page1.ReportTitle.Height = Units.Centimeters * 1.5f;
// create group header
GroupHeaderBand group1 = new GroupHeaderBand();
group1.Name = "GroupHeader1";
group1.Height = Units.Centimeters * 1;
// set group condition
group1.Condition = "[Employees.Surname].Substring(0, 1)";
// add group to the page.Bands collection
page1.Bands.Add(group1);
// create group footer
group1.GroupFooter = new GroupFooterBand();
group1.GroupFooter.Name = "GroupFooter1";
group1.GroupFooter.Height = Units.Centimeters * 1;
// create DataBand
DataBand data1 = new DataBand();
data1.Name = "Data1";
data1.Height = Units.Centimeters * 0.5f;
// set data source
data1.DataSource = report.GetDataSource("Employees");
// connect databand to a group
group1.Data = data1;
// create "Text" objects
// report title
TextObject text1 = new TextObject();
text1.Name = "Text1";
// set bounds
text1.Bounds = new RectangleF(0, 0,
Units.Centimeters * 19, Units.Centimeters * 1);
// set text
text1.Text = "Employees";
// set appearance
text1.HorzAlign = HorzAlign.Center;
text1.Font = new Font("Tahoma", 14);
// add it to ReportTitle
page1.ReportTitle.Objects.Add(text1);
// group
TextObject text2 = new TextObject();
text2.Name = "Text2";
text2.Bounds = new RectangleF(0, 0,
Units.Centimeters * 2, Units.Centimeters * 1);
text2.Text = "[[Employees.Name].Substring(0, 1)]";
text2.Font = new Font("Tahoma", 10);
// add it to the GroupHeader
group1.Objects.Add(text2);
// data band
TextObject text3 = new TextObject();
text3.Name = "Text3";
text3.Bounds = new RectangleF(0, 0,
Units.Centimeters * 10, Units.Centimeters * 0.5f);
text3.Text = "[Employees.Patronym]";
text3.Font = new Font("Tahoma", 8);
// add it to DataBand
data1.Objects.Add(text3);
report.Prepare();
PDFSimpleExport pdf = new PDFSimpleExport();
// Save the report
report.Export(pdf, "ExportedPDF.pdf");
}
}
}
至此
using FastReport;
using FastReport.Data;
using FastReport.Export.Image;
using FastReport.Export.PdfSimple;
using FastReport.Format;
using FastReport.Table;
using FastReport.Utils;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Windows;
namespace KursovayaAvtoparkAvtobusov
{
/// <summary>
/// Interaction logic for ReportExport.xaml
/// </summary>
public partial class ReportExport : Window
{
private static string outFolder = @"..\..\..\out\";
private static string inFolder = @"..\..\..\in\";
public ReportExport()
{
InitializeComponent();
}
/*private void Fucttardmethod()
{
RegisteredObjects.AddConnection(typeof(MsSqlDataConnection));
Report report = new Report();
MsSqlDataConnection sqlConnection = new MsSqlDataConnection();
sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["KursovayaAvtoparkAvtobusov"].ToString();
sqlConnection.CreateAllTables();
report.Report.Dictionary.Connections.Add(sqlConnection);
var select = "SELECT * FROM Employees; SELECT* FROM Maintenance; SELECT* FROM Marshuti; SELECT* FROM Prodazhi";
report.GetDataSource("Employees").Enabled = true;
report.GetDataSource("Maintenance").Enabled = true;
report.GetDataSource("Marshuti").Enabled = true;
report.GetDataSource("Prodazhi").Enabled = true;
report.Prepare();
PDFSimpleExport pdf = new PDFSimpleExport();
// Save the report
report.Export(pdf, "ExportedPDF.pdf");
}*/
private void SimpleRepExport_Click(object sender, RoutedEventArgs e)
{
RegisteredObjects.AddConnection(typeof(MsSqlDataConnection));
Report report = new Report();
MsSqlDataConnection connection = new MsSqlDataConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["KursovayaAvtoparkAvtobusov"].ToString();
connection.CreateAllTables();
report.Dictionary.Connections.Add(connection);
report.RegisterData(connection.DataSet, "mssql");
report.GetDataSource("Employees").Enabled = true;
ReportPage page = new ReportPage();
report.Pages.Add(page);
page.CreateUniqueName();
page.TopMargin = 10.0f;
page.LeftMargin = 10.0f;
page.RightMargin = 10.0f;
page.BottomMargin = 10.0f;
DataBand data = new DataBand();
data.CreateUniqueName();
page.AddChild(data);
data.DataSource = report.GetDataSource("Employees"); //adding a table to a bend
data.Height = Units.Centimeters * 5f;
// Creating report title
page.ReportTitle = new ReportTitleBand();
page.ReportTitle.CreateUniqueName();
page.ReportTitle.Height = 4.0f * Units.Centimeters;
TextObject titleText = new TextObject();
titleText.CreateUniqueName();
titleText.Left = 1.0f * Units.Centimeters;
titleText.Top = 1.0f * Units.Centimeters;
titleText.Width = 17.0f * Units.Centimeters;
titleText.Height = 2.0f * Units.Centimeters;
titleText.HorzAlign = HorzAlign.Center;
titleText.VertAlign = VertAlign.Center;
titleText.Font = new Font("Times New Roman", 32.0f);
titleText.TextColor = Color.Black;
titleText.FillColor = Color.Wheat;
titleText.Border.Color = Color.Black;
titleText.Border.Lines = BorderLines.All;
titleText.Border.Width = 4.0f;
titleText.Text = "Avtopark Report";
page.ReportTitle.Objects.Add(titleText);
// Header object
TextObject headerText = new TextObject();
headerText.CreateUniqueName();
headerText.Bounds = new RectangleF(0.0f, 2.7f * Units.Centimeters,
19.0f * Units.Centimeters, 1.0f * Units.Centimeters);
headerText.HorzAlign = HorzAlign.Center;
headerText.VertAlign = VertAlign.Center;
headerText.Font = new Font("Times New Roman", 16.0f);
headerText.TextColor = Color.Black;
headerText.FillColor = Color.Wheat;
headerText.Border.Color = Color.Black;
headerText.Border.Lines = BorderLines.All;
headerText.Border.Width = 4.0f;
headerText.Text = "Employees" + " Table";
page.ReportTitle.Objects.Add(headerText);
// Text bands
TextObject bandText = new TextObject();
bandText.CreateUniqueName();
bandText.HorzAlign = HorzAlign.Center;
bandText.Bounds = new RectangleF(0.0f * Units.Centimeters, 0.25f,
1.0f * Units.Centimeters, 0.5f * Units.Centimeters);
bandText.Border.Lines = BorderLines.All;
bandText.Text = "[Employees.Num]";
data.AddChild(bandText);
TextObject text1 = new TextObject();
text1.Parent = data;
text1.CreateUniqueName();
text1.Bounds = new RectangleF(1 * Units.Centimeters, 0, Units.Centimeters * 3, Units.Centimeters * 0.5f);
text1.Text = "[Employees.Surname]"; //table and its field
text1.Border.Lines = BorderLines.All;
TextObject text2 = new TextObject();
text2.Parent = data;
text2.CreateUniqueName();
text2.Bounds = new RectangleF(4 * Units.Centimeters, 0, Units.Centimeters * 20, Units.Centimeters * 0.5f);
text2.Text = "[Employees.Name]"; //table and its field
text2.Border.Lines = BorderLines.All;
TextObject text3 = new TextObject();
text3.Parent = data;
text3.CreateUniqueName();
text3.Bounds = new RectangleF(10 * Units.Centimeters, 0, Units.Centimeters * 15, Units.Centimeters * 0.5f);
text3.Text = "[Employees.Patronym]"; //table and its field
text3.Border.Lines = BorderLines.All;
TextObject text4 = new TextObject();
text4.Parent = data;
text4.CreateUniqueName();
text4.Bounds = new RectangleF(13 * Units.Centimeters, 0, Units.Centimeters * 5, Units.Centimeters * 0.5f);
text4.Text = "[Employees.Employed_Since]"; //table and its field
text4.Border.Lines = BorderLines.All;
TextObject text5 = new TextObject();
text5.Parent = data;
text5.CreateUniqueName();
text5.Bounds = new RectangleF(15 * Units.Centimeters, 0, Units.Centimeters * 5, Units.Centimeters * 0.5f);
text5.Text = "[Employees.Job]"; //table and its field
text5.Border.Lines = BorderLines.All;
TextObject text6 = new TextObject();
text6.Parent = data;
text6.CreateUniqueName();
text6.Bounds = new RectangleF(18 * Units.Centimeters, 0, Units.Centimeters * 5, Units.Centimeters * 0.5f);
text6.Text = "[Employees.Internship]"; //table and its field
text6.Border.Lines = BorderLines.All;
// Footer
page.PageFooter = new PageFooterBand();
page.PageFooter.CreateUniqueName();
page.PageFooter.Height = 0.5f * Units.Centimeters;
TextObject footerText = new TextObject();
footerText.CreateUniqueName();
footerText.HorzAlign = HorzAlign.Right;
footerText.VertAlign = VertAlign.Center;
footerText.Bounds = new RectangleF(0.0f, 0.0f,
19.0f * Units.Centimeters, 0.5f * Units.Centimeters);
footerText.TextColor = Color.Black;
footerText.FillColor = Color.Wheat;
titleText.Border.Color = Color.Black;
titleText.Border.Lines = BorderLines.All;
titleText.Border.Width = 4.0f;
footerText.Text = "Page [Page]";
page.PageFooter.Objects.Add(footerText);
// Exporting the report
report.Prepare();
PDFSimpleExport pdf = new PDFSimpleExport();
// Save the report
report.Export(pdf, "ExportedPDF.pdf");
}
}
}