将byte []写入.xls文件时写入的信息是字节

时间:2015-11-27 15:09:19

标签: c# php excel wcf post

我有一个WCF服务,它在一个帖子中从一个PHP页面接收一个byte []。

if (isset($_FILES['uploadFile'])) {
            $name = $_FILES['uploadFile']['name'];

            $byteArr = file_get_contents($_FILES['uploadFile']['tmp_name']);

            $fuelExt = substr($name, strrpos($name, '.') + 1);

            $tmpName = substr($_FILES['uploadFile']['tmp_name'],strripos($_FILES['uploadFile']['tmp_name'],'/') + 1);
            $fileSize = $_FILES['uploadFile']['size'];
            $fileType = $_FILES['uploadFile']['type'];


            /* TODO enviar por post */
            $url = 'http://10.0.0.121/ExcelParser/ExcelParser.svc/POST/'.$hf;
            $data = array('data' => $byteArr);

            // use key 'http' even if you send the request to https://...
            $options = array(
                'http' => array(
                    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                    'method'  => 'POST',
                    'content' => $byteArr,
                ),
            );
            $context  = stream_context_create($options);
            $result = file_get_contents($url, false, $context);

这是WCF代码:

public string GetPost(Stream postData, string type)
    {
        log4net.Config.XmlConfigurator.Configure();
        try
        {
            var post = new StreamReader(postData).ReadToEnd();

            //var array = data.Substring(data.IndexOf("data"))

            Log.Debug("Postdata : " + post);
            Log.Debug("TYPE : " + type);

            var ascii = Encoding.ASCII.GetBytes(post);
            var utf8 = Encoding.UTF8.GetBytes(post);

            Log.Debug("size ASCII-->>>  : " + ascii.Length);
            Log.Debug("size utf8-->>>  : " + utf8.Length);

            var isSuccess = false;


            const string strFileFullPath = @"C:\temp\temp.xls";
            const string strFileFullPathASCII = @"C:\temp\ASCII.xls";
            const string strFileFullPathUTF8 = @"C:\temp\UTF.xls";


            var writer = new BinaryWriter(File.OpenWrite(strFileFullPathASCII));

            writer.Write(ascii);
            writer.Flush();
            writer.Close();

            writer = new BinaryWriter(File.OpenWrite(strFileFullPathUTF8));

            writer.Write(utf8);
            writer.Flush();
            writer.Close();


            //File.WriteAllBytes(strFileFullPathASCII, ascii);
            //File.WriteAllBytes(strFileFullPathUTF8, utf8);


            //var fileStream = new FileStream(strFileFullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            //// write file stream into the specified file  
            //using (var fs = fileStream)
            //{
            //    //var xls = Encoding.ASCII.GetBytes(Encoding.ASCII.GetString(pFileBytes));
            //    //Log.Debug("XLS -->>>  : " + xls);
            //    //Log.Debug("ENCODING -->>>  : " + Encoding.ASCII.GetString(pFileBytes));
            //    //fs.Write(xls, 0, pFileBytes.Length);
            //    fs.Write(utf8, 0, utf8.Length);
            //    isSuccess = true;
            //}


            //Log.Debug("JSON DATA : " + JsonConvert.DeserializeObject(data));

            var book = new LinqToExcel.ExcelQueryFactory(strFileFullPath);
            Log.Debug("BOOK  : " + book);


            var sheetName = book.GetWorksheetNames();

            Log.Debug("sheetname  : " + sheetName);

            var query =
                from row in book.Worksheet(sheetName.First())
                let import = new FuelImport
                {
                    Client = row[PrioCentroHeaders.Client].Cast<string>(),
                    Supplier = "",
                    DateTime = row[PrioCentroHeaders.Date].Cast<DateTime>().ToString("yyyy-MM-dd HH:mm:ss"),
                    LicencePlate = row[PrioCentroHeaders.LicencePlate].Cast<string>(),
                    Card = row[PrioCentroHeaders.Card].Cast<string>(),
                    PurchaseNumber = row[PrioCentroHeaders.PurchaseNumber].Cast<int>(),
                    Product = row[PrioCentroHeaders.Product].Cast<string>(),
                    UnPrice = row[PrioCentroHeaders.UnitPrice].Cast<double>(),
                    Quantity = row[PrioCentroHeaders.Quantity].Cast<double>(),
                    Km = row[PrioCentroHeaders.Km].Cast<double>()
                }
                select import;

            var ret = query.ToList();

            //removes the blank rows
            ret = ret.Except(ret.Where(w => w.Client == null)).ToList();
            var json = JsonConvert.SerializeObject(ret);
            return json;

        }
        //return "Sucess POST postdata : " + data;
        catch (Exception ex)
        {
            Log.Fatal("erros : " + ex.Message);
            throw;
        }
    }

我尝试了几种编码,ASCII和UTF-8以及几种创建文件和编写字节数组的方法,但我得到的只是电子表格中的字节数组:

????>??     .????????%&'()*+,-????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????  =??????\pJonas                                                                                                          B??!Relat?rio Mapa de Vendas Por Ca4_FiltrarBaseDados;??)==,8X@?"??1??          
MS Sans Serif1??Arial1??Arial1??Arial1??Arial1??Arial1??Arial1??Arial1??Arial1??Arial1??Arial1??                        
MS Sans Serif1??Arial1??Calibri1?   ?Calibri1,9?Calibri19?Calibri1?9?Calibri1?                  
?Calibri1?                      
?Calibri1??Calibri1?>?Calibri1??Calibri1??Calibri1???Calibri1?                      
?Calibri1??Calibri1h9?Cambria1??Calibri1?   ?Calibri#,##0\ "?";\-#,##0\ "?"#,##0\ "?";[Red]\-#,##0\ "?" #,##0.00\ "?";\-#,##0.00\ "?"%"#,##0.00\ "?";[Red]\-#,##0.00\ "?"9*6_-* #,##0\ "?"_-;\-* #,##0\ "?"_-;_-* "-"\ "?"_-;_-@_-6)3_-* #,##0\ _?_-;\-* #,##0\ _?_-;_-* "-"\ _?_-;_-@_-A,>_-* #,##0.00\ "?"_-;\-* #,##0.00\ "?"_-;_-* "-"??\ "?"_-;_-@_->+;_-* #,##0.00\ _?_-;\-* #,##0.00\ _?_-;_-* "-"??\ _?_-;_-@_-?dd\-mm\-yyyy?dd\-mm\-yyyy"  "hh":"mm":"ss?#,##0.000_);\-#,##0.000?#,##0.00_);\-#,##0.00??? ? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? ??? ?? A.I.???? ?? ????+?? ?? ?)?? ?? ?,?? ?? ?*?? ?? ??? ?? ??? ?? ??? ?? @c??? ?? @Y??? ?? ?X??? ?? ??? ?? A.I.???? ?? ???? ?? ??? ?? A,I,??? ?? A~I~??    ?? ?? ??? ?? ??? ?? ?cb??? ?? ?? ?? ?? ?? ?? ??? ?  ? ?         
?? ??? ?? ??? ?? ??? ??? ?                      
? ?                     
? ?                     
?? ?                        
? ?                     
?? ?                        
?? ?                        
20% - Accent1?M??                       
20% - Accent1ef?????%?                      
20% - Accent2?M?"?                      
20% - Accent2ef?????%?                      
20% - Accent3?M?&?                      
20% - Accent3ef?????%?                      
20% - Accent4?A?*?                      
20% - Accent4?%?                        
20% - Accent5?M?.?                      
20% - Accent5ef?????%?                      
20% - Accent6?M?2?                      
20% - Accent6   ef?????%?                   
40% - Accent1?M??                       
40% - Accent1?L?????%?                      
40% - Accent2?M?#?              

0 个答案:

没有答案