开发者

Downloading and saving excel file

开发者 https://www.devze.com 2023-02-24 06:39 出处:网络
i am using: `private void get_stocks_data() { byte[] result; byte[] buffer = new byte[4096]; WebRequest wr = WebRequest.Create(\"http://www.tase.co.il/TASE/Pages/ExcelExport.aspx?sn=he-IL_ds&enum

i am using: `private void get_stocks_data() { byte[] result; byte[] buffer = new byte[4096];

        WebRequest wr = WebRequest.Create("http://www.tase.co.il/TASE/Pages/ExcelExport.aspx?sn=he-IL_ds&enumTblType=AllSecurities&Columns=he-IL_Columns&Titles=he-IL_Titles&TblId=0&ExportType=1");

        using (WebResponse response = wr.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    int count = 0;
                    do
                    {
                        count = responseStream.Read(buffer, 0, buffer.Length);
                        memoryStream.Write(buffer, 0, count);
                    } while (count != 0);
                    result = memoryStream.ToArray();
                    write_data_to_excel(result);

                }
            }
        }`

to download the excel file,

And this method to fill the file on my computer:

private void write_data_to_excel(byte[] input)
    {
        StreamWriter str = new StreamWriter("stockdata.xls");

        for (int i = 0; input.Length > i; i++)
        {
            str.WriteLine(input[i].ToString());
        }
        str.Close();
    }

The result is that i get a lot of numbers... What a开发者_开发知识库m i doing wrong? the file i am downloadin is excel version 2003, on my computer i have 2007... Thanks.


I would suggest that you use WebClient.DownloadFile() instead.

This is a higher level method that will abstract from creating the request manually, dealing with encoding, etc.


Problem is in your Write_data_to_excel function
as you are using StreamWriter.WriteLine method it needs string,
you are passing byte as string so your binary value say 10 will be now string 10
try FileStream f = File.OpenWrite("stockdata.xlsx");
f.Write(input,0,input.Length);
this will work.

0

精彩评论

暂无评论...
验证码 换一张
取 消