- private void btnDownLoad_Click(object sender, EventArgs e)
- {
- DownLoad();
- }
- private void DownLoad()
- {
- string FilePath = System.Windows.Forms.Application.StartupPath + "\\" + "ProductLineCategoryImport.xlsx";//文件路径
- string FileName = Path.GetFileName(FilePath);
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Title = "下载文件";
- saveFileDialog.Filter = "xlsx文件(*.xlsx)|*.xlsx";
-
- saveFileDialog.FileName = FileName;
- DialogResult dialogResult = saveFileDialog.ShowDialog(this);
- if (dialogResult == DialogResult.OK)
- {
- System.Net.WebClient client = new System.Net.WebClient();
- byte[] data = client.DownloadData(FilePath);//一个真正存放数据的地址,一般我们将连接存在数据库中,数据存放在数据服务器上
- FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create);
- fs.Write(data, 0, data.Length);
- fs.Close();
- MessageBox.Show("下载成功!");
- }
- }
注: EXCEL模板文件,放在Debug文件夹的根目录里,和编译后的 XXXXXX.exe文件放一起