从URL下载Excel并打开它以供阅读?

时间:2012-07-24 05:47:49

标签: c# asp.net

嗨我需要读取存储在s3服务器中的excel文件,我试过但它显示无效的网址。请帮我解决

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();
            }

2 个答案:

答案 0 :(得分:2)

您无法通过http直接打开文件。您必须下载它然后从磁盘打开它。您下载它的最简单方法可能是使用WebClient。

如果您不想将文件保留在磁盘上,那么这样的事情应该有效:

var client = new WebClient();

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

答案 1 :(得分:0)