Stimulsoft.Report.net报表简单实用

2019-09-11 15:28:30 浏览数 (1)

代码语言:javascript复制

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.UI;
  6 using System.Web.UI.WebControls;
  7 using System.Data;
  8 using Stimulsoft.Report;
  9 using Stimulsoft.Report.Components;
 10 using Stimulsoft.Report.Print;
 11 using Stimulsoft.Report.Export;
 12 using System.IO;
 13 using System.Net.Mail;
 14 using System.Text;
 15 using System.Net;
 16 
 17 public class applicationStart
 18 {
 19     public static bool isshowlogo = false;
 20     public static string orderid = "";
 21     public static DataTable dthead;
 22     public static DataTable dtmessage;
 23 }
 24 
 25 public partial class StimulReport : System.Web.UI.Page
 26 {
 27     double left = 0, right = 0, top = 0, bottom = 20;
 28 
 29     protected void Page_Load(object sender, EventArgs e)
 30     {
 31         if (!Page.IsPostBack)
 32         {
 33             try
 34             {
 35                 string aa = Request.QueryString["printno"];
 36                 if (aa == null || aa == "")
 37                     throw new Exception();
 38                 applicationStart.orderid = aa;
 39             }
 40             catch
 41             {
 42                 return;
 43             }
 44             try
 45             {
 46                 applicationStart.isshowlogo = Convert.ToBoolean(Request.QueryString["isshowlogo"]);
 47 
 48             }
 49             catch
 50             {
 51             }
 52             GetSource();
 53         }
 54         ShowReport(applicationStart.isshowlogo);
 55     }
 56 
 57     private void ShowReport(bool isshowlogo)
 58     {
 59         if (applicationStart.isshowlogo)
 60         {
 61             this.Button2.Text = "HideLogo";
 62         }
 63         else
 64         {
 65             this.Button2.Text = "ShowLogo";
 66         }
 67 
 68         string filepath = Server.MapPath("~/Report_1.mrt");
 69         StiReport stireport = new StiReport();
 70         stireport.Load(filepath);
 71 
 72         stireport.RegData("datasource", applicationStart.dtmessage);
 73         //设置报表内控件状态
 74         StiFooterBand fb = stireport.Pages[0].Components["FooterBand1"] as StiFooterBand;
 75         StiText st = fb.Components["Text47"] as StiText;
 76         st.Enabled = applicationStart.dthead.Rows[0]["looksampletype"].ToString() == "0" ? false : true;
 77 
 78         StiPageHeaderBand head = stireport.Pages[0].Components["PageHeaderBand1"] as StiPageHeaderBand;
 79         StiImage imglogo1 = head.Components["Image1"] as StiImage;
 80         imglogo1.Enabled = applicationStart.isshowlogo;
 81 
 82         StiPageFooterBand footer = stireport.Pages[0].Components["PageFooterBand1"] as StiPageFooterBand;
 83         StiImage imglogo2 = footer.Components["Image2"] as StiImage;
 84         imglogo2.Enabled = applicationStart.isshowlogo;
 85 
 86         //设置页边距
 87         stireport.Pages[0].Margins = new StiMargins(left, right, top, bottom);
 88 
 89         stireport.Compile();
 90         //设置报表内的参数值
 91         stireport["code"] = applicationStart.dthead.Rows[0]["code"].ToString();
 92         stireport["orderno"] = applicationStart.dthead.Rows[0]["orderno"].ToString();
 93         stireport["price"] = applicationStart.dthead.Rows[0]["price"].ToString();
 94         stireport["discountprice"] = applicationStart.dthead.Rows[0]["discountprice"].ToString();
 95         stireport["tobefinishstr"] = applicationStart.dthead.Rows[0]["tobefinishstr"].ToString();
 96         stireport["createdate"] = applicationStart.dthead.Rows[0]["createdate"].ToString();
 97         stireport["contact"] = applicationStart.dthead.Rows[0]["contact"].ToString();
 98         stireport["companyname"] = applicationStart.dthead.Rows[0]["companyname"].ToString();
 99         stireport["tel"] = applicationStart.dthead.Rows[0]["tel"].ToString();
100         stireport["fax"] = applicationStart.dthead.Rows[0]["fax"].ToString();
101         stireport["mobile"] = applicationStart.dthead.Rows[0]["mobile"].ToString();
102         stireport["address"] = applicationStart.dthead.Rows[0]["address"].ToString();
103         stireport["email"] = applicationStart.dthead.Rows[0]["email"].ToString();
104         stireport["deliverytypeenname"] = applicationStart.dthead.Rows[0]["deliverytypeenname"].ToString();
105         stireport["name"] = applicationStart.dthead.Rows[0]["name"].ToString();
106         stireport["customermemo"] = applicationStart.dthead.Rows[0]["customermemo"].ToString();
107         stireport["looksampletypestr"] = applicationStart.dthead.Rows[0]["looksampletypestr"].ToString();
108 
109         viewer1.Report = stireport;
110     }
111 
112     private void GetSource()
113     {
114         string headsql = "select  orderno, tobefinishdate, so.price,so.discountprice, tobefinishstr,so.deliverydate,createdate ,so.contact,so.companyname,so.tel,so.fax,so.mobile,so.address,so.looksampletype, "  
115                   "so.orderid, so.email,so.code, sales.username as salename ,openso.username as openname ,base_delivery_type.deliverytypeenname,base_delivery_type.shiptypename,user_position.positionname,"   "name,so.discountoffflag,so.customermemo from so ,user as sales,user as openso ,base_delivery_type,user_position ,customer,customer_discount "  
116                   "where  so.userid=sales.userid and so.opuserid=openso.userid and base_delivery_type.deliverytypeid=so.deliverytypeid "  
117                   "and so.positionid=user_position.positionid and customer.discountid=customer_discount.discountid "  
118                   "and so.customerid=customer.customerid  and so.orderid="   applicationStart.orderid;
119 
120         string messagesql = "SELECT *from ("  
121             "select jobno,product.productname,enproductname,so_detail.printnum,sampleflag,socontent as jobcontent  , printprice , "  
122             "sodetailid as sortno,so.customermemo as memo "  
123             "From so_detail ,so,product where so_detail.deleteflag=0 and so.orderid=so_detail.orderid and "  
124             "product.productid=so_detail.productid and so.orderid= "   applicationStart.orderid  
125             " union all "  
126             "select '' as jobno , 'Delivery' as productname  ,'Delivery' as enproductname , ' ' as printnum ,' ' as  sampleflag , '' as jobcontent ,price ,"  
127             "'1'  as sortno ,address as memo from  so_delivery where orderid= "   applicationStart.orderid  
128             " union all "  
129             "select '' as jobno , 'PostProcess' as productname  ,'PostProcess' as enproductname, outsidenum as printnum ,' ' as  sampleflag , postprocessname, costamount "  
130             ", so_detail.sodetailid as sortno ,concat(memo,memostr) as memo  From so_detail_outside,so ,so_detail where so.orderid=so_detail.orderid and so_detail.deleteflag=0 "  
131             "and so_detail.sodetailid=so_detail_outside.sodetailid and so.orderid= "   applicationStart.orderid  
132             " union  all "  
133             "select '' as jobno , 'OtherItem' as productname  ,'OtherItem' as enproductname , ' ' as printnum ,' ' as  sampleflag , pricenamecn, so_detail_price.price  "  
134             ", so_detail.sodetailid as sortno ,'' as memo  From so_detail_price,so ,so_detail where so.orderid=so_detail.orderid and so_detail.deleteflag=0  "  
135             "and so_detail.sodetailid=so_detail_price.sodetailid and so.orderid= "   applicationStart.orderid  
136             " union all "  
137             "select '' as jobno , offername as productname ,"  
138             "'Discountinfo' as enproductname , ' ' as printnum ,' ' as sampleflag , offer_so_history.offermemo as discountmemo, -offer_so_history.discountprice as price , "  
139             "'0' as sortno ,  offer_so_history.offername as memo From offer_so_history,so where so.orderid=offer_so_history.orderid  and so.deleteflag=0 "  
140             "and so.orderid="   applicationStart.orderid   " ) as itemlist order by sortno desc";
141         DataModule dm = DataModule.GetDataModule();
142         applicationStart.dthead = dm.QuerySql(headsql);
143         applicationStart.dtmessage = dm.QuerySql(messagesql);
144         DataTable dt = applicationStart.dtmessage;
145 
146         applicationStart.dthead.Columns.Add(new DataColumn("looksampletypestr", typeof(string)));
147         applicationStart.dthead.Columns.Add(new DataColumn("isshowdiscountprice", typeof(string)));
148         if (applicationStart.dthead.Rows.Count > 0)
149         {
150             if (applicationStart.dthead.Rows[0][2].ToString() == applicationStart.dthead.Rows[0][3].ToString())
151             {
152                 applicationStart.dthead.Rows[0]["isshowdiscountprice"] = 0;//false
153             }
154             else
155             {
156                 applicationStart.dthead.Rows[0]["isshowdiscountprice"] = 1;//true
157             }
158         }
159 
160         foreach (DataRow dr in applicationStart.dthead.Rows)
161         {
162             if (dr["looksampletype"].ToString() == "0")
163             {
164                 dr["looksampletypestr"] = "不打稿";
165             }
166             else if (dr["looksampletype"].ToString() == "1")
167             {
168                 dr["looksampletypestr"] = "打稿";
169             }
170             else
171             {
172                 dr["looksampletypestr"] = "";
173             }
174         }
175     }
176 
177     protected void Button1_Click(object sender, EventArgs e)
178     {
179         SendEmail(this.DropDownList1.SelectedValue, this.viewer1.Report, "aaa@163.com", this.TextBox1.Text, "Report["   this.DropDownList1.SelectedValue   "]"   DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "", "smtp.163.com", 25, "aaa@163.com", "123456");
180     }
181 
182     private void GetFormat(string filetype, ref StiExportFormat sf, ref string filename)
183     {
184         switch (filetype)
185         {
186             case "pdf": sf = StiExportFormat.Pdf; filename = "report.pdf"; break;
187             case "jpeg": sf = StiExportFormat.ImageJpeg; filename = "report.jpeg"; break;
188             case "gif": sf = StiExportFormat.ImageGif; filename = "report.gif"; break;
189             case "excel": sf = StiExportFormat.Excel; filename = "report.xls"; break;
190             case "word2007": sf = StiExportFormat.Word2007; filename = "report.docx"; break;
191         }
192     }
193 
194     public void SendEmail(string filetype, StiReport report, string fromaddress, string toaddress, string subject, string messageBody, string host, int port, string username, string password)
195     {
196         using (MemoryStream ms = new MemoryStream())
197         {
198             string filename = "";
199             StiExportFormat sef = StiExportFormat.Pdf;
200             GetFormat(filetype, ref sef, ref filename);
201             report.ExportDocument(sef, ms);
202             ms.Seek(0, SeekOrigin.Begin);
203 
204             //使用自己的邮件发送机制
205             System.Net.Mail.Attachment attachment = new System.Net.Mail.Attachment(ms, filename);
206 
207             MailMessage item = new MailMessage(fromaddress, toaddress);
208             item.Subject = subject;
209             item.Body = messageBody;
210             item.Attachments.Add(attachment);
211 
212             SmtpClient smtp = new SmtpClient(host, port);
213             smtp.UseDefaultCredentials = false;
214             smtp.Credentials = new System.Net.NetworkCredential(username, password);
215             smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
216             smtp.Send(item);
217             ms.Close();
218         }
219         ClientScript.RegisterStartupScript(GetType(), "", "<script>showmessage();</script>");
220     }
221     protected void Button2_Click(object sender, EventArgs e)
222     {
223         applicationStart.isshowlogo = !applicationStart.isshowlogo;
224         ShowReport(applicationStart.isshowlogo);
225     }
226 }
代码语言:javascript复制
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StimulReport.aspx.cs" Inherits="StimulReport" %>

<%@ Register Assembly="Stimulsoft.Report.Web, Version=2013.3.1800.0, Culture=neutral, PublicKeyToken=ebe6666cba19647a"
    Namespace="Stimulsoft.Report.Web" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 498px;
        }
    </style>
    <script language="javascript" type="text/javascript">
        function showmessage() {
            document.getElementById("divmessage").style.display = "block";
            setTimeout("document.getElementById('divmessage').style.display ='none';", 2000);
        }       
    </script>
</head> 
<body>
    <form id="form1" runat="server">
    <table style="width: 100%">
        <tr>
            <td class="style1">
                <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
                    Text="ShowLogo" />
                Email:<asp:TextBox ID="TextBox1" runat="server">huang_198906@163.com</asp:TextBox>
                <asp:DropDownList ID="DropDownList1" runat="server">
                    <asp:ListItem Selected="True">pdf</asp:ListItem>
                    <asp:ListItem>jpeg</asp:ListItem>
                    <asp:ListItem>gif</asp:ListItem>
                    <asp:ListItem>excel</asp:ListItem>
                    <asp:ListItem>word2007</asp:ListItem>
                </asp:DropDownList>
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send Email" />
            </td>
            <td>
                <div id="divmessage" style="width: 69px; background-color: #BDB8FA; display: none">
                    發送成功
                </div>
            </td>
        </tr>
    </table>
    <cc1:StiWebViewer ID="viewer1" runat="server" ShowBookmarksButton="False" ShowParametersButton="False" />
    <cc1:StiWebReport ID="report1" runat="server" />
    </form>
</body>
</html>

0 人点赞