duheng
2025-02-19 ad8f813f5eddd66740b4e09801e4ea02ddf70a4a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
namespace HStation.WinFrmUI
{
    public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
    {
        public XtraForm1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
        }
 
        private XtraReport1 _report1;
 
        public void SetBindingData(SimulationPrintViewModel viewModel)
        {
            _report1 = new XtraReport1();
            _report1.SetBingdingData(viewModel);
 
            // 生成报表文档
            _report1.CreateDocument();
            // 将报表文档加载到 DocumentViewer 中
            documentViewer1.DocumentSource = _report1;
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            /*      XtraReport1 report = new XtraReport1();
                  // 生成报表文档
                  report.CreateDocument();
 
                  // 将报表文档加载到 DocumentViewer 中
                  documentViewer1.DocumentSource = report;*/
        }
 
        private void btnExportPDF_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            // 设置文件筛选器,只允许选择特定格式的文件
            saveFileDialog.Filter = "PDF Files (*.pdf)|*.pdf|";
            saveFileDialog.Title = "选择保存路径";
 
            // 显示对话框并获取用户选择的结果
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
 
                try
                {
                    _report1.ExportToPdf(filePath);
                    TipFormHelper.ShowSucceed("导出成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"导出报表时发生错误: {ex.Message}", "导出失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
 
        private void btnExportWord_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            // 设置文件筛选器,只允许选择特定格式的文件
            saveFileDialog.Filter = "Word Files (*.docx)|*.docx";
            saveFileDialog.Title = "选择保存路径";
 
            // 显示对话框并获取用户选择的结果
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = saveFileDialog.FileName;
 
                try
                {
                    _report1.ExportToDocx(filePath);
                    TipFormHelper.ShowSucceed("导出成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"导出报表时发生错误: {ex.Message}", "导出失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}