4,819
社区成员




//载入报表参数
private void ReportInput()
{
DataTable dt = new DataTable();
if ((CheckShop.Checked == true) && (DDLShop.SelectedValue != ""))
{
string shop = DDLShop.SelectedItem.Value.ToString().Trim();
string text = DDLCX.SelectedItem.Value.ToString().Trim();
string value = string.Empty;
if (text == "null")
{
value = null;
}
else
{
value = DDLCon.SelectedItem.Value.ToString().Replace(" 0:00:00", "");
}
dt = service.yyhz(jbtj, "tbl_jiebantongji", "shop_no", shop, text, value);
}
else
{
dt = service.yyhz(jbtj, "tbl_jiebantongji");
}
string reportPath = Server.MapPath("~/Reports/jiebantongji.rpt");
report.Load(reportPath);
report.SetDatabaseLogon("sa", "");
report.SetDataSource(dt);
}
/// <summary>
/// 报表导出功能实现
/// </summary>
protected void btnSave_Click(object sender, EventArgs e)
{
ReportInput();
//导出类型
string contenttype = string.Empty;
//导出的文件类型
string fileType = DDLSaveType.SelectedValue;
//导出的文件路径
string ExportPath;
//导出的文件名称
string FileName;
ExportPath = Request.PhysicalApplicationPath + "报表/" + ReportName.AddDate() + "/";
if (!Directory.Exists(ExportPath))
{
System.IO.Directory.CreateDirectory(Request.PhysicalApplicationPath + "报表/" + ReportName.AddDate() + "/");
}
FileName = "结班统计报表" + ReportName.AddDate();
//FileName = Request.MapPath(".")+"\\报表\\" + ReportName.AddDate() + "\\" + FileName + "." + fileType;
DiskFileDestinationOptions diskFile = new DiskFileDestinationOptions();
//diskFile.DiskFileName = FileName;
//导出为磁盘文件
ExportOptions options = report.ExportOptions;
options.ExportDestinationOptions = diskFile;
options.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
switch (DDLSaveType.SelectedItem.Value)
{
case "pdf":
contenttype = "application/pdf";
options.ExportFormatType = ExportFormatType.PortableDocFormat;
break;
case "doc":
Response.ContentType = "application/ms-word";
options.ExportFormatType = ExportFormatType.WordForWindows;
break;
case "xls":
contenttype = "application/ms-excel";
options.ExportFormatType = ExportFormatType.Excel;
break;
default:
contenttype = "application/ms-excel";
options.ExportFormatType = ExportFormatType.Excel;
break;
}
FileName = FileName + "." + DDLSaveType.SelectedItem.Value;
diskFile.DiskFileName = ExportPath + FileName;
//导出操作
report.Export();
if (MessageBox.Show(null, "数据已保存到服务器,是否下载到本地?", "信息提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,MessageBoxOptions.ServiceNotification) == DialogResult.Yes)
{
Response.Redirect("DownloadFile.aspx?FileName=" + diskFile.DiskFileName);
}
ReportLoad();
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), null, "alert('报表已保存到服务器!')", true);
}
protected void Page_Load(object sender, EventArgs e)
{
string fileroot=Request.QueryString["FileName"].ToString();
string type = getFileType(fileroot);
HttpResponse ht = Page.Response;
UserCom.DownloadFile(ht,fileroot,type);
}
public static string getFileType(string fileroot)
{
string newstr = string.Empty;
int det = 0;
char[] rootchar = fileroot.ToCharArray();
for (int i = 0; i < rootchar.Length; i++)
{
if (rootchar[i].ToString() == ".")
{
det = i;
}
}
for (int j = det; j < rootchar.Length; j++)
{
newstr += rootchar[j].ToString();
}
return newstr;
}
#region 报表文件下载
/// <summary>
/// 报表下载到客户端
/// </summary>
/// <param name="response"></param>
/// <param name="serverPath"></param>
/// <param name="type"></param>
public static void DownloadFile(HttpResponse response,string serverPath,string type)
{
FileStream fs = null;
try
{
fs = File.OpenRead(serverPath);
byte[] buffer = new byte[1024];
long count = 1024;
response.Buffer = true;
response.Charset = "GB2312";
response.ContentEncoding = System.Text.Encoding.UTF7;
response.AppendHeader("Content-Disposition", "attachment;filename =" + HttpUtility.UrlEncode(serverPath, Encoding.UTF8).ToString());
response.ContentType = "";
switch (type)
{
case ".pdf":
response.ContentType = "application/pdf";
break;
case ".doc":
response.ContentType = "application/ms-word";
break;
case ".xls":
response.ContentType = "application/ms-excel";
break;
default:
response.ContentType = "application/all";
break;
}
while (count == 1024)
{
count = fs.Read(buffer, 0, 1024);
response.BinaryWrite(buffer);
}
}
catch {
}
finally
{
fs.Close();
}
}
#endregion