DataTableToCSV再转 byte[]再转MemoryStream(上传阿里云)

2019/02/25 14:06
阅读数 17

public static string ToCSV( DataTable table)
{
var result = new StringBuilder();
for (int i = 0; i < table.Columns.Count; i++)
{
if (table.Columns[i].ColumnName.ToUpper()=="ID")
{
result.Append("主键");
}
else
{
result.Append(table.Columns[i].ColumnName);
}

result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}

foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
result.Append(row[i].ToString() == "" ? "-" : row[i].ToString());
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}
}

return result.ToString();
}

public string FileSaveExcelAliyunOSS(DataTable dt, string fileName, string extension)
        {
            #region wps可用
            //StringBuilder sb = new StringBuilder();
            //for (int k = 0; k < dt.Columns.Count; k++)
            //{
            //    sb.Append(dt.Columns[k].ColumnName.ToString() + "\t");
            //}
            //sb.Append(Environment.NewLine);

            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    for (int j = 0; j < dt.Columns.Count; j++)
            //    {
            //        sb.Append(dt.Rows[i][j].ToString() + "\t");
            //    }
            //    sb.Append(Environment.NewLine);//每写一行数据后换行
            //}

            //byte[] array = Encoding.Default.GetBytes(sb.ToString());
            //MemoryStream stream = new MemoryStream(array);   
            #endregion
            byte[] array = Encoding.Default.GetBytes(ToCSV(dt));
            MemoryStream stream = new MemoryStream(array);  

            string FilePath = "";
            //上传到阿里云  
            using (stream)//转成Stream流  
            {
                string md5 = OssUtils.ComputeContentMd5(stream, stream.Length);
                string FileName = Convert.ToString(Guid.NewGuid()).Replace("-", "") + extension;//文件名=文件名+当前上传时间  
                FilePath = fileName + "/" + Convert.ToString(DateTime.Now.Year) + "/" + Convert.ToString(DateTime.Now.Month) + "/" + FileName;//云文件保存路径  

                try
                {
                    //初始化阿里云配置--外网Endpoint、访问ID、访问password  
                    OssClient aliyun = new OssClient(endpoint, accessKeyId, accessKeySecret);

                    //将文件md5值赋值给meat头信息,服务器验证文件MD5  
                    var objectMeta = new ObjectMetadata
                    {
                        ContentMd5 = md5,
                        ContentType = "application/octet-stream",
                    };
                    //文件上传--空间名、文件保存路径、文件流、meta头信息(文件md5) //返回meta头信息(文件md5)  
                    PutObjectResult por = aliyun.PutObject(BucketName, FilePath, stream, objectMeta);
                    string text = por.ETag;

                }
                catch (Exception e)
                {
                    ICE.Core.LogHelper.ErrorLog(log, string.Format("图片上传OSS出错了:{0}", Convert.ToString(e.Message)));
                    FilePath = "";
                }
            }
            return FilePath;
        }

  

展开阅读全文
ice
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部