Exemplo n.º 1
0
        /// <summary>
        /// 获取季度的第一天
        /// </summary>
        /// <param name="month">2018Q1</param>
        /// <returns></returns>
        public static DateTime GetQuarterFirstday(string quarter)
        {
            if (quarter == "")
            {
                quarter = GetYearQuarterString();
            }

            int yearIndex    = ConvertObject.ToInt32(quarter.Substring(0, 4));
            int quarterIndex = ConvertObject.ToInt32(quarter.Substring(5, 1));

            switch (quarterIndex)
            {
            case 4:
                return(new DateTime(yearIndex, 10, 1));

            case 3:
                return(new DateTime(yearIndex, 7, 1));

            case 2:
                return(new DateTime(yearIndex, 4, 1));

            default:
                return(new DateTime(yearIndex, 1, 1));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 将IP地址转换为长整型数字
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static long ToIpAddrValue(string obj)
        {
            long result = -1;

            string[] arrnum = obj.Split('.');

            if (arrnum.Length != 4)
            {
                return(result);
            }

            int[] ints = { 0, 0, 0, 0 };

            for (int i = 0; i < 4; i++)
            {
                ints[i] = ConvertObject.ToInt32(arrnum[i], -1);
                if (ints[i] < 0 && ints[i] > 255)
                {
                    return(result);
                }
            }

            result = (long)ints[0] * 256 * 256 * 256 + (long)ints[1] * 256 * 256 + (long)ints[2] * 256 + (long)ints[3];

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取 DateTimeString 的季度
        /// </summary>
        /// <param name="data">yyyy-MM-dd 或 yyyyMM</param>
        /// <returns></returns>
        public static int GetQuarterNumber(string data)
        {
            if (data.Length == 10)
            {
                DateTime date = ConvertObject.ToDateTime(data);
                return(GetQuarterNumber(date));
            }
            else if (data.Length == 6)
            {
                string month = data.Substring(4, 2);
                switch (month)
                {
                case "01":
                case "02":
                case "03":
                    return(1);

                case "04":
                case "05":
                case "06":
                    return(2);

                case "07":
                case "08":
                case "09":
                    return(3);

                case "10":
                case "11":
                case "12":
                    return(4);
                }
            }
            return(0);
        }
Exemplo n.º 4
0
 /// <summary>
 /// 将类型转换为Boolean
 /// </summary>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static Boolean ToBoolean(object obj)
 {
     if (ConvertObject.ToString(obj) == "1")
     {
         return(true);
     }
     if (ConvertObject.ToString(obj) == "0")
     {
         return(false);
     }
     return(ToBoolean(obj, false));
 }
Exemplo n.º 5
0
 /// <summary>
 /// 获取月份的第一天
 /// </summary>
 /// <param name="month">yyyyMM</param>
 /// <returns></returns>
 public static DateTime GetMonthFirstday(string month)
 {
     try
     {
         if (month.Length == 6)
         {
             int yearIndex  = ConvertObject.ToInt32(month.Substring(0, 4));
             int monthIndex = ConvertObject.ToInt32(month.Substring(4, 2));
             return(new DateTime(yearIndex, monthIndex, 1));
         }
         else
         {
             throw new Exception();
         }
     }
     catch
     {
         return(GetMonthFirstday());
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// 获取后一个月
 /// </summary>
 /// <param name="month">当前月</param>
 /// <param name="nexts">后面的月数(1为后一个月,2为后两个月,-1为前一个月)</param>
 /// <returns></returns>
 public static string GetNextMonth(string month, int nexts)
 {
     try
     {
         if (month.Length == 6)
         {
             int yearIndex  = ConvertObject.ToInt32(month.Substring(0, 4));
             int monthIndex = ConvertObject.ToInt32(month.Substring(4, 2));
             return(GetMonthString(new DateTime(yearIndex, monthIndex, 1).AddMonths(nexts)));
         }
         else
         {
             throw new Exception();
         }
     }
     catch
     {
         DateTime today = DateTime.Now;
         return(GetMonthString(new DateTime(today.Year, today.Month, 1)));
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// 获取当前季度的月份
        /// </summary>
        /// <param name="yearQuarter"></param>
        /// <returns></returns>
        public static string[] GetQuarterMonths(string yearQuarter)
        {
            int yearIndex    = ConvertObject.ToInt32(yearQuarter.Substring(0, 4));
            int quarterIndex = ConvertObject.ToInt32(yearQuarter.Substring(5, 1));

            switch (quarterIndex)
            {
            case 1:
                return(new String[] {
                    yearIndex + "01",
                    yearIndex + "02",
                    yearIndex + "03"
                });

            case 2:
                return(new String[] {
                    yearIndex + "04",
                    yearIndex + "05",
                    yearIndex + "06"
                });

            case 3:
                return(new String[] {
                    yearIndex + "07",
                    yearIndex + "08",
                    yearIndex + "09"
                });

            case 4:
                return(new String[] {
                    yearIndex + "10",
                    yearIndex + "11",
                    yearIndex + "12"
                });
            }
            return(new String[] { });
        }
Exemplo n.º 8
0
 public static int GetDictionaryIntValue(Dictionary <string, object> json, string key, int defaultValue)
 {
     return(ConvertObject.ToInt32(GetDictionaryStringValue(json, key), defaultValue));
 }
Exemplo n.º 9
0
        /// <summary>
        /// 通过Stream获取Datatable
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileType">Excel文件类型(xls或xlsx)</param>
        /// <param name="sheetName">excel工作薄sheet的名称</param>
        /// <param name="isFirstRowColumn">第一行是否是DataTable的列名</param>
        /// <returns></returns>
        public DataTable ConvertStreamToDataTable(Stream stream, string fileType, string sheetName, bool isFirstRowColumn)
        {
            IWorkbook workbook = null;
            ISheet    sheet    = null;
            DataTable data     = new DataTable();
            int       startRow = 0;

            if (fileType == "xlsx")
            {
                workbook = new XSSFWorkbook(stream);//2007版本
            }
            else if (fileType == "xls")
            {
                try
                {
                    workbook = new HSSFWorkbook(stream);//2003版本
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("Office 2007"))
                    {
                        throw new Exception("xlsx");
                    }
                    else
                    {
                        throw ex;
                    }
                }
            }
            else
            {
                throw new Exception("Excel文件类型不正确");
            }

            if (sheetName != "")
            {
                sheet = workbook.GetSheet(sheetName);
                if (sheet == null)
                {
                    //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
                    sheet = workbook.GetSheetAt(0);
                }
            }
            else
            {
                sheet = workbook.GetSheetAt(0);
            }

            if (sheet != null)
            {
                IRow firstRow  = sheet.GetRow(0);
                int  cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数

                if (isFirstRowColumn)
                {
                    for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                    {
                        ICell cell = firstRow.GetCell(i);
                        if (cell != null)
                        {
                            string cellValue = ConvertObject.ToString(cell.StringCellValue);
                            if (cellValue != null)
                            {
                                DataColumn column = new DataColumn(cellValue);
                                data.Columns.Add(column);
                            }
                        }
                    }
                    startRow = sheet.FirstRowNum + 1;
                }
                else
                {
                    startRow = sheet.FirstRowNum;
                }

                //最后一列的标号
                int rowCount = sheet.LastRowNum;
                for (int i = startRow; i <= rowCount; ++i)
                {
                    IRow row = sheet.GetRow(i);
                    if (row == null)
                    {
                        continue;              //没有数据的行默认是null       
                    }
                    DataRow dataRow = data.NewRow();
                    for (int j = row.FirstCellNum; j < cellCount; ++j)
                    {
                        if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
                        {
                            dataRow[j] = row.GetCell(j).ToString();
                            if (row.GetCell(j).CellType == CellType.Numeric && DateUtil.IsCellDateFormatted(row.GetCell(j)))//如果类型是日期形式的,转成合理的日期字符串
                            {
                                dataRow[j] = row.GetCell(j).DateCellValue.ToString();
                            }
                        }
                    }
                    data.Rows.Add(dataRow);
                }
            }

            return(data);
        }
Exemplo n.º 10
0
        /// <summary>
        /// 转换图片格式
        /// </summary>
        /// <param name="originalStream">源图片流</param>
        /// <param name="formatWidth">格式化宽度</param>
        /// <param name="formatHeight">格式化高度</param>
        /// <param name="cut">是否裁剪,true:裁剪、false:不裁剪</param>
        /// <param name="newFormat">图片输出格式</param>
        /// <returns>新图片流</returns>
        public static Stream FormatPicture(Stream originalStream, int formatWidth, int formatHeight, bool cut, PictureFormat newFormat)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromStream(originalStream);

            if (formatWidth > 0 && formatHeight == 0)
            {
                formatHeight = ConvertObject.ToInt32(Math.Round(formatWidth * (float)originalImage.Height / originalImage.Width));
            }
            if (formatHeight > 0 && formatWidth == 0)
            {
                formatWidth = ConvertObject.ToInt32(Math.Round(formatHeight * (float)originalImage.Width / originalImage.Height));
            }

            int towidth  = formatWidth;
            int toheight = formatHeight;

            int x  = 0;
            int y  = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;

            if (cut)
            {
                //指定高宽裁减(不变形)
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y  = 0;
                    x  = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * formatHeight / towidth;
                    x  = 0;
                    y  = (originalImage.Height - oh) / 2;
                }
            }
            else
            {
                //默认指定高度宽度,失真缩放

                //指定宽度缩放
                if (formatHeight <= 0)
                {
                    formatWidth = originalImage.Width * formatHeight / originalImage.Height;
                }

                //指定高度缩放
                else if (formatWidth <= 0)
                {
                    formatHeight = originalImage.Height * formatWidth / originalImage.Width;
                }
            }

            //新建一个bmp图片
            System.Drawing.Image image = new System.Drawing.Bitmap(formatWidth, formatHeight);
            //新建一个画板
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image);
            //设置高质量插值法
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            graphics.Clear(System.Drawing.Color.Transparent);
            //在指定位置并且按指定大小绘制原图片的指定部分
            graphics.DrawImage(
                originalImage,
                new System.Drawing.Rectangle(0, 0, formatWidth, formatHeight),
                new System.Drawing.Rectangle(x, y, ow, oh),
                System.Drawing.GraphicsUnit.Pixel
                );
            originalImage.Dispose();

            System.Drawing.Imaging.ImageFormat imageFormat = GetImageFormat(newFormat);

            MemoryStream newStream = new MemoryStream();

            try
            {
                //以指定格式保存图片
                string outputFile = Path.GetTempPath() + Guid.NewGuid().ToString();// +".jpg";
                image.Save(outputFile, imageFormat);
                FileStream fs = File.Open(outputFile, FileMode.Open);
                newStream = new MemoryStream(ConvertStream.ToBuffer(fs));
                fs.Close();
                File.Delete(outputFile);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                originalImage.Dispose();
            }

            return(newStream);
        }
Exemplo n.º 11
0
        /// <summary>
        /// 格式化图片
        /// </summary>
        /// <param name="fileBinary">源图片二进制</param>
        /// <param name="formatWidth">格式化宽度</param>
        /// <param name="formatHeight">格式化高度</param>
        /// <param name="cut">是否裁剪,true:裁剪、false:不裁剪</param>
        /// <param name="picFormat">图片输出格式</param>
        /// <returns>新图片二进制</returns>
        public static Byte[] FormatPicture(Byte[] fileBinary, int formatWidth, int formatHeight, bool cut, PictureFormat picFormat)
        {
            System.Drawing.Image originalImage = System.Drawing.Image.FromStream(new MemoryStream(fileBinary));

            if (formatWidth > 0 && formatHeight == 0)
            {
                formatHeight = ConvertObject.ToInt32(Math.Round(formatWidth * (float)originalImage.Height / originalImage.Width));
            }
            if (formatHeight > 0 && formatWidth == 0)
            {
                formatWidth = ConvertObject.ToInt32(Math.Round(formatHeight * (float)originalImage.Width / originalImage.Height));
            }

            int towidth  = formatWidth;
            int toheight = formatHeight;

            int x  = 0;
            int y  = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;



            if (cut)
            {
                //指定高宽裁减(不变形)
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y  = 0;
                    x  = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * formatHeight / towidth;
                    x  = 0;
                    y  = (originalImage.Height - oh) / 2;
                }
            }
            else
            {
                //默认指定高度宽度,失真缩放

                //指定宽度缩放
                if (formatHeight <= 0)
                {
                    formatWidth = originalImage.Width * formatHeight / originalImage.Height;
                }

                //指定高度缩放
                else if (formatWidth <= 0)
                {
                    formatHeight = originalImage.Height * formatWidth / originalImage.Width;
                }
            }

            //新建一个bmp图片
            System.Drawing.Image image = new System.Drawing.Bitmap(formatWidth, formatHeight);
            //新建一个画板
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(image);
            //设置高质量插值法
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            graphics.Clear(System.Drawing.Color.Transparent);
            //在指定位置并且按指定大小绘制原图片的指定部分
            graphics.DrawImage(
                originalImage,
                new System.Drawing.Rectangle(0, 0, formatWidth, formatHeight),
                new System.Drawing.Rectangle(x, y, ow, oh),
                System.Drawing.GraphicsUnit.Pixel
                );
            originalImage.Dispose();

            System.Drawing.Imaging.ImageFormat imageFormat = GetImageFormat(picFormat);

            //以指定格式保存图片
            try
            {
                MemoryStream stream = new MemoryStream();
                image.Save(stream, imageFormat);
                return(stream.GetBuffer());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //originalImage.Dispose();
                image.Dispose();
                graphics.Dispose();
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// 将 DateTimeString 转换为 ""yyyyMM" 格式 String
        /// </summary>
        /// <param name="dateString"></param>
        /// <returns></returns>
        public static String GetMonthString(string dateString)
        {
            DateTime date = ConvertObject.ToDateTime(dateString);

            return(GetMonthString(date));
        }
Exemplo n.º 13
0
        /// <summary>
        /// 将 DateTimeString 转换为指定格式 String
        /// </summary>
        /// <param name="dateString"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static String ToString(string dateString, string format)
        {
            DateTime date = ConvertObject.ToDateTime(dateString);

            return(ToString(date, format));
        }
Exemplo n.º 14
0
        /// <summary>
        /// 将 DateTimeString 转换为 ""yyyy" 格式 String
        /// </summary>
        /// <param name="dateString"></param>
        /// <returns></returns>
        public static String ToYearString(string dateString)
        {
            DateTime date = ConvertObject.ToDateTime(dateString);

            return(ToYearString(date));
        }
Exemplo n.º 15
0
 public static decimal GetDictionaryDecimalValue(Dictionary <string, object> json, string key, decimal defaultValue)
 {
     return(ConvertObject.ToDecimal(GetDictionaryStringValue(json, key), defaultValue));
 }
Exemplo n.º 16
0
 public static bool GetDictionaryBoolValue(Dictionary <string, object> json, string key, bool defaultValue)
 {
     return(ConvertObject.ToBoolean(GetDictionaryStringValue(json, key), defaultValue));
 }
Exemplo n.º 17
0
        /// <summary>
        /// 获取 DateTimeString 的季度
        /// </summary>
        /// <param name="dateString"></param>
        /// <returns></returns>
        public static int ToQuarterNumber(string dateString)
        {
            DateTime date = ConvertObject.ToDateTime(dateString);

            return(ToQuarterNumber(date));
        }