void Cp()
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "请选择源文件 ";
openFileDialog1.Filter = "Excel文件(*.xls)|*.xls ";
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName.Equals(" "))
{
MessageBox.Show("请选择一个Excel文件! ");
}
else
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Workbook workbook = excel.Workbooks.Open(openFileDialog1.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.FileName = "未命名表.xls";
saveFileDialog.DefaultExt = ".xls";
saveFileDialog.FileOk += new CancelEventHandler(delegate(object sender, CancelEventArgs e)
{
Workbook workbook1 = excel.Workbooks.Open(saveFileDialog.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Worksheet worksheet = (Worksheet)workbook.Sheets[1];
worksheet.Copy(workbook1.Sheets[1], Type.Missing);
workbook1.Save();
workbook1.Close(false, Type.Missing, Type.Missing);
excel.DisplayAlerts = false;
});
if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
{
return true;
}
workbook.Save();
workbook.Close(false, Type.Missing, Type.Missing);
excel.Quit();
excel = null;
GC.Collect();
}
}