Copy an Excel table, disable worksheet protection, and unfreeze

Source: Internet
Author: User

When I recently processed the Excel saved by Farpoint, I found that there were various problems with the table style. below is how to modify the Excel format to operate on the content and take notes :)

 

/// <Summary>
/// Excel table operations
/// </Summary>
/// <Param name = "firstpath"> path of the first Excel table </param>
/// <Param name = "secondpath"> path of the second Excel table </param>
Private void exceloperation (string firstpath, string secondpath)
{
Try
{
Microsoft. Office. InterOP. Excel. Application APP = new Microsoft. Office. InterOP. Excel. Application ();
Microsoft. office. interOP. excel. workbook fworkbook = app. workbooks. open (firstpath, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing );
Microsoft. office. interOP. excel. workbook sworkbook = app. workbooks. open (secondpath, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing );
App. displayalerts = false; // The operation prompt is blocked.
Try
{
(Microsoft. Office. InterOP. Excel. worksheet) sworkbook. Sheets [1]). Delete (); // Delete the first sheet of the second table
Sworkbook. Save ();

Microsoft. Office. InterOP. Excel. worksheet sheet = (Microsoft. Office. InterOP. Excel. worksheet) fworkbook. Sheets [1]; // obtain the first sheet of the first table

Sheet. unprotect (type. Missing); // cancel worksheet Protection

Microsoft. Office. InterOP. Excel. Range = (Microsoft. Office. InterOP. Excel. Range) sheet. Rows [1, type. Missing]; // obtain the first row
Sheet. Activate ();
App. activewindow. freezepanes = false; // unfreeze
App. activewindow. splitcolumn = 0;
App. activewindow. splitrow = 0;

Range. Interior. colorindex = 0; // you can specify that the first row is white.
Range. Delete (Microsoft. Office. InterOP. Excel. xldirection. xldown); // Delete the first row.

Sheet. Copy (sworkbook. Sheets [1], type. Missing); // copy to the second table
Sworkbook. Save ();

}
Catch (exception ex)
{
}
Finally
{
Fworkbook. Close (false, firstpath, null );
System. runtime. interopservices. Marshal. releasecomobject (fworkbook );
Fworkbook = NULL;

Sworkbook. Close (false, secondpath, null );
System. runtime. interopservices. Marshal. releasecomobject (sworkbook );
Sworkbook = NULL;

App. Quit ();
System. runtime. interopservices. Marshal. releasecomobject (APP );
}
}
Catch (exception ex)
{

}
}

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.