Last active
February 26, 2016 11:00
-
-
Save kwestground/9f4c8cdd51cd46a4cb7b to your computer and use it in GitHub Desktop.
Print Crystal Report from Embeded Resource with Customer Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using CrystalDecisions.CrystalReports.Engine; | |
using CrystalDecisions.Shared; | |
using ITCO.SboAddon.Framework; | |
namespace MyAddon | |
{ | |
public class PrintCrystalReportFromEmbededResource | |
{ | |
private static void PrintResult(IEnumerable<int> docNums) | |
{ | |
try | |
{ | |
var report = new ReportDocument(); | |
var reportFile = Path.GetTempFileName(); | |
ExtractEmbeddedResource("Reports.MyReport.rpt", reportFile); | |
report.Load(reportFile); | |
var defaultConnection = report.DataSourceConnections[0]; | |
var attributes = defaultConnection.Attributes; | |
var logonProperties = defaultConnection.LogonProperties; | |
logonProperties.Set("Initial Catalog", SboApp.Company.CompanyDB); | |
logonProperties.Set("Data Source", SboApp.Company.Server); | |
logonProperties.Set("Integrated Security", SboApp.Company.UseTrusted); | |
attributes.Collection.Set(DbConnectionAttributes.QE_DATABASE_NAME, SboApp.Company.CompanyDB); | |
attributes.Collection.Set(DbConnectionAttributes.QE_SERVER_DESCRIPTION, SboApp.Company.Server); | |
attributes.Collection.Set(DbConnectionAttributes.CONNINFO_SSO_ENABLED, SboApp.Company.UseTrusted); | |
var connectionInfo = new ConnectionInfo | |
{ | |
ServerName = SboApp.Company.Server, | |
DatabaseName = SboApp.Company.CompanyDB, | |
IntegratedSecurity = true, | |
Type = ConnectionInfoType.CRQE, | |
Attributes = attributes, | |
LogonProperties = logonProperties | |
}; | |
var docNumsSql = string.Join(",", docNums.Select(docNum => $"'{docNum}'")); | |
report.SetSQLCommandTable(connectionInfo, "OINV", $"SELECT * FROM OINV WHERE DocNum IN ({docNumsSql})"); | |
report.PrintToPrinter(0, true, 1, 1); | |
} | |
catch (Exception e) | |
{ | |
SboApp.Application.MessageBox($"Fel vid utskrift: {e.Message}"); | |
} | |
} | |
private static void ExtractEmbeddedResource(string resource, string file) | |
{ | |
using ( | |
var stream = System.Reflection.Assembly.GetExecutingAssembly() | |
.GetManifestResourceStream(resource)) | |
{ | |
using (var fileStream = new FileStream(file, FileMode.Create)) | |
{ | |
for (int i = 0; i < stream.Length; i++) | |
{ | |
fileStream.WriteByte((byte)stream.ReadByte()); | |
} | |
fileStream.Close(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment