[Transfer from others ' blogs] Excel Worksheet Copy

Source: Internet
Author: User

void Cp ()

{

OpenFileDialog openFileDialog1 = new OpenFileDialog ();
Openfiledialog1.title = "Please select source file";
Openfiledialog1.filter = "Excel file (*.xls) |*.xls";
Openfiledialog1.showdialog ();

if (OpenFileDialog1.FileName.Equals (""))
{
MessageBox.Show ("Please select an Excel file!");
}
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 = "Unnamed table. 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 ();
}

}

[Transfer from others ' blogs] Excel Worksheet Copy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.