从URL下载一个Excel并打开阅读? [英] Download an Excel from a URL and open it for reading?

查看:250
本文介绍了从URL下载一个Excel并打开阅读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要读取Excel存储在S3服务器上的文件,我想却是露出无效的互联网地址。请帮我解决。

 字符串FULLPATH =htt​​p://tempfordevelopment.s3.amazonaws.com/ad53498f-74e8-412c-8c8f-e36b18f16e4eEmailExcel2.xlsx;            //乌里URI =新的URI(完整路径);
            //字符串excelLocation = uri.LocalPath;            字符串sConnectionString =供应商= Microsoft.ACE.OLEDB.12.0;数据源=+完整路径+;扩展属性='Excel的8.0; HDR =是';
            System.Data.DataSet中DtSet;
            System.Data.OleDb.OleDbDataAdapter MyCommand;
            OleDbConnection的objConn =新的OleDbConnection(sConnectionString);            MyCommand =新System.Data.OleDb.OleDbDataAdapter(SELECT * FROM [工作表Sheet1 $],sConnectionString);
            MyCommand.TableMappings.Add(表,Net-informations.com);
            DtSet =新System.Data.DataSet中();
            MyCommand.Fill(DtSet); /////在这里,我得到的错误为无效的Internet地址
            数据表表= DtSet.Tables [0];            字符串[]字符串=新的字符串[table.Rows.Count]
            INT IDX = 0;
            的foreach(在table.Rows的DataRow行)
            {
                StringBuilder的SB =新的StringBuilder();
                [对象]单元= row.ItemArray;
                的for(int i = 0; I< cells.Length;我++)
                {
                    (!I = 0)如sb.Append(,);
                    sb.Append(细胞[I]);
                }
                串[IDX ++] = sb.ToString();
            }


解决方案

您可以通过HTTP不是直接打开的文件。你必须下载它,然后从磁盘打开它。最简单的方法供你下载它很可能将与Web客户端。

这样的事情应该工作,如果你不想保存在磁盘上的文件:

  VAR的客户=新的WebClient();字符串URL =HTTP:// ...;
变种FULLPATH = Path.GetTempFileName();
client.DownloadFile(URL,完整路径);

hi i need to read excel file which is stored in s3 server,i tried but it is showing invalid internet address.please help me to solve

string fullPath = "http://tempfordevelopment.s3.amazonaws.com/ad53498f-74e8-412c-8c8f-e36b18f16e4eEmailExcel2.xlsx";

            //Uri uri = new Uri(fullPath);
            //string excelLocation = uri.LocalPath;

            String sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + fullPath + "; Extended Properties='Excel 8.0;HDR=Yes'";
            System.Data.DataSet DtSet;
            System.Data.OleDb.OleDbDataAdapter MyCommand;
            OleDbConnection objConn = new OleDbConnection(sConnectionString);

            MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", sConnectionString);
            MyCommand.TableMappings.Add("Table", "Net-informations.com");
            DtSet = new System.Data.DataSet();
            MyCommand.Fill(DtSet);/////Here i am getting error as invalid internet address


            DataTable table = DtSet.Tables[0];

            string[] strings = new string[table.Rows.Count];
            int idx = 0;
            foreach (DataRow row in table.Rows)
            {
                StringBuilder sb = new StringBuilder();
                object[] cells = row.ItemArray;
                for (int i = 0; i < cells.Length; i++)
                {
                    if (i != 0) sb.Append(',');
                    sb.Append(cells[i]);
                }
                strings[idx++] = sb.ToString();
            }

解决方案

You can't directly open files over the http. You'll have to download it and then open it from disk. The easiest way for you to download it is probably going to be with WebClient.

Something like this should work if you don't want to keep the file on disk:

var client = new WebClient();

String url = "http://...";
var fullPath = Path.GetTempFileName();
client.DownloadFile(url, fullPath);

这篇关于从URL下载一个Excel并打开阅读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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