VS提供了功能强大的报表工具栏,但有时候需要重新自定义工具栏,可以重新封转一个报表的操作类,重新定义报表的操作函数。
(1)报表导出
public static bool ReportExportTo(ReportViewer reportView, HttpResponse Response, stringfileType)
{
Microsoft.Reporting.WebForms.Warning[] Warnings;
string[] strStreamIds;
stringstrMimeType;
stringstrEncoding;
stringstrFileNameExtension;
stringfileSuffix = ".doc";
if(fileType.ToLower().Equals("word"))
{
fileSuffix = ".doc";
}
elseif(fileType.ToLower().Equals("pdf"))
{
fileSuffix = ".pdf";
}
elseif(fileType.ToLower().Equals("excel"))
{
fileSuffix = ".xls";
}
byte[] bytes = reportView.LocalReport.Render(fileType,null,outstrMimeType,
{
Microsoft.Reporting.WebForms.Warning[] Warnings;
string[] strStreamIds;
stringstrMimeType;
stringstrEncoding;
stringstrFileNameExtension;
stringfileSuffix = ".doc";
if(fileType.ToLower().Equals("word"))
{
fileSuffix = ".doc";
}
elseif(fileType.ToLower().Equals("pdf"))
{
fileSuffix = ".pdf";
}
elseif(fileType.ToLower().Equals("excel"))
{
fileSuffix = ".xls";
}
byte[] bytes = reportView.LocalReport.Render(fileType,null,outstrMimeType,
outstrEncoding,outstrFileNameExtension,outstrStreamIds,outWarnings);
try
{
Response.Buffer = true;
Response.AddHeader("Connection","Keep-Alive"); //添加文件标头
Response.ContentType = "application/ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename="+ Utilities.GetTimeStamp() + fileSuffix);//下载时要保存的默认文件名
Response.BinaryWrite(bytes);
returntrue;
}
catch
{
returnfalse;
}
finally
{
Response.End();
Response.Close();
}
}
try
{
Response.Buffer = true;
Response.AddHeader("Connection","Keep-Alive"); //添加文件标头
Response.ContentType = "application/ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename="+ Utilities.GetTimeStamp() + fileSuffix);//下载时要保存的默认文件名
Response.BinaryWrite(bytes);
returntrue;
}
catch
{
returnfalse;
}
finally
{
Response.End();
Response.Close();
}
}
(2)报表导出,报表直接打印功能涉及到DeviceInfo类参数的设置,在尝试失败后,采用的是先将报表导出到word文档,然后打印word文档,有一些影响性能,如果遇到更好的方法,以后再补充。
public static bool ReportExportTo(ReportViewer reportView, HttpResponse Response, string fileType)
{ Microsoft.Reporting.WebForms.Warning[] Warnings; string[] strStreamIds; string strMimeType; string strEncoding; string strFileNameExtension; string fileSuffix = ".doc"; if (fileType.ToLower().Equals("word")) { fileSuffix = ".doc"; } else if (fileType.ToLower().Equals("pdf")) { fileSuffix = ".pdf"; } else if (fileType.ToLower().Equals("excel")) { fileSuffix = ".xls"; } byte[] bytes = reportView.LocalReport.Render(fileType, null, out strMimeType, out strEncoding, out strFileNameExtension, out strStreamIds, out Warnings); try { Response.Buffer = true; Response.AddHeader("Connection", "Keep-Alive"); //添加文件标头 Response.ContentType = "application/ms-excel"; Response.AddHeader("Content-Disposition", "attachment;filename=" + Utilities.GetTimeStamp() + fileSuffix);//下载时要保存的默认文件名 Response.BinaryWrite(bytes); return true; } catch { return false; } finally { Response.End(); Response.Close(); } } |