private void down()
{
//本地文件夹
string folder = @"C:\Commodity Search";
string contact_list = @"http://intranet.hp.com/cass/procurementcentral/ApprovedSupplierCompliance/Documents/Contact%20List.xlsx";
string print = @"http://intranet.hp.com/cass/procurementcentral/ApprovedSupplierCompliance/Documents/Print%20Commodity%20Code%20Contact%20List.xlsx";
string Travel = @"http://intranet.hp.com/cass/procurementcentral/ApprovedSupplierCompliance/Documents/Travel%20Contact%20List.xlsx";
string CommodityMasterDataWorksheet = @"http://teams1.sharepoint.hp.com/teams/ASLLopez/Shared%20Documents/Master%20Data/CommodityMasterDataWorksheet.xls";
string Approved_Supplier_Library = @"http://intranet.hp.com/cass/procurementcentral/ApprovedSupplierCompliance/Documents/Approved%20Supplier%20Library.xlsm";
this.tsStatus.Text = "Downloading...";
this.tsStatusNum.Text = "1/5";
DownExcel(contact_list, folder + @"\Contact List.xlsx");
this.tsStatusNum.Text = "2/5";
DownExcel(print, folder + @"\Print Commodity Code Contact List.xlsx");
this.tsStatusNum.Text = "3/5";
DownExcel(Travel, folder + @"\Travel Contact List.xlsx");
this.tsStatusNum.Text = "4/5";
DownExcel(CommodityMasterDataWorksheet, folder + @"\CommodityMasterDataWorksheet.xls");
this.tsStatusNum.Text = "5/5";
DownExcel(Approved_Supplier_Library, folder + @"\Approved Supplier Library.xlsm");
this.tsStatus.Text = "Completed";
this.tsStatusNum.Text = "";
MessageBox.Show("文件下载完成!");
}
public void DownExcel(string url, string filepath)
{
try
{
//string localFilePath = "http://enhanced1.sharepoint.hp.com/teams/Quote/NBQ/Shared%20Documents/TCO_Comment_List.xls";
string localFilePath = url;
// string downloadpath = Application.StartupPath + @"\TEMP\comment.xls";
string downloadpath = filepath;
//DirectoryInfo TheFolder = new DirectoryInfo(System.Windows.Forms.Application.StartupPath + @"\TEMP\");
DirectoryInfo TheFolder = new DirectoryInfo(@"C:\Commodity Search");
if (TheFolder.Exists)
{
if (File.Exists(downloadpath))
{
File.Delete(downloadpath);
}
}
else
{
TheFolder.Create();
}
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(localFilePath);
Myrq.Credentials = CredentialCache.DefaultCredentials;
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
System.IO.Stream st = myrp.GetResponseStream();
System.IO.Stream so = new System.IO.FileStream(downloadpath, System.IO.FileMode.Create);
byte[] by = new byte[1024];
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
so.Write(by, 0, osize);
osize = st.Read(by, 0, (int)by.Length);
}
so.Close();
st.Close();
myrp.Close();
Myrq.Abort();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
//MessageBox.Show("Download file failed! Err!" + ex.StackTrace);
}