Step by step, explore the Second Development of Excel. net source code (4)-workbooks 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
An Excel instance has been created and can be saved and printed on Excel. Now, we will encapsulate the use of the workbooks class to facilitate operations on the Excel worksheet, to add, delete, modify, and locate worksheets.
Using system; using goldprinter. excelexpert. exceptions; using goldprinter. excelexpert. constants; 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 However, please keep this description completely to protect your intellectual property rights★★★★★///// </Summary> public class workbooks: system. Collections. ienumerable {private excel. Application _ xlapp; // Excel application. Private object omissing = system. reflection. missing. value; // default parameter # region application field attribute when instantiating an object // <summary> // obtain the Excel application // </Summary> Public excel. application application {get {return _ xlapp ;}# endregion /// <summary> // create a new object of the class and specify the attached working thin. /// </Summary> /// <Param name = "application"> </param> Public workbooks (Excel. application application) {_ xlapp = Application ;}/// <summary> /// obtain the number of worksheets. /// </Summary> /// <returns> </returns> Public int count {get {return getworkbookscount () ;}# add) /// <summary> /// Add a new working thin and open and return. /// </Summary> /// <returns> </returns> Public excel. workbook add () {try {// create and return the Excel worksheet return _ xlapp. workbooks. add (omissing);} catch (system. exception ex) {Throw new openexcelexception ("Excel opening error, details:" + ex. message) ;}}/// <summary> /// add and enable the template based on the existing template. If the specified template does not exist, the default empty template is used. /// </Summary> /// <Param name = "templatefilename"> used as the template's thin file name </param> Public excel. workbook add (string templatefilename) {If (system. io. file. exists (templatefilename) {// open it with a template // workbooks. add template: = "C: \ TPT. xlt "try {return _ xlapp. workbooks. add (templatefilename);} catch (system. exception ex) {Throw new openexcelexception ("Excel opening error, details:" + ex. message) ;}} else {return add () ;}/ * /// <summary> /// read the specified text from the specified row This document is in Excel. Text items are separated by commas and are defined in double quotation marks, for example, "A", "B ", "C" /// </Summary> /// <Param name = "textfilename"> </param> /// <Param name = "startrow"> </param> /// <returns> </returns> Public excel. workbook addtextfile (string textfilename, int startrow) {If (system. io. file. exists (textfilename) {try {excel. workbook xlworkbook = NULL; _ xlapp. workbooks. opentext (textfilename, type. missing, startrow, Excel. xltextparsingtype. xldelimited, exce L. xltextqualifier. xltextqualifierdoublequote, type. missing, type. missing, type. missing, true, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing); Return xlworkbook;} catch (system. exception ex) {Throw new openexcelexception ("Excel opening error, details:" + ex. message) ;}} else {return add () ;}} */# endregion // <summary> // close all opened workbooks, and clear () in the same way, close all thin windows. // However, the latter does not mention Indicates whether the system displays a prompt based on the value of application. displayalerts. /// </Summary> Public void close () {// unlike most collections, this set allows you to close all members at once try {_ xlapp. workbooks. close ();} catch {// The user closes the Excel window of the program manually from the interface }}/// <summary> /// gets the currently activated workbook /// </Summary> Public excel. workbook activeworkbook {get {return (Excel. workbook) _ xlapp. activeworkbook; }}/// <summary> // obtain the working thin of the specified index in the set. If not, null is returned. /// Use the name in the navigation bar, that is, before saving the file, this name is not included in the extension. xls. /// </Summary> Public excel. workbook this [string name] {get {excel. workbook bookreturn = NULL; object OBJ = NULL; try {// if no file exists, an error occurs. Try {// the file name is not included in the file extension. OBJ = _ xlapp. workbooks [name];} catch {// after saving the file, this name is named "".xls "extension. OBJ = _ xlapp. workbooks [name + ". xls"] ;}} catch {} if (OBJ! = NULL) {bookreturn = (Excel. workbook) OBJ ;}return bookreturn ;}/// <summary> /// obtain the specified index in the set. If the index is too large, the system returns the last one, if it is too small, the first working thin is returned. /// </Summary> Public excel. workbook this [int Index] {get {excel. workbook bookreturn = NULL; int COUNT = getworkbookscount (); 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 ;} // obtain the I-th workbook activation and return the result (index starts from 1) bookreturn = (Excel. workbook) _ xlapp. workbooks [I]; return bookreturn ;}} /// <summary> /// force re-calculation of all opened workbooks, specific workbooks, or specific ranges /// </Summary> /// <returns> </returns> Public void calculate () {_ xlapp. calculate ();} /// <summary> /// force re-calculation of all opened workbooks, specific workbooks, or specific ranges /// </Summary> /// <returns> </returns> Public void calculate (Excel. worksheet sheet) {sheet. c Alculate ();} /// <summary> /// force re-calculation of all opened workbooks, specific workbooks, or specific ranges /// </Summary> /// <returns> </returns> Public void calculate (Excel. range) {range. calculate () ;}//< summary> /// activate the working thin of the specified index and return it. If the index is too large, the last one is returned. If the index is too small, the first working thin is returned. /// </Summary> /// <Param name = "Index"> valid index. If the index is too large, the system returns the last one. If the index is too small, the system returns the first working thin. </Param> // <returns> </returns> Public excel. workbook activate (INT index) {excel. workbook bookreturn = NULL; bookreturn = This [Index]; bookreturn. activate (); Return bookreturn;} /// <summary> /// activate the specified workbook and return it. If it is not stored, null is returned. /// </Summary> /// <Param name = "name"> A small work name. If it is saved, it must contain .xls, </param> // <returns> </returns> Public Excel is not saved. workbook activate (string name) {excel. workbook bookreturn = NULL; bookreturn = This [name]; If (bookreturn! = NULL) {bookreturn. activate ();} return bookreturn ;} /// <summary> /// Delete the working book at the specified position /// </Summary> /// <Param name = "Index"> specify the location to be deleted </Param >/// <returns> </returns> Public void remove (INT index) {excel. workbook book = This [Index]; If (book! = NULL) {// Save the prompt to switch bool blntmp = _ xlapp when the window is closed. displayalerts; _ xlapp. displayalerts = false; book. saved = true; book. close (omissing, omissing, omissing); _ xlapp. displayalerts = blntmp ;}} /// <summary> /// Delete the workbook with the specified name /// </Summary> /// <Param name = "name"> specify the name of the workbook to be deleted </ param> // <returns> </returns> Public void remove (string name) {excel. workbook book = This [name]; If (book! = NULL) {// Save the prompt to switch bool blntmp = _ xlapp when the window is closed. displayalerts; _ xlapp. displayalerts = false; book. saved = true; book. close (omissing, omissing, omissing); _ xlapp. displayalerts = blntmp ;}/// <summary> /// clear all the workbooks. Close all thin windows in the same way as the close () method, but there is no prompt in this method. /// </Summary> Public void clear () {// save whether bool blntmp = _ xlapp is enabled when the window is closed. displayalerts; _ xlapp. displayalerts = false; foreach (Excel. workbook book in this) {book. saved = true; book. close (omissing, omissing, omissing);} _ xlapp. displayalerts = blntmp;} /// <summary> /// you can use foreach to access each workbook. /// </Summary> /// <returns> </returns> public system. collections. ienumerator getenumerator () {int COUNT = getworkbookscount (); excel. workbook [] arr = new excel. workbook [count]; for (INT I = 0; I <count; I ++) {arr [I] = (Excel. workbook) _ xlapp. workbooks [I + 1]; // The Excel Index starts from 1} return arr. getenumerator () ;}/// <summary> /// obtain the number of workbooks /// </Summary> /// <returns> </returns> private int getworkbookscount () {int bookscount = _ Xlapp. workbooks. Count; return bookscount;} // <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
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