在C#winfrom中,我用了一个水晶报表控件显示报表文件(即.rpt文件),然后导出保存到excel中(其实最终要实现的功能是提取报表里面的某些文本保存到excel指定的位置中),但我目前就想先试试直接把整个报表导出来,然后保存到excel中可不可以实现。现在我保存到excel中成功是成功了,但是打开excel文件全是乱码,这是为什么呢?有什么解决办法吗?求高手相助 谢谢啦!我的代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Web.UI;
using System.Web; namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
//打开报表文件(.rpt)
private void button1_Click(object sender, System.EventArgs e)
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "打开水晶报表文件";
dlg.Filter = "水晶报表文件(*.rpt)|*.rpt|所有文件|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
this.txtPath.Text = dlg.FileName;
crystalReportViewer1.ReportSource = dlg.FileName;//加载水晶报表,将资源报表绑定到水晶报表查看器
}
}
catch(Exception error)
{
MessageBox.Show(error.ToString(),"错误"); //处理异常错误
}
}
//导出并保存到excel文件中
private void button2_Click(object sender, EventArgs e)
{
CrystalReport1 cr = new CrystalReport1();
DiskFileDestinationOptions dOpt = new DiskFileDestinationOptions();
cr.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; ;
cr.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
// dOpt.DiskFileName = "E:\\output.xls";
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "表格 (*.xls)|*.xls"; sfd.FilterIndex = 2;
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
dOpt.DiskFileName = sfd.FileName;
cr.ExportOptions.DestinationOptions = dOpt;
cr.Export();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Web.UI;
using System.Web; namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
//打开报表文件(.rpt)
private void button1_Click(object sender, System.EventArgs e)
{
try
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "打开水晶报表文件";
dlg.Filter = "水晶报表文件(*.rpt)|*.rpt|所有文件|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
this.txtPath.Text = dlg.FileName;
crystalReportViewer1.ReportSource = dlg.FileName;//加载水晶报表,将资源报表绑定到水晶报表查看器
}
}
catch(Exception error)
{
MessageBox.Show(error.ToString(),"错误"); //处理异常错误
}
}
//导出并保存到excel文件中
private void button2_Click(object sender, EventArgs e)
{
CrystalReport1 cr = new CrystalReport1();
DiskFileDestinationOptions dOpt = new DiskFileDestinationOptions();
cr.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile; ;
cr.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
// dOpt.DiskFileName = "E:\\output.xls";
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "表格 (*.xls)|*.xls"; sfd.FilterIndex = 2;
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
dOpt.DiskFileName = sfd.FileName;
cr.ExportOptions.DestinationOptions = dOpt;
cr.Export();
}
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货