如何将字节数据转换为excel格式 [英] how to convert byte data into excel format

查看:263
本文介绍了如何将字节数据转换为excel格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



我将文件(word,excel,pdf)文件存储到sqlserver中,在下载文件时我们使用下面的代码: -





I m storing files(word,excel,pdf) files into sqlserver, and while downloading file we are using below code:-

for (int i = 0; i < file.Tables[1].Rows.Count; i++)
               {
                   Byte[] data = (Byte[])file.Tables[1].Rows[i]["FileBlob"];
                   string s = System.Text.UTF8Encoding.UTF8.GetString(data);
                   Response.Write(s);
                   byte[] bytearray = (data);
                   System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                   //Response.Write( encoding.GetString(bytearray)) ;

                   string filename = file.Tables[1].Rows[i]["FileName"].ToString();
                   test.Text = filename + "<\br>";
                   string path = dir + "/" + filename;
                   var file1 = new FileInfo(Path.Combine(dir.FullName, filename));
                   if (!file1.Exists) // you may not want to overwrite existing files
                   {
                       using (Stream stream = file1.OpenWrite())
                       using (StreamWriter writer = new StreamWriter(stream))
                       {
                         writer.Write(encoding.GetString(bytearray);
                          writer.Flush();

                       }
                   }
                   zip.AddFile(path, "Files");
               }
           }







在将字节数据写入excel文件时,我们收到文件损坏。当我们使用下面的代码时: -

writer。写(encoding.GetString(bytearray);



以上行正在为文本文件工作,能够在文本文件中显示数据,问题是word和excel以及pdf文件。




while writing byte data to excel file we are getting file corrupted. when we use below code:-
writer.Write(encoding.GetString(bytearray);

above line is working for text file and able to display data in text file the problem is with word and excel and pdf file.

推荐答案

writer.Write(encoding.GetString(bytearray);



为什么要将文件内容从其本机格式转换为字符串?这将破坏Excel数据。


Why are you converting the file content from its native form into a string? That will destroy the Excel data.


这篇关于如何将字节数据转换为excel格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆