我在南通移动成本系统中的技术归纳

来源:互联网 发布:济南编程培训58 编辑:程序博客网 时间:2024/04/28 09:51

1.枚举

 1)定义

  例如:using System;
     using System.Collections;
     using System.Collections.Generic;
     using System.Text;
     using System.Drawing;
     using System.Data; 

namespace Suzsoft.DotNet.Common.Enum
{
    public class NTCMSEnum
    {          

       #region BillTypeList
        /// <summary>
        /// 申请单类型
        /// </summary>
        public enum BillTypeList
        {
            //个人申请单
            BillPersonCost = 10,

            //集团工单
            BillGroupCost = 20,

            //信用用户工单
            BillCreditUserCost = 30
        }

        /// <summary>
        /// 申请单类型
        /// </summary>
        /// <returns></returns>
        public static SortedList BillTypeString()
        {
            SortedList SL = new SortedList();
            SL.Add("10", "个人成本申请单");
            SL.Add("20", "集团成本工单");
            SL.Add("30", "信用用户申请单");

            return SL;
        }
        #endregion

2)读取

  using Suzsoft.DotNet.Common.Enum;

    string strBillType = (Convert.ToInt32(NTCMSEnum.BillTypeList.BillGroupCost)).ToString();//获取值

   SortedList sl = Suzsoft.DotNet.Common.Enum.NTCMSEnum.BillTypeString();
          string strBillType = sl.GetByIndex(sl.IndexOfKey(strBillType )).ToString();//获取所代表的含义 

2.实现GridView中行单选radiobutton

 例如: <script language="javascript" type="text/javascript">
        //取消其他RadioButton的选中
        function OnSelect(obj)
       {
            elements = obj.form.elements;
            for(var i=0;i<elements.length;i++)
           {
                if(elements[i].type =="radio" && elements[i].id != obj.id)
                {
                    elements[i].checked = false;
                }
            }
        }
         function OpenWindow(url,width,height)
                {
                    var elements= document.getElementsByTagName("input");
                     for (var i=0;i<elements.length;i++)
                     {    
                         if(elements[i].type =="radio"&&elements[i].checked)
                          {                              
                              var strID= elements[i].value;
                              window.open(url+"?CancelBillID="+strID,"","width="+width+",height="+height+",top=50,left=100,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no");
                              return;                                                      
                          }
                     }
                     window.alert("请选择所要处理的数据行!");
                }
             
        function open2()
        {
         OpenWindow("../PersonalCost/CostView.aspx",800,600);
        }     
    </script>
 <asp:GridView ID="sgvPersonCost" runat="server" AllowCascade="False" AllowScrollBars="False"
                                            ArrowColor="Gray" AutoGenerateColumns="False" BorderCollapse="True"       DarkshadowColor="White"
                                            DivBorderColor="" DivBorderStyle="NotSet" DivBorderWidth="" DownImageURL="" ExtendedPagerStyle="NotSet"
                                            ExtendedStyle="NotSet" FaceColor="White" HighlightColor="LightGray" ScrollBarsCss=""
                                            ShadowColor="LightGray" ShowActionToolBar="False" SubGridViewTemplateID="" ThreeDlightColor="White"
                                            TrackColor="White" UpImageURL="" UseStyleForSubGrid="False" Width="100%" XScrollBar="auto"
                                            YScrollBar="auto" DataKeyNames="BILL_ID" AllowPaging="True" AllowSorting="True"
                                            OnRowDataBound="sgvPersonCost_RowDataBound" OnPageIndexChanging="sgvPersonCost_PageIndexChanging">
                                            <columns>
                                                <asp:TemplateField HeaderText="选择">
                                                    <ItemTemplate>
                                                        <input type="radio" id="rbIsAcceptProcess" name="RadioButton" onclick="OnSelect(this)"
                                                            value='<%# Eval("BILL_ID")%>' runat="server" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                 <asp:BoundField DataField="BILL_ID" HeaderText="申请单号">
                                                    <ItemStyle HorizontalAlign="Center"></ItemStyle>
                                                    <HeaderStyle Width="10%" HorizontalAlign="Center"></HeaderStyle>
                                                </asp:BoundField>
                                                <asp:BoundField DataField="GSM_USER_ID" HeaderText="客户代码" Visible="false">
                                                    <ItemStyle HorizontalAlign="center"></ItemStyle>
                                                    <HeaderStyle HorizontalAlign="center"></HeaderStyle>
                                                </asp:BoundField>
      </columns>
                                        </asp:GridView>

 

 <table width="100%">
        <tr>
            <td align="right">
                <input id="btnLook" type="button" value="撤 消" onclick="open2();" runat="server" />&nbsp;&nbsp;
            </td>
        </tr>
    </table>

3.脚本

  1)弹出信息

   public static void Alert(System.Web.UI.Page pageCurrent, string strMsg)
    {

        //Replace /n
        strMsg = strMsg.Replace("/n", "//n");
        //Replace /r
        strMsg = strMsg.Replace("/r", "//r");
        //Replace "
        strMsg = strMsg.Replace("/"", "///"");
        //Replace '
        strMsg = strMsg.Replace("/'", "///'");

        pageCurrent.ClientScript.RegisterStartupScript(pageCurrent.GetType(),
            System.Guid.NewGuid().ToString(),
            "<script>window.alert('" + strMsg + "')</script>"
            );
    }

2)绑定GridView 时显示表头

  /// <summary>
    /// 绑定GridView时显示表头
    /// </summary>
    /// <param name="sgv"></param>
    public static void BindNullSGridView(GridView sgv)
    {
        if (sgv != null)
        {
            if (sgv.Rows.Count == 0)//在gridview的数据为空的情况下显示gird的title
            {
                //排序冲突

                sgv.AllowSorting = false;
                DataTable dt = new DataTable();
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                sgv.DataSource = dt;
                try
                {
                    sgv.DataBind();//数据为空,此时会报错,但是grid的标题会显示出来
                }
                catch
                {
                    //不做处理
                }
                if (sgv.Rows.Count > 0)
                {
                    sgv.Rows[0].Visible = false;
                }
            }
            else
            {
            }

        }
    }

3) 刷新父窗口, 并且关闭当前窗口
            this.Page.ClientScript.RegisterStartupScript(this.GetType(),
                                                         Guid.NewGuid().ToString(),
                                                          "<script type='text/javascript'>window.opener.location.href=window.opener.location.href;window.close();</script>"
                                                          );

4)打印

<script language="javascript" type="text/javascript">   
     function doPrint()
    {
        bdhtml=window.document.body.innerHTML;
        sprnstr="<!--startprint-->";
        eprnstr="<!--endprint-->";
        prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
        prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
        window.document.body.innerHTML=prnhtml;   
        window.print();
        window.document.body.innerHTML=bdhtml;
    }
 </script>

<body>
 <form id="form1" runat="server">
  <!--startprint-->
  <div>
   <table width="100%" border="1" cellpadding="3" cellspacing="0">
    <tr>
     <%--<td>
                        <p class="MsoNormal" style="margin: 0cm 0cm 1pt; text-indent: 160.65pt; line-height: 150%;
                            mso-char-indent-count: 10.0">
                            <b><span  class="headTitle">集团营销成本使用工单 工单号: </span></b><b>
                                <asp:Label ID="Label1"  CssClass="headInfo"  runat="server" ></asp:Label>
                                <asp:Label ID="lblBillID" runat="server"></asp:Label>
                                </b>
                        </p>
                    </td>--%>
     <td align="center">
      <asp:Label ID="lblTitle" runat="server" Text="集团营销成本使用工单" Font-Bold="true"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;
      <asp:Label runat="server" Text="工单号:"></asp:Label>
      <asp:Label ID="lblBillID" runat="server"></asp:Label>
     </td>
    </tr>

</table>
  </div>
  <!--endprint-->

   <input id="btnPrint" runat="server" class="ipt" type="button" name="button" value="打 印"
                                onclick="  javascript  :doPrint();"  visible="false"/>

 

5.gridview导出到excell

using Microsoft.Office.Interop.Excel;

例如:一.配置

<appSettings>
  <!--个人成本相关的报表及打印文件存放路径,可以建子目录-->
  <add key="Personal_ReportFilePath" value="../Files/PersonalReportFiles"/>
  <add key="Personal_TempReportFilePath" value="../Files/TempReportFiles"/>
 </appSettings>

 二.下载文件

#region DownloadFile
    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="filename"></param>
    protected void DownloadFile(string filename)
    {
        try
        {
            string saveFileName = "test.xls";
            int intStart = filename.LastIndexOf("//") + 1;
            saveFileName = filename.Substring(intStart, filename.Length - intStart);

            Response.Clear();
            Response.Charset = "utf-8";
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.UTF8;

            Response.AppendHeader("Content-Disposition", "attachment;filename=" + saveFileName);
            Response.WriteFile(filename);
            Response.Flush();
            Response.Close();
            Response.End();
        }
        catch (Exception ex)
        {
            string strMsg = ex.Message;
        }
    }
    #endregion

 三.导出数据

#region btnExport
    /// <summary>
    /// 导出数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExport_ServerClick(object sender, EventArgs e)
    {
        if (sgvPersonCost.Rows.Count == 0)
        {
            UIHelper.BindNullSGridView(sgvPersonCost);
            return;
        }

        string strDate = GetDate();
        string strTemplateFile = Server.MapPath(ConfigurationManager.AppSettings["Personal_TempReportFilePath"]) + "//日报表Excel报表样式.xls";
        string strNewFilePath = Server.MapPath(ConfigurationManager.AppSettings["Report_DayReportFilesPath"]);
        Delete_Files(strNewFilePath);
        string strstrNewFileName = "";
        if (CalendarPicker1.Date != "" || CalendarPicker1.Date != null)
        {
            strstrNewFileName = (new CM_PERSONAL_COST_ITEM()).ExportFinanceStatistics(txtOperatorPhonenumber.Text.Trim(), CalendarPicker1.Date, ddlMaterial.SelectedValue.ToString(), ddlMaterialCategory.SelectedValue, strTemplateFile, strNewFilePath);
        }
        else
        {
            strstrNewFileName = (new CM_PERSONAL_COST_ITEM()).ExportFinanceStatistics(txtOperatorPhonenumber.Text.Trim(), strDate.Trim(), ddlMaterial.SelectedValue, ddlMaterialCategory.SelectedValue, strTemplateFile, strNewFilePath);
        }
        DownloadFile(strstrNewFileName);
    }
    #endregion

四.类方法

    #region FinanceStatistics
        /// <summary>
        /// 日报表统计
        /// </summary>
        /// <param name="strOperatorPhone">操作员工号</param>
        ///  <param name="strMaterialType">物品类型</param>
        /// <param name="strDate">当天时间</param>
        /// <returns></returns>
        public DataSet FinanceStatistics(string strOperatorPhone, string strDate, string strCategoryID, string strParentID)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                Suzsoft.DotNet.DAL.DataAccessParamCollection dpc = new Suzsoft.DotNet.DAL.DataAccessParamCollection();

                string strSql = "select m.material_name,SUM (c.cost_qty)as counts, SUM (c.cost_qty*c.unit_price )as FinanceStatistics";
                strSql += " from cm_personal_cost_item c";
                strSql += " join cm_material m on m.material_id=c.material_id";
                strSql += "  join cm_material_category mc on mc.category_id=m.category_id";
                strSql += " join cm_personal_cost p on p.bill_guid=c.bill_id where 1=1";

                if (!strOperatorPhone.Trim().Equals(""))
                {
                    strSql += " and p.operator_cellphone=" + "'" + strOperatorPhone + "'";
                }
                if (!strCategoryID.Trim().Equals(""))
                {
                    strSql += " and mc.category_id=" + "'" + strCategoryID + "'";
                }
                else
                {
                    if (!strParentID.Trim().Equals(""))
                    {
                        DataSet ds = GetCategoryIDByParentID(strParentID);
                        if (ds.Tables[0].Rows.Count>0)
                        {
                            strSql += " and mc.parent_id=" + "'" + strParentID + "'";
                        }
                        else
                        {
                            strSql += " and mc.category_id=" + "'" + strParentID + "'";
                        }
                    }
                }

                if (!strDate.Trim().Equals(""))
                {
                    strSql += " and p.transact_time like to_date('" + strDate + "','yyyy-mm-dd')";
                }

                strSql += " group by  m.material_name";

                return broker.ExecuteSQLForDataSet(strSql);

                //if (!strOperatorPhone.Trim().Equals(""))
                //{
                //    strSql += " and p.operator_cellphone=@operator_phonenumber";
                //    dpc.AddWithValue("@operator_phonenumber", strOperatorPhone);
                //}

                //if (!strMaterialType.Trim().Equals(""))
                //{
                //    strSql += " and mc.category_name=@category_name";
                //    dpc.AddWithValue("@category_name", strMaterialType);
                //}

                //if (!strDate.Trim().Equals(""))
                //{
                //    strSql += " and p.transact_time like to_date('@workeddate','yyyy-mm-dd')";
                //    dpc.AddWithValue("@workeddate", strDate);
                //}

                //return broker.ExecuteCommandForDataSet(strSql, dpc);
            }
        }
        #endregion

  #region FinanceStatistics
        /// <summary>
        /// 日报表统计
        /// </summary>
        /// <param name="strOperatorPhone">操作员工号</param>
        /// <param name="strDate">当天日期</param>
        /// <returns></returns>
        public DataSet FinanceStatistics(string strOperatorPhone, string strDate,string strCategoryID,string strParentID)
        {
            return (new CM_PERSONAL_COST_ITEMDA()).FinanceStatistics(strOperatorPhone, strDate, strCategoryID,strParentID);
        }
        #endregion

       

 

#region ExportFinanceStatistics
        /// <summary>
        /// 导出数据:日报表
        /// </summary>
        /// <param name="strOperatorPhone">操作员工号</param>
        /// <param name="strDate">当天日期</param>
        /// <param name="strTemplateFile">摸版文件名</param>
        /// <param name="strNewFilePath">新的文件路径</param>
        /// <returns></returns>
        public String ExportFinanceStatistics(string strOperatorPhone, string strDate, string strCategoryID,string strParentID, string strTemplateFile, string strNewFilePath)
        {
            string strNewFileName = "";
            DataSet ds = (new CM_PERSONAL_COST_ITEM()).FinanceStatistics(strOperatorPhone, strDate, strCategoryID,strParentID);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            System.Data.DataTable dt = ds.Tables[0];
            Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook xBook = null;
            Microsoft.Office.Interop.Excel.Worksheet xSheet = null;
            object objOpt = System.Reflection.Missing.Value;
            try
            {
                string strGUID = Guid.NewGuid().ToString();
                strNewFileName = string.Format("{0}//{1}.xls", strNewFilePath, strGUID + "NTCMS");//strOperatorPhone
                if (File.Exists(strTemplateFile))
                {
                    FileInfo fi = new FileInfo(strTemplateFile);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                    {
                        fi.Attributes = FileAttributes.Normal;//改变template文件的只读属性
                    }
                }
                System.IO.File.Copy(strTemplateFile, strNewFileName, true);
                xBook = xApp.Workbooks.Open(strNewFileName, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt, objOpt);
                xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];
                for (int i = 1; i <= dt.Rows.Count; i++)
                {
                    xSheet.Cells[3 + i, 1] = Convert.ToString(dt.Rows[i - 1]["material_name"]);
                    xSheet.Cells[3 + i, 2] = dt.Rows[i - 1]["counts"];
                    xSheet.Cells[3 + i, 3] = dt.Rows[i - 1]["FinanceStatistics"];
                }
                xBook.Save();
                xBook.Close(true, objOpt, objOpt);
                xApp.Quit();
                return strNewFileName;
            }
            catch (Exception ex)
            {
                string strMsg = ex.Message;
                if (xBook != null)
                {
                    xBook.Close(true, objOpt, objOpt);
                }
                if (xApp != null)
                {
                    xApp.Quit();
                }
                return null;
            }
        }
        #endregion

6............   判断输入的字符串是否为半角数据
转半角的函数(DBC case)

#region  CheckNumericString
    /// <summary>
    /// 判断输入的字符串是否为半角数据
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    protected bool CheckNumericString(string temp)
    {
        if (temp.Trim() != "")
        {
            char[] arr = temp.ToCharArray();
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] > 57 || arr[i] < 48)
                {
                    return false;
                }
            }

        }
        return true;
    }
    #endregion

    #region ToDBC
    /// <summary>
    ///  转半角的函数(DBC case)
    ///
    /// 全角空格为12288,半角空格为32
    /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
    /// </summary>
    /// <param name="input">任意字符串</param>
    /// <returns></returns>
    public string ToDBC(string input)
    {
        char[] c = input.ToCharArray();
        for (int i = 0; i < c.Length; i++)
        {
            if (c[i] == 12288)
            {
                c[i] = (char)32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char)(c[i] - 65248);
        }
        return new string(c);
    }


    #endregion

 

7.......................发送短信

 /// <summary>
    /// 短信发送接口
    /// </summary>
    /// <param name="strMobileNo">手机号码</param>
    /// <param name="strContent">短信内容</param>
    /// <returns></returns>
    public string SendMessage(string strMobileNo, string strContent)
    {
        string str = "";
        SMS sms = new SMS();
        try
        {
            sms.SendSMS(strMobileNo, strContent);
        }
        catch (Exception ex)
        {
            str = ex.Message;
        }
        return str;
    }
/// <summary>
    /// 短信发送接口
    /// </summary>
    /// <param name="strMobileNo">手机号码数组</param>
    /// <param name="strContent">短信内容</param>
    /// <returns></returns>
    public string SendMessage(string[] strMobileNo, string strContent)
    {
        if (strMobileNo != null && !String.IsNullOrEmpty(strContent))
        {
            SMS sms = new SMS();
            try
            {
                for (int i = 0; i < strMobileNo.Length; i++)
                {
                    sms.SendSMS(strMobileNo[i], strContent);
                }
            }
            catch { }
        }
        return "";
    }

    /// <summary>
    /// 短信发送接口
    /// </summary>
    /// <param name="dicMessage">手机号码和内容组合</param>
    /// <returns></returns>
    public string SendMessage(Dictionary<string, string> dicMessage)
    {
        if (dicMessage != null)
        {
            SMS sms = new SMS();
            try
            {
                foreach (string strkey in dicMessage.Keys)
                {
                    sms.SendSMS(strkey, dicMessage[strkey]);
                }
            }
            catch { }
        }
        return "";
    }

 

     public class SMS
 {
  public int SendSMS(string Cellphone, string Content)
  {
   if (String.IsNullOrEmpty(Cellphone) || Cellphone.Length > 12)
   {
    throw new Exception("手机号码格式不正确!");
   }

   if (String.IsNullOrEmpty(Content) || Content.Length > 140)
   {
    throw new Exception("短信内容长度不正确!");
   }

   using (SMSDA sms = new SMSDA())
   {
    return sms.Invoke_P_SMS_SEND(Cellphone, Content);
   }
  }
 }


       public class SMSDA : DataAccessBase
 {
  /// <summary>
  ///  调用用于发送短信的存储过程P_SMS_SEND
  /// </summary>
  /// <param name="Cellphone">发送对象的手机号码</param>
  /// <param name="Content">发送内容,不得超过140个字符</param>
  /// <param name="broker"></param>
  /// <returns></returns>
  public int Invoke_P_SMS_SEND(string Cellphone, string Content, DataAccessBroker broker)
  {
   if (broker == null)
   {
    throw new System.ObjectDisposedException(GetType().FullName);
   }

   DataAccessParamCollection dpc = new DataAccessParamCollection();
   dpc.AddWithValue("@mobile_no", Cellphone);
   dpc.AddWithValue("@source_id", 5);
   dpc.AddWithValue("@sms_content", Content);
   dpc.AddWithValue("@booking_time", DBNull.Value);
   dpc.AddWithValue("@log_type", 0);
   dpc.AddWithValue("@pri_id", 0);
   dpc.AddWithValue("@result", 0);

   return broker.ExecuteCommand("P_SMS_SEND", dpc);
  }

  public int Invoke_P_SMS_SEND(string Cellphone, string Content)
  {
   using (DataAccessBroker objBroker = DataAccessFactory.Instance("SMS"))
   {
    return Invoke_P_SMS_SEND(Cellphone, Content, objBroker);
   }
  }
 }

     public class DataAccessBase : IDisposable
 {

  public DataAccessBase()
  {

  }


  public virtual void Dispose()
  {

  }

  public static string FormatToSql(string parameter)
  {
   return " '" + parameter + "'";
  }

  public static string FormatToSql(int parameter)
  {
   return " " + parameter.ToString() + " ";
  }

  public static string FormatToSql(double parameter)
  {
   return " " + parameter.ToString() + " ";
  }

  public static string FormatToSql(DateTime parameter)
  {
   return " '" + parameter.ToString() + "' ";
  }

  #region IDisposable Members

  void IDisposable.Dispose()
  {
   GC.SuppressFinalize(true); // as a service to those who might inherit from us
  }

  #endregion


 }

 

原创粉丝点击