当前位置:网站首页>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;
}
}
}
边栏推荐
- [unity] using GB2312, the solution to abnormal program after packaging
- Memory management 01 - link script
- Redis database persistence
- Daily question: 1175 Prime permutation
- How to explain binary search to my sister? This is really difficult, fan!
- MySQL -- convert rownum in Oracle to MySQL
- Daily practice of C language --- monkeys divide peaches
- Selenium installing selenium in pycharm
- Three talking about exception -- error handling
- Astro learning notes
猜你喜欢

QT new project_ MyNotepad++

Bridge of undirected graph
![Unity small map production [2]](/img/d6/9d6556d37525b9986b74133f2a7aaa.jpg)
Unity small map production [2]
![[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术](/img/a7/44609a5acf25021f1fca566c3d8c90.png)
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术

qt中uic的使用

Essential for operation and maintenance - Elk log analysis system

Find love for speed in F1 delta time Grand Prix

Node.js通过ODBC访问PostgreSQL数据库

Answer: can the audio be set to on by default during easydss video on demand?

Simple introduction to ENSP
随机推荐
[technology development-22]: rapid overview of the application and development of network and communication technology-2-communication Technology
ArrayList and LinkedList
故事點 vs. 人天
De4000h storage installation configuration
混沌工程平台 ChaosBlade-Box 新版重磅发布
故事点 vs. 人天
[USACO05JAN]Watchcow S(欧拉回路)
Runhe hi3516 development board openharmony small system and standard system burning
Qt新项目_MyNotepad++
Solve "sub number integer", "jump happily", "turn on the light"
石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
Achievements in science and Technology (27)
Essential for operation and maintenance - Elk log analysis system
[unity] using GB2312, the solution to abnormal program after packaging
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
2022 Heilongjiang provincial examination on the writing skills of Application Essays
不会看器件手册的工程师不是个好厨子
[document tree, setting] font becomes smaller
【模板】最长公共子序列 (【DP or 贪心】板子)
(POJ - 1308)Is It A Tree? (tree)