Step by step, explore the Second Development of Excel. net source code (5)-Worksheets class

Source: Internet
Author: User
Tags ranges
Step by step, explore the Second Development of Excel. net source code (5)-Worksheets class
-- Enable EXCEL to close printing and preview
Author: Yangtze River Tributary

Keywords:. net, Excel, Excel open, Excel closed, Excel print preview, Excel secondary development, object-oriented, design mode

  

The structure of an Excel worksheet is composed of multiple worksheet. Currently, a worksheet collection class is provided to facilitate operations on the Excel worksheet. This allows you to add, delete, modify, locate, and copy data on the worksheet.

Using system; namespace goldprinter. excelexpert {// <summary> // worksheet set in the Excel worksheet. As a visitor to the workbook, you can add, delete, modify, and locate the worksheet set to facilitate expansion. /// Note that the index here follows C # from 0, while Excel itself starts from 1. /// // Author: Yangtze River Tributary (Zhou fangyong) // Email: MisGoldPrinter@163.com QQ: 150439795 // address: www.webmis.com.cn ///★★★★★You can use this program for free, but please keep this description completely to maintain intellectual property rights★★★★★///// </Summary> public class worksheets: system. Collections. ienumerable {private excel. Workbook _ xlworkbook; // The Workbook of the Excel worksheet set. Private object omissing = system. reflection. missing. value; // default parameter # region application and workbooks field attributes when instantiating an object // <summary> // obtain the Excel application // </Summary> Public excel. application application {get {return _ xlworkbook. application ;}//< summary> // obtain the workbook of the Excel worksheet set. /// </Summary> Public excel. workbook workbook {get {return _ xlworkbook ;}# endregion /// <summary> // create a new object of the class, and specify the attached working thin. /// </Summary> /// <Param name = "workbook"> </param> Public worksheets (Excel. workbook workbook) {_ xlworkbook = workbook;} // <summary> // obtain the number of worksheets. /// </Summary> /// <returns> </returns> Public int count {get {return getworksheetscount ();}} /// <summary> /// obtain the currently activated worksheet /// </Summary> Public excel. worksheet activesheet {get {return (Excel. worksheet) _ xlworkbook. activesheet ;}/// <summary> // obtain the worksheet of the specified index in the set. If no worksheet exists, null is returned. /// </Summary> Public excel. worksheet this [string name] {get {excel. worksheet sheetreturn = NULL; object OBJ = NULL; try {// if no value exists, an error occurs. OBJ = _ xlworkbook. sheets [name];} catch {} If (OBJ! = NULL) {sheetreturn = (Excel. worksheet) OBJ ;}return sheetreturn ;}/// <summary> // obtain the worksheet of the specified index in the set. If the index is too large, the last one is returned, if it is too small, the first worksheet is returned. /// </Summary> Public excel. worksheet this [int Index] {get {excel. worksheet sheetreturn = NULL; int COUNT = getworksheetscount (); int I = index + 1; // note that the index starts from 0, while Excel starts from 1, so add 1. If (I <1) {I = 1;} else if (I> count) {I = count ;} // get the I worksheet to activate and return (index starts from 1) sheetreturn = (Excel. worksheet) _ xlworkbook. worksheets [I]; return sheetreturn ;}} /// <summary> /// force re-calculation of all worksheets, specific ranges /// </Summary> /// <returns> </returns> Public void calculate () {foreach (Excel. worksheet sheet in this) {sheet. calculate ();}} /// <summary> /// force re-calculation of all worksheets, specific ranges /// </Summary> /// <returns> </returns> Public void calculate (exce L. worksheet sheet) {sheet. calculate ();} /// <summary> /// force re-calculation of all worksheets, specific ranges /// </Summary> /// <returns> </returns> Public void calculate (Excel. range) {range. calculate () ;}/// <summary> // clear the worksheet in the workbook. Note that not all worksheets are cleared because at least one worksheet is retained in Excel. /// </Summary> Public void clear () {// Number of worksheets int sheetscount = getworksheetscount (); // delete a previous worksheet, but at least one worksheet must be retained in Excel, note that the Excel Index starts from 1. If (sheetscount> 1) {for (INT I = 2; I <= sheetscount; I ++) {(Excel. worksheet) _ xlworkbook. worksheets [2]). delete () ;}}/// <summary> // activate the worksheet of the specified index and return it. If the index is too large, the last worksheet is returned. If the index is too small, the first worksheet is returned. /// </Summary> /// <Param name = "Index"> </param> /// <returns> </returns> Public excel. worksheet activate (INT index) {excel. worksheet sheetreturn = NULL; sheetreturn = This [Index]; sheetreturn. activate (); Return sheetreturn;} // <summary> // activate the specified worksheet and return it. If the specified table does not exist, null is returned. /// </Summary> /// <Param name = "name"> worksheet name </param> /// <returns> </returns> Public excel. worksheet activate (string name) {excel. worksheet sheetreturn = NULL; sheetreturn = This [name]; If (sheetreturn! = NULL) {sheetreturn. Activate ();} return sheetreturn;} // <summary> // Add and return a worksheet. /// </Summary> Public excel. worksheet add () {return add (1) ;}/// <summary> /// Add a specified number of worksheets and return the last added worksheet. /// </Summary> /// <Param name = "count"> added quantity </param> Public excel. worksheet add (INT count) {return (Excel. worksheet) _ xlworkbook. worksheets. add (omissing, _ xlworkbook. worksheets [getworksheetscount ()], Count, omissing) ;}/// <summary> /// insert a worksheet before the workbook and return a worksheet. /// </Summary> Public excel. worksheet insert () {return (Excel. worksheet) _ xlworkbook. worksheets. add (omissing, omissing) ;}/// <summary> /// insert a worksheet before the workbook and return a worksheet. /// </Summary> /// <Param name = "Index"> specify the position to insert </param> Public excel. worksheet insert (INT index) {return insert (index, 1 );} /// <summary> /// insert a specified number of worksheets before the workbook and return the last inserted worksheet. /// </Summary> /// <Param name = "Index"> specify the location to insert </param> /// <Param name = "count"> specify number of inserts </param> Public excel. worksheet insert (INT index, int count) {int worksheetscount = getworksheetscount (); If (index> = worksheetscount) {// append return add ();} else {// activate (INDEX); Return (Excel. worksheet) _ xlworkbook. worksheets. add (omissing, omissing, Count, omissing) ;}/// <summary> // insert a specified number of worksheets before the specified worksheet and return the last inserted worksheet. /// </Summary> /// <Param name = "name"> specify the location to insert </param> /// <Param name = "count"> specify number of inserts </param> Public excel. worksheet insert (string name, int count) {// activate (name); Return (Excel. worksheet) _ xlworkbook. worksheets. add (omissing, omissing, Count, omissing );} /// <summary> /// Delete the worksheet at the specified position /// </Summary> /// <Param name = "Index"> specify the location to be deleted </Param >/// <returns> </returns> Public void remove (INT index) {int COUNT = getw Orksheetscount (); int I = index + 1; // note that the index starts from 0, while Excel itself starts from 1, so Add 1. // Excel requires that at least one worksheet be retained if (count> 1) {// if (I> = 1 & I <= count) {(Excel. worksheet) _ xlworkbook. worksheets [I]). delete ();}}} /// <summary> /// Delete the specified worksheet /// </Summary> /// <Param name = "name"> specify the name of the table to be deleted </Param >/// <returns> </returns> Public void remove (string name) {excel. worksheet sheet = NULL; sheet = This [name]; If (sheet! = NULL) {sheet. delete ();}} /// <summary> /// rename the specified worksheet /// </Summary> /// <Param name = "oldname"> specify the name of the table to be renamed </Param> /// <Param name = "newname"> New table name </param> /// <returns> </returns> Public excel. worksheet Rename (string oldname, string newname) {excel. worksheet sheet = NULL; sheet = This [oldname]; If (sheet! = NULL) {sheet. Name = newname;} return sheet;} // <summary> // create a copy of the table and insert the table to the specified position. /// </Summary> /// <Param name = "Source"> location or name of the original table </param> /// <Param name = "DEST"> Target the location where the table is inserted or before the table with the specified name </param> Public void copy (INT source, int DEST) {// you can specify to insert a new table to the front or back of an existing table. If no location is specified, Excel creates a new workbook to store the new table. // The following code snippet copies the first table in the current workbook and places the copy behind the third table: // (Excel. worksheet) thisworkbook. sheets [1]). copy (type. missing, thisworkbook. sheets [3]);. excel. worksheet sheetsource = This [Source]; If (sheetsource! = NULL) {excel. worksheet sheetdesc = This [DEST]; int sheetcount = getworksheetscount (); // This [Index] is used mainly to retrieve invalid indexes and return valid worksheets for valid boundary values, therefore, determine if (DEST> = sheetcount | sheetdesc = NULL) {// Add this [Source] after the last worksheet. copy (type. missing, this [getworksheetscount ()]);} else {If (sheetdesc! = NULL) {// insert this [Source] at the specified position. copy (this [DEST], type. missing) ;}}}/// <summary> /// create a copy of the table and insert the table to the specified position. /// </Summary> /// <Param name = "Source"> location or name of the original table </param> /// <Param name = "DEST"> Target the location where the table is inserted or before the table with the specified name </param> Public void copy (string source, string DEST) {excel. worksheet sheetsource = This [Source]; If (sheetsource! = NULL) {excel. worksheet sheetdesc = This [DEST]; If (sheetdesc! = NULL) {// insert this [Source] at the specified position. copy (this [DEST], type. missing);} else {// Add this [Source] after the last worksheet. copy (type. missing, this [getworksheetscount ()]) ;}}/// <summary> // you can use foreach to access each worksheet. /// </Summary> /// <returns> </returns> public system. collections. ienumerator getenumerator () {int COUNT = getworksheetscount (); excel. worksheet [] arr = new excel. worksheet [count]; for (INT I = 0; I <count; I ++) {arr [I] = (Excel. worksheet) _ xlworkbook. worksheets [I + 1]; // The Excel Index starts from 1} return arr. getenumerator ();} // <summary> // obtain the number of worksheets /// </Summary> /// <returns> </returns> private int getworksheetscount () {int Sheetscount = _ xlworkbook. worksheets. Count; return sheetscount;} // <summary> /// obtain the developer information of the program. If you encounter any difficulties or have new ideas or suggestions during development and usage, contact the author. /// Our aim is to popularize it. net common educational promotion technology sharing practical source code /// </Summary> Public void print?infotoconsole () {string authorinfo = "\ n \ r" + "author: yangtze River Tributary (Zhou fangyong) "+" \ n \ r "+" Email: MisGoldPrinter@163.com QQ: 150439795 "+" \ n \ r "+" Address: www.webmis.com.cn "; console. writeline (authorinfo) ;}// end class} // end namespace

 

In the subsequent sections, we will continue to explain that rangevisitor is used to read and write band, merge, and border, while MIS gold printing is a font defined for excel. Because the font of the COM component is essentially different from that of the. NET font, we will provide methods to add the underline, shadow, background, and upper/lower mark operations to the important attributes of the font.

 

For information on source code download, visit:
Http://blog.csdn.net/flygoldfish

Disclaimer: This article is copyrighted by Zhou fangyong. You are welcome to reprint it. Please keep the complete content and source.
Flygoldfish@163.com

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.