当前位置:网站首页>c# 水晶报表打印
c# 水晶报表打印
2022-07-02 10:23:00 【coder i++】
转:https://www.cnblogs.com/aitong/p/10717786.html
水晶报表介绍
水晶报表是 vs 早期版本自带的一款报表控件。vs2017不自带水晶报表,需要自行安装。虽然水晶报表是收费控件,但是微软已经付过钱了,所以vs自带的水晶报表可以直接使用。
如果项目是 .net 4.0 以上版本,需要修改 App.config 文件。因为水晶报表的 .net 版本可能低于 .net 4.0
在软件发布的时候需要把对应的config文件一起发布,否则也无法使用水晶报表
<startup useLegacyV2RuntimeActivationPolicy="true"> //在这一行加入 useLegacyV2RuntimeActivationPolicy="true"
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
在解决方案资源管理器中添加数据集DataSet1.xsd,在数据集中添加 DataTable.
新建 Crystal Reports 文件
在解决方案资源管理器中新建 Crystal Reports 文件: CrystalReport1.rpt 。添加后左侧会出现 “字段资源管理器” 。如果不小心关掉字段资源管理器,可以在 “视图” “其它窗口” “文档大纲” 将其调出。

vs的工具箱中也会出现相应的控件。可以将文本对象添加到报表中。 文本对象常用属性:CanGrow (可以自动换行) HorAlignment (居中,靠左,靠右)。

在数据库字段右键选择数据库专家。添加项目数据中的 DataTable1

在 “组名字段” 右键插入组。选择 DataTable1 中的字段 ,排序方式选择按原始顺序(水晶报表的排序方式不实用,一般在设置 DataTable 时自行排序)。
数据库字段中的 Name Sex 可以拖到报表的详细资料中。报表的页眉会自动添加文本框。文本框的内容可修改为姓名,性别。

在界面中添加显示控件:CrystalReportViewer 重命名为crv
在窗口中创建报表对象并添加数据,显示到 CrystalReportViewer 上面。可以直接打印,也可以通过控件上的打印按钮打印。
using CrystalDecisions.CrystalReports.Engine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CrTest
{
public partial class Form1 : Form
{
public Form1(bool directPrint = false)
{
InitializeComponent();
DataSet1 ds = new DataSet1();
ds.DataTable1.Rows.Add("组1", "张1", "男");
ds.DataTable1.Rows.Add("组3", "王1", "男");
ds.DataTable1.Rows.Add("组2", "李1", "男");
ds.DataTable1.Rows.Add("组1", "张2", "男");
ds.DataTable1.Rows.Add("组1", "张3", "女");
ds.DataTable1.Rows.Add("组1", "张4", "男");
ds.DataTable1.Rows.Add("组2", "李2", "男");
ds.DataTable1.Rows.Add("组2", "李3", "女");
ds.DataTable1.Rows.Add("组2", "李4", "男");
ds.DataTable1.Rows.Add("组3", "王2", "男");
ReportClass cr = new CrystalReport1();
cr.SetDataSource(ds);
TextObject title = (TextObject)cr.ReportDefinition.ReportObjects["Text1"];
title.Text = "人员表格";
if (directPrint)
{
PrintDialog print = new PrintDialog();
if (print.ShowDialog() == DialogResult.OK)
{
cr.PrintOptions.PrinterName = print.PrinterSettings.PrinterName;
cr.PrintToPrinter(1, false, 0, 0);
}
}
this.crv.ReportSource = cr;
}
}
}
边栏推荐
- When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
- Origin绘制热重TG和微分热重DTG曲线
- P1347 排序(拓扑 + spfa判断环 or 拓扑[内判断环])
- Téléchargement par navigateur
- (POJ - 1308)Is It A Tree? (tree)
- 验证失败,请检查您的回电网址。您可以按照指导进行操作
- P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
- [Unity]使用GB2312,打包后程序不正常解决方案
- The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
- Why is the default of switch followed by break?
猜你喜欢

题解:《压缩技术》(原版、续集版)

OpenFOAM:lduMatrix&lduAddressing

Bridge of undirected graph

Qt-制作一个简单的计算器-实现四则运算

Integral link, inertia link and proportion link in Simulink

How to explain binary search to my sister? This is really difficult, fan!

QT new project_ MyNotepad++

Solution: Compression Technology (original version and sequel version)

Subcontracting configuration of uniapp applet subpackages

Performance optimization of memory function
随机推荐
如何设置Qt手工布局
Android kotlin broadcast technology point
Pattern matching and regular expressions in PostgreSQL - Das
693. 行程排序(map + 拓扑)
Drawing Nyquist diagram with MATLAB
Find love for speed in F1 delta time Grand Prix
基于ssm+jsp框架实现的学生选课信息管理系统【源码+数据库】
Sum of the first n terms of Fibonacci (fast power of matrix)
[Unity]使用GB2312,打包后程序不正常解决方案
BeanUtils -- shallow copy -- example / principle
题解:《压缩技术》(原版、续集版)
[unity] using GB2312, the solution to abnormal program after packaging
Simple introduction to ENSP
[cloud native database] what to do when encountering slow SQL (Part 1)?
Astro learning notes
[document tree, setting] font becomes smaller
Node.js通过ODBC访问PostgreSQL数据库
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
(POJ - 1984) navigation nightare (weighted and search set)
Bridge of undirected graph