📄 accessadapter.cs
字号:
using System;
using System.IO;
using System.Configuration;
using AWC.Reporter.Web;
using AWC.Reporter.Web.Entities;
using MsAccess = Microsoft.Office.Interop.Access;
namespace AWC.Reporter.Web.EnterpriseReporting.Adapters
{
/// <summary>
/// Summary description for AccessAdapter.
/// </summary>
public class AccessAdapter: IReportAdapter
{
/// <summary>
/// Render Access report
/// </summary>
/// <param name="reportRequest">An Instance of the ReportRequest entity</param>
/// <returns>The report payload</returns>
public byte[] RenderReport(ReportRequest reportRequest)
{
MsAccess.Application oAccess = null;
byte[] reportPayload = null;
// get to the report request row
AWC.Reporter.Web.Entities.ReportRequest.ReportRow rptRst = reportRequest.Report[0];
// get a temp file to render the
string fileName = Path.GetTempFileName();
try
{
// start a new instance of Access
oAccess = new MsAccess.Application();
// open the database
oAccess.OpenCurrentDatabase(ConfigurationSettings.AppSettings[Constants.CONFIG_ACCESS_RPT_DB], false, null);
// render the report
oAccess.DoCmd.OpenReport(rptRst.Name, MsAccess.AcView.acViewPreview, null, null, MsAccess.AcWindowMode.acWindowNormal, null);
// save the report as a file
oAccess.DoCmd.OutputTo(MsAccess.AcOutputObjectType.acOutputReport, String.Empty, GetAccessReportFormat(rptRst.ExportFormat), fileName, null, null, null);
// convert the report file to a byte array
reportPayload = ConvertPayload (fileName);
}
catch (System.Security.SecurityException ex)
{
throw (new ApplicationException(String.Format("The ASP.NET process identity doen't have sufficient permissions to open {0}", fileName), ex));
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
// clean up
if (oAccess!=null)
{
oAccess.Quit(MsAccess.AcQuitOption.acQuitSaveNone);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oAccess);
oAccess = null;
}
File.Delete(fileName);
}
return reportPayload;
}
/// <summary>
/// A helper class to get the Access Report Format specifier
/// </summary>
/// <param name="format">The report format as per the RS specification, e.g. "html4.0"</param>
/// <returns></returns>
private object GetAccessReportFormat(string format)
{
object accessFormat = null;
switch (format.ToLower())
{
case "html4.0" : accessFormat = MsAccess.Constants.acFormatHTML; break;
case "html3.2" : accessFormat = MsAccess.Constants.acFormatHTML; break;
case "mhtml" : accessFormat = MsAccess.Constants.acFormatHTML; break;
case "htmlowc" : accessFormat = MsAccess.Constants.acFormatHTML; break;
case "excel" : accessFormat = MsAccess.Constants.acFormatXLS; break;
default : accessFormat = MsAccess.Constants.acFormatSNP ; break;
}
return accessFormat;
}
/// <summary>
/// Convert the access report to a byte array
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private byte[] ConvertPayload (string fileName)
{
FileStream stream = null;
FileInfo reportFile = new FileInfo(fileName);
long fileSize = reportFile.Length;
// load the file into a binary arry
byte[] binaryData = new byte[fileSize];
try
{
stream = new FileStream(fileName, FileMode.Open);
stream.Read(binaryData, 0, (int) fileSize);
}
finally
{
stream.Close();
}
return binaryData;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -