• 导出,邮件发送,画图项目公共方法


    using System;
    using System.Data;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using Excel = Microsoft.Office.Interop.Excel;
    using System.Text.RegularExpressions;
    using System.Threading;
    using System.Globalization;
    using System.Net.Mail;
    using Yokohama.ApplicationBlocks.Data;

    namespace Yokohama.Toolkits
    {
        public class General
        {
            #region 加载语言信息
            /// <summary>
            /// 加载指定语言信息
            /// </summary>
            /// <param name="UserCulture">用户语言</param>
            public static void InitializeCulture(string UserCulture)
            {
                // there is a user language setting in the profile: switch to it
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);

            }
            #endregion

            #region 取得当前语言对应的信息
            /// <summary>
            /// 取得当前语言对应的信息
            /// </summary>
            /// <param name="Language">语言</param>
            /// <param name="zhMessage">中文信息</param>
            /// <param name="enMessage">英文信息</param>
            /// <returns>String</returns>
            public static string GetMessageByLanguage(string Language, string zhMessage, string enMessage)
            {
                if (Language.Trim().ToUpper() == "CHINESE")
                {
                    return zhMessage;
                }
                else
                {
                    return enMessage;
                }
            }
            #endregion

            #region ExportDataToExcel
            /// <summary>
            /// 导出Excel文件
            /// </summary>
            /// <param name="dataTable">导出的DataTable</param>
            /// <param name="TitleBold">标题是否Bold</param>
            /// <param name="DrawLine">是否显示框线</param>
            /// <param name="FontSize">字体大小</param>
            /// <param name="StartColIndex">导出数据的起始列</param>
            public static void ExportDataToExcel(DataTable dataTable, bool TitleBold, bool DrawLine, int FontSize, int StartColIndex)
            {
                Excel.Application excel = new Excel.Application();
                try
                {

                    //Excel.Worksheet m_objSheet;//当前Sheet对象
                    Excel.Range m_Range;//当前Range对象

                    excel.Application.Workbooks.Add(true);//引用Excel工作簿

                    excel.Visible = true;//使Excel可视
                    //统计rows和columns
                    int colCount = dataTable.Columns.Count;
                    int rowCount = dataTable.Rows.Count;
                    //写入栏位标题
                    for (int j = StartColIndex; j < colCount; j++)
                    {
                        excel.Cells[1, j + 1 - StartColIndex] = dataTable.Columns[j].ToString();
                    }
                    m_Range = excel.get_Range(excel.Cells[1, 1], excel.Cells[1, colCount - StartColIndex]);
                    m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                    m_Range.Font.Size = 11;
                    if (TitleBold) m_Range.Font.Bold = true;
                    if (DrawLine) m_Range.Borders.LineStyle = 1;
                    //写入Cell数据
                    for (int i = 0; i < rowCount; i++)
                    {
                        for (int j = StartColIndex; j < colCount; j++)
                        {
                            excel.Cells[i + 2, j + 1 - StartColIndex] = dataTable.Rows[i][j].ToString();
                        }
                    }
                    m_Range = excel.get_Range(excel.Cells[2, 1], excel.Cells[rowCount + 1, colCount - StartColIndex]);
                    if (DrawLine) m_Range.Borders.LineStyle = 1;
                    m_Range.EntireColumn.AutoFit();
                    m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                    m_Range.Font.Size = FontSize;
                    m_Range.NumberFormatLocal = "@ ";//文本格式

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            #endregion

            #region ExportDataToTxt
            /// <summary>
            /// //导出txt文件
            /// </summary>
            /// <param name="dataTable">导出的dataTable</param>
            /// <param name="SavePath">保存路径</param>
            /// <param name="FileName">保存文件名</param>
            /// <param name="StartColIndex">导出的数据开始列</param>
            public static void ExportDataToTxt(DataTable dataTable, string SavePath, string FileName, int StartColIndex)
            {
                try
                {
                    string strPathName = "";
                    if (SavePath.Substring(SavePath.Length - 1) == "\\")
                    {
                        strPathName = SavePath + FileName;
                    }
                    else
                    {
                        strPathName = SavePath + "\\" + FileName;
                    }
                    StreamWriter sw = new StreamWriter(strPathName, false, System.Text.Encoding.Unicode);
                    sw.Flush();
                    string stringList = "";
                    for (int j = StartColIndex; j < dataTable.Columns.Count; j++)
                    {
                        //((char)9).ToString(): tab分隔符
                        stringList += (stringList == "" ? "" : ((char)9).ToString()) + dataTable.Columns[j].ToString();
                    }
                    sw.WriteLine(stringList);
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        stringList = "";
                        for (int j = StartColIndex; j < dataTable.Columns.Count; j++)
                        {
                            stringList += (stringList == "" ? "" : ((char)9).ToString()) + dataTable.Rows[i][j].ToString().Trim();
                        }
                        sw.WriteLine(stringList);

                    }
                    sw.Flush();
                    sw.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            #endregion

            #region 数字处理
            /// <summary>
            /// 从字串中取得数值
            /// </summary>
            /// <param name="value">目标字串</param>
            /// <returns>数值</returns>
            public static string GetQtyFromString(string value)
            {
                string returnValue = "";
                for (int i = value.Length; i > 0; i--)
                {
                    if (Regex.IsMatch(value.Substring(0, i), @"^[+-]?\d*[.]?\d*$"))
                    {
                        returnValue = value.Substring(0, i);
                        break;
                    }
                }
                return returnValue;
            }

            /// <summary>
            /// 判断是否为数字
            /// </summary>
            /// <param name="value">目标字串</param>
            /// <returns>True/False</returns>
            public static bool IsNumeric(string value)
            {
                if (Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            #endregion

            #region "SendMail"

            /// <summary>
            /// SendMail
            /// </summary>
            /// <param name="MailTo">收件人</param>
            /// <param name="MailFrom">发件人</param>
            /// <param name="subject">主题</param>
            /// <param name="body">Mail内容</param>
            /// <param name="IsBodyHtml">是否使用Html</param>
            /// <param name="BodyEncoding">Mail内容格式</param>
            /// <returns>Ture/False</returns>
            public static bool SendMail(string MailTo, string MailFrom, string subject, string body, bool IsBodyHtml, Encoding BodyEncoding)
            {
                string strMailHost = "10.1.73.253";
                string MailCC = "";
                MailPriority Priority = MailPriority.Normal;
                return SendMail(strMailHost, MailTo, MailFrom, MailCC, subject, body, IsBodyHtml, BodyEncoding, Priority);
            }

            /// <summary>
            /// SendMail
            /// </summary>
            /// <param name="MailTo">收件人</param>
            /// <param name="MailFrom">发件人</param>
            /// <param name="MailCC">CC</param>
            /// <param name="subject">主题</param>
            /// <param name="body">Mail内容</param>
            /// <param name="IsBodyHtml">是否使用Html</param>
            /// <param name="BodyEncoding">Mail内容格式</param>
            /// <returns>Ture/False</returns>
            public static bool SendMail(string MailTo, string MailFrom, string MailCC, string subject, string body, bool IsBodyHtml, Encoding BodyEncoding)
            {
                string strMailHost = "10.1.73.253";
                MailPriority Priority = MailPriority.Normal;
                return SendMail(strMailHost, MailTo, MailFrom, MailCC, subject, body, IsBodyHtml, BodyEncoding, Priority);
            }
            /// <summary>
            /// SendMail
            /// </summary>
            /// <param name="MailTo">收件人</param>
            /// <param name="MailFrom">发件人</param>
            /// <param name="MailCC">CC</param>
            /// <param name="subject">主题</param>
            /// <param name="body">Mail内容</param>
            /// <param name="IsBodyHtml">是否使用Html</param>
            /// <param name="BodyEncoding">Mail内容格式</param>
            /// <param name="Priority">优先级</param>
            /// <returns>Ture/False</returns>
            public static bool SendMail(string MailTo, string MailFrom, string MailCC, string subject, string body, bool IsBodyHtml, Encoding BodyEncoding, MailPriority Priority)
            {
                string strMailHost = "IP地址";
                return SendMail(strMailHost, MailTo, MailFrom, MailCC, subject, body, IsBodyHtml, BodyEncoding, Priority);
            }

            /// <summary>
            /// SendMail
            /// </summary>
            /// <param name="MailHost">SMTP事务主机</param>
            /// <param name="MailTo">收件人</param>
            /// <param name="MailFrom">发件人</param>
            /// <param name="MailCC">CC</param>
            /// <param name="subject">主题</param>
            /// <param name="body">Mail内容</param>
            /// <param name="IsBodyHtml">是否使用Html</param>
            /// <param name="BodyEncoding">Mail内容格式</param>
            /// <param name="Priority">优先级</param>
            /// <returns>Ture/False</returns>
            public static bool SendMail(string MailHost, string MailTo, string MailFrom, string MailCC, string subject, string body, bool IsBodyHtml, Encoding BodyEncoding, MailPriority Priority)
            {
                SmtpClient client = new SmtpClient();
                //设置用于 SMTP 事务的主机的名称,填IP地址也可以了
                //client.Host = "IP地址";
                client.Host = MailHost;
                client.UseDefaultCredentials = false;
                //client.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["MailFrom"],ConfigurationManager.AppSettings["MailFromPwd"]);
                client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                MailMessage message = new MailMessage();

                message.From = new MailAddress(MailFrom);
                //MailTO
                string[] arrMailTo = MailTo.Split(';');
                for (int i = 0; i < arrMailTo.Length; i++)
                {
                    if (arrMailTo[i].Trim() != "")
                    {
                        message.To.Add(new MailAddress(arrMailTo[i].Trim()));
                    }
                }
                string[] arrMialCC = MailCC.Split(';');
                //CC
                for (int i = 0; i < arrMialCC.Length; i++)
                {
                    if (arrMialCC[i].Trim() != "")
                    {
                        message.CC.Add(new MailAddress(arrMialCC[i].Trim()));
                    }
                }
                message.Subject = subject;
                message.Body = body;
                message.BodyEncoding = BodyEncoding; //System.Text.Encoding.UTF8;          
                message.IsBodyHtml = IsBodyHtml;
                message.Priority = Priority;

                try
                {
                    client.Send(message);
                    return true;
                }
                catch
                {
                    return false;
                }

            }
            #endregion

            #region GetWashingMaster

            /// <summary>
            /// 取得指定WashCode的WashMaster
            /// </summary>
            /// <param name="ConnectionString">连接字串</param>
            /// <param name="WashCodeColumnName">洗水符号栏位名称</param>
            /// <param name="WashMasterColumnName">洗水文字栏位名称</param>
            /// <param name="CommanddText">执行文本</param>
            public static string GetWashingMasterByCode(string ConnectionString, string WashCodeColumnName, string WashMasterColumnName, string CommanddText)
            {
                int MaxLength = 40;
                string WashCodeFontFamily = "BBCWASH_2012";
                string WashCodeSize="25pt";
                string WashMasterFontFamily="Times New Roman";
                string WashMasterSize="12pt";
                return GetWashingMasterByCode(ConnectionString, WashCodeColumnName, WashMasterColumnName, CommanddText, MaxLength, WashCodeFontFamily, WashCodeSize, WashMasterFontFamily, WashMasterSize);
            }

            /// <summary>
            /// 取得指定WashCode的WashMaster
            /// </summary>
            /// <param name="ConnectionString">连接字串</param>
            /// <param name="WashCodeColumnName">洗水符号栏位名称</param>
            /// <param name="WashMasterColumnName">洗水文字栏位名称</param>
            /// <param name="CommanddText">执行文本</param>
            public static string GetWashingMasterByCode(string ConnectionString, string WashCodeColumnName, string WashMasterColumnName, string CommanddText,int MaxLength)
            {
                string WashCodeFontFamily = "BBCWASH_2012";
                string WashCodeSize = "25pt";
                string WashMasterFontFamily = "Times New Roman";
                string WashMasterSize = "12pt";
                return GetWashingMasterByCode(ConnectionString, WashCodeColumnName, WashMasterColumnName, CommanddText, MaxLength, WashCodeFontFamily, WashCodeSize, WashMasterFontFamily, WashMasterSize);
            }

            /// <summary>
            /// 取得指定WashCode的WashMaster
            /// </summary>
            /// <param name="ConnectionString">连接字串</param>
            /// <param name="WashCodeColumnName">洗水符号栏位名称</param>
            /// <param name="WashMasterColumnName">洗水文字栏位名称</param>
            /// <param name="CommanddText">执行文本</param>
            /// <param name="Maxlength">显示最大长度</param>
            /// <param name="WashCodeFontFamily">洗水符号字体</param>
            /// <param name="WashCodeSize">洗水符号字体大小</param>
            public static string GetWashingMasterByCode(string ConnectionString, string WashCodeColumnName, string WashMasterColumnName, string CommanddText, int MaxLength, string WashCodeFontFamily, string WashCodeSize)
            {
                string WashMasterFontFamily = "Times New Roman";
                string WashMasterSize = "12pt";
                return GetWashingMasterByCode(ConnectionString, WashCodeColumnName, WashMasterColumnName, CommanddText, MaxLength, WashCodeFontFamily, WashCodeSize, WashMasterFontFamily, WashMasterSize);
            }
            
            /// <summary>
            /// 取得指定WashCode的WashMaster
            /// </summary>
            /// <param name="ConnectionString">连接字串</param>
            /// <param name="WashCodeColumnName">洗水符号栏位名称</param>
            /// <param name="WashMasterColumnName">洗水文字栏位名称</param>
            /// <param name="CommanddText">执行文本</param>
            /// <param name="MaxLength">最大显示长度</param>
            /// <param name="WashCodeFontFamily">洗水符号字体</param>
            /// <param name="WashCodeSize">洗水符号字体大小</param>
            /// <param name="WashMasterFontFamily">洗水文字字体</param>
            /// <param name="WashMasterSize">洗水文字大小</param>
            /// <returns>WashingMaster</returns>
            public static string GetWashingMasterByCode(string ConnectionString, string WashCodeColumnName, string WashMasterColumnName, string CommanddText, int MaxLength, string WashCodeFontFamily, string WashCodeSize, string WashMasterFontFamily, string WashMasterSize)
            {
                try
                {
                    DataTable dt = SqlHelper.ExecuteDataset(ConnectionString, CommandType.Text, CommanddText).Tables[0];
                    int intCurLength = 0;//当前长度
                    string strWashMaster = "";
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string strWashValue = dt.Rows[i][WashMasterColumnName].ToString().Trim();
                        if (strWashValue != "")
                        {
                            if (strWashMaster == "")
                            {
                                //读取WashCode
                                strWashMaster = "<span style='font-family:" + WashCodeFontFamily + ";font-size:" + WashCodeSize + ";'>" + dt.Rows[i][WashCodeColumnName].ToString().Trim() + "</span><br /><span style='font-family:" + WashMasterFontFamily + ";font-size:" + WashMasterSize + ";'>" + strWashValue;
                                intCurLength += strWashValue.Length + 1;
                            }
                            else
                            {
                                //当多个Wash长度加起来不超过maxLength时,则将他们放在一起显示;否则分行显示
                                int intWashLength = strWashValue.Length + 1;
                                if (intCurLength + intWashLength > MaxLength)
                                {
                                    strWashMaster += "<br />" + strWashValue;
                                    intCurLength = intWashLength;
                                }
                                else
                                {
                                    strWashMaster += " " + strWashValue;
                                    intCurLength += intWashLength;
                                }
                            }
                        }
                    }
                    return strWashMaster + "</span>";
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }


            #endregion
    }
    }

  • 相关阅读:
    LabVIEW(数据库连接)
    单片机之静态局部变量static
    LabVIEW(数据库自动编号)
    为什么大电容滤低频,小电容滤高频 ?(转载)
    电容、频率的一点小关系(项目有关)
    深入理解计算机系统 第三部分程序间的交互和通信
    深入理解计算机系统chapter9
    深入理解计算机系统chapter8
    深入理解计算机系统chapter7
    深入理解计算机系统chapter6
  • 原文地址:https://www.cnblogs.com/yzenet/p/2501099.html
Copyright © 2020-2023  润新知