WebClient

View Code
 1 //WebClient wc = new WebClient();
 2             //wc.DownloadFile("http://192.168.1.100/b.xls", @"c:\b.xls");
 3 
 4             //using(Stream stream = File.OpenRead(@"c:\b.xls"))
 5             //{
 6             //    HSSFWorkbook workbook = new HSSFWorkbook(stream);
 7             //    HSSFSheet sheet = workbook.GetSheetAt(0);
 8             //    HSSFRow row = sheet.GetRow(0);
 9             //    MessageBox.Show(row.GetCell(0).StringCellValue);
10             //}
11 
12             //WebClient wc = new WebClient();
13             //Stream stream = wc.OpenRead("http://192.168.1.100/b.xls");
14 
15             ////有的Stream子类不支持指针的后退、Seek
16             ////stream.Position = 2;
17             //using (stream)
18             //{
19             //    //HSSFWorkbook要求一个指针能随意移动的流
20             //    HSSFWorkbook workbook = new HSSFWorkbook(stream);
21             //    HSSFSheet sheet = workbook.GetSheetAt(0);
22             //    HSSFRow row = sheet.GetRow(0);
23             //    MessageBox.Show(row.GetCell(0).StringCellValue);
24             //}
25 
26             WebClient wc = new WebClient();
27             Stream stream = wc.OpenRead("http://192.168.1.100/b.xls");
28             //stream.po
29             //存放数据到内存流中
30             using (Stream memstream = new MemoryStream())
31             {
32                 //因为所有数据都拷贝到了memstream,所以考完了就可以Dispose
33                 using (stream)
34                 {
35                     //从stream读取到memstream中
36                     byte[] bytes = new byte[1024];
37                     int readBytes;
38                     //把网络流中的数据读到bytes数组中
39                     while ((readBytes = stream.Read(bytes, 0, bytes.Length)) > 0)
40                     {
41                         //把bytes数组中的数据写入到内存流中
42                         memstream.Write(bytes, 0, readBytes);
43                     }
44                 }
45                 HSSFWorkbook workbook = new HSSFWorkbook(memstream);
46                 HSSFSheet sheet = workbook.GetSheetAt(0);
47                 HSSFRow row = sheet.GetRow(0);
48                 MessageBox.Show(row.GetCell(0).StringCellValue);
49             }
50             
51         }

 

posted @ 2013-03-16 16:33  Big.Eagle  阅读(261)  评论(0)    收藏  举报