C#中下载Excel文件(其实就是将Excel文件从一个路径Copy到另一个路径)
C#中下载Excel文件(其实就是将Excel文件从一个路径Copy到另一个路径)
public void download(string TempName)
{
try
{
string StrPath ;
StrPath=Application.StartupPath+@"\template\"+TempName+@".xls";
Excel.Application xlApp=new Excel.Application();
Excel.Worksheet xlSheet;
object missing=System.Reflection.Missing.Value;
Excel.Workbook xlBook = xlApp.Workbooks.Open(StrPath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
xlSheet =(Excel.Worksheet)xlBook.Worksheets[1];
FolderBrowserDialog Foler=new FolderBrowserDialog();
string FileName="";
Foler.Description="选择保存模板的目录";
if(Foler.ShowDialog()==DialogResult.OK)
{
if(Foler.SelectedPath!="")
if(System.IO.File.Exists(StrPath))
{
FileName=Foler.SelectedPath;
if(FileName.LastIndexOf(@"\")==(FileName.Length-1))
FileName=Foler.SelectedPath+TempName+@".xls";
else
FileName=Foler.SelectedPath+@"\"+TempName+@".xls";
if(System.IO.File.Exists(FileName))
{
MessageBox.Show(Foler.SelectedPath+@" 已经存在文件"+TempName+@".xls","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
xlSheet.SaveAs(FileName,missing, missing, missing, missing, missing, missing, missing, missing);
}
xlApp.Quit();
if(FileName=="")
return;
Excel.Application NewApp=new Excel.Application();
Excel.Workbook NewBook;
Excel.Worksheet NewSheet;
NewBook = NewApp.Workbooks.Open(FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
NewSheet = (Excel.Worksheet)NewBook.Worksheets[1];
NewApp.Visible = true;
MessageBox.Show("下载成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
xlApp.Quit();
MessageBox.Show("没有成功下载模板","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
catch(Exception err)
{
MessageBox.Show(err.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
{
try
{
string StrPath ;
StrPath=Application.StartupPath+@"\template\"+TempName+@".xls";
Excel.Application xlApp=new Excel.Application();
Excel.Worksheet xlSheet;
object missing=System.Reflection.Missing.Value;
Excel.Workbook xlBook = xlApp.Workbooks.Open(StrPath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
xlSheet =(Excel.Worksheet)xlBook.Worksheets[1];
FolderBrowserDialog Foler=new FolderBrowserDialog();
string FileName="";
Foler.Description="选择保存模板的目录";
if(Foler.ShowDialog()==DialogResult.OK)
{
if(Foler.SelectedPath!="")
if(System.IO.File.Exists(StrPath))
{
FileName=Foler.SelectedPath;
if(FileName.LastIndexOf(@"\")==(FileName.Length-1))
FileName=Foler.SelectedPath+TempName+@".xls";
else
FileName=Foler.SelectedPath+@"\"+TempName+@".xls";
if(System.IO.File.Exists(FileName))
{
MessageBox.Show(Foler.SelectedPath+@" 已经存在文件"+TempName+@".xls","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
xlSheet.SaveAs(FileName,missing, missing, missing, missing, missing, missing, missing, missing);
}
xlApp.Quit();
if(FileName=="")
return;
Excel.Application NewApp=new Excel.Application();
Excel.Workbook NewBook;
Excel.Worksheet NewSheet;
NewBook = NewApp.Workbooks.Open(FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
NewSheet = (Excel.Worksheet)NewBook.Worksheets[1];
NewApp.Visible = true;
MessageBox.Show("下载成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
xlApp.Quit();
MessageBox.Show("没有成功下载模板","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
catch(Exception err)
{
MessageBox.Show(err.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}