1

嗨,我需要读取存储在 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();
            }
4

2 回答 2

2

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

如果您不想将文件保留在磁盘上,这样的事情应该可以工作:

var client = new WebClient();

String url = "http://...";
var fullPath = Path.GetTempFileName();
client.DownloadFile(url, fullPath);
于 2012-07-24T05:53:39.173 回答
0

读取 excel 文件 - http://exceldatareader.codeplex.com/

于 2012-07-24T06:03:30.213 回答