c#各种数据转换

1.时间戳转换成时间格式

private DateTime GetDateTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + “0000”);
TimeSpan toNow = new TimeSpan(lTime);
DateTime targetDt = dtStart.Add(toNow);
return dtStart.Add(toNow);
}

2.将字符串计算公式执行

var a = new System.Data.DataTable().Compute(“123”, “”);

3.MD5标准32位小写加密

private static string GetMD5(string myString)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            //byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
            byte[] fromData = System.Text.Encoding.UTF8.GetBytes(myString);//
            byte[] targetData = md5.ComputeHash(fromData);
            string byte2String = null;

            for (int i = 0; i < targetData.Length; i++)
            {
                //这个是很常见的错误,你字节转换成字符串的时候要保证是2位宽度啊,某个字节为0转换成字符串的时候必须是00的,否则就会丢失位数啊。不仅是0,1~9也一样。
                //byte2String += targetData[i].ToString("x");//这个会丢失
                byte2String = byte2String + targetData[i].ToString("x2");
            }

            return byte2String;
        }

4.原生word转pdf方法

 public static bool WordToPDF(string fullName,string sourcePath)
        {
            bool result = false;
            object objMissing = System.Reflection.Missing.Value;
            object saveChanges = false;
            Microsoft.Office.Interop.Word.Application WordApp = null;
            if (WordApp == null)//一定要判断wordApp 是否为空
            {
                WordApp = new Microsoft.Office.Interop.Word.Application()
                {
                    DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone
                };
            }
            Microsoft.Office.Interop.Word.Document document = null;
            try
            {
                WordApp.Visible = false;
                document = WordApp.Documents.Open(fullName, objMissing, objMissing, objMissing, objMissing, objMissing,
                    objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
                document.SaveAs2(sourcePath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing,
                    objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
                //document.ExportAsFixedFormat(sourcePath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
                result = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
                document.Close();
                object IsSave = true;
                WordApp.Quit(ref IsSave, ref objMissing, ref objMissing);//退出程序,相当于关闭word   

                System.Runtime.InteropServices.Marshal.ReleaseComObject(WordApp); //释放内存

                WordApp = null;
            }
            return result;
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值