C # problems encountered when reading Excel content to the class

Source: Internet
Author: User
<Summary> /// set the horizontal center of the continuous area /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <Param name = "objstartcell"> Start Cell </param> // <Param name = "objendcell"> end cell </param> Public void sethaligncenter (Excel. _ worksheet cursheet, object objstartcell, object objendcell) {cursheet. get_range (objstartcell, objendcell ). horizontalalignment = excel. xlhalign. xlhaligncenter ;} /// <summary> /// set the horizontal center of the continuous area to left /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <Param name = "objstartcell"> Start Cell </param> // <Param name = "objendcell"> end cell </param> Public void sethalignleft (Excel. _ worksheet cursheet, object objstartcell, object objendcell) {cursheet. get_range (objstartcell, objendcell ). horizontalalignment = excel. xlhalign. xlhalignleft ;} /// <summary> /// set the continuous area to the right. /// </Summary> /// <Param name = "cursheet"> worksheet </param> // /<Param name = "objstartcell"> Start Cell </param> // <Param name = "objendcell"> end cell </param> Public void sethalignright (Excel. _ worksheet cursheet, object objstartcell, object objendcell) {cursheet. get_range (objstartcell, objendcell ). horizontalalignment = excel. xlhalign. xlhalignright ;} /// <summary> /// set the display format of the continuous area /// </Summary> /// <Param name = "cursheet"> worksheet </param> // /<Param name = "objstartcell"> Start Cell </param> // <Param name = "objendcell"> end cell </param> // <Param name =" strnf "> such "#, #0.00 "display format </param> Public void setnumberformat (Excel. _ worksheet cursheet, object objstartcell, object objendcell, string strnf) {cursheet. get_range (objstartcell, objendcell ). numberformat = strnf ;} /// <summary> /// set the column width /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <Param name = "strcolid"> column ID, for example, a indicates the first column </param> // <Param name = "dblwidth"> width </param> Public void setcolumnwidth (Excel. _ worksheet cursheet, string strcolid, double dblwidth) {(Excel. range) cursheet. columns. getType (). invokemember ("item", system. reflection. bindingflags. getproperty, null, cursheet. columns, new object [] {(strcolid + ":" + strcolid ). tostring ()})). columnwidth = dblwidth ;} /// <summary> /// set the column width /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <Param name = "objstartcell"> Start Cell </param> // <Param name = "objendcell"> end cell </param> /// <Param name = "dblwidth"> width </param> Public void setcolumnwidth (Excel. _ worksheet cursheet, object objstartcell, object objendcell, double dblwidth) {cursheet. get_range (objstartcell, objendcell ). columnwidth = dblwidth ;} /// <summary> /// set the Row Height /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <Param name = "objstartcell"> Start Cell </param> // <Param name = "objendcell"> end cell </param> /// <Param name = "dblheight"> row Height </param> Public void setrowheight (Excel. _ worksheet cursheet, object objstartcell, object objendcell, double dblheight) {cursheet. get_range (objstartcell, objendcell ). rowheight = dblheight ;} /// <summary> /// Add a hyperlink to a cell /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <Param name = "objcell"> cell </param> /// <Param name = "straddress"> link address </param> /// <Param name = "strtip"> on-screen prompt </param> /// <Param name = "strtext"> link text </param> Public void addhyperlink (Excel. _ worksheet cursheet, object objcell, string straddress, string strtip, string strtext) {cursheet. hyperlinks. add (cursheet. get_range (objcell, objcell), straddress, mvalue, strtip, strtext );} /// <summary> /// save as an xls file /// </Summary> /// <Param name = "curbook"> workbook </param> /// <Param name = "strfilepath"> file path </param> Public void save (Excel. _ workbook curbook, string strfilepath) {curbook. savecopyas (strfilepath );} /// <summary> /// save the file /// </Summary> /// <Param name = "curbook"> workbook </param> /// <Param name = "strfilepath"> file path </param> Public void saveas (Excel. _ workbook curbook, string strfilepath) {curbook. saveas (strfilepath, mvalue, Excel. xlsaveasaccessmode. xlshared, mvalue, mvalue );} /// <summary> /// save as an HTML file /// </Summary> /// <Param name = "curbook"> workbook </param> /// <Param name = "strfilepath"> file path </param> Public void savehtml (Excel. _ workbook curbook, string strfilepath) {curbook. saveas (strfilepath, Excel. xlfileformat. xlhtml, mvalue, Excel. xlsaveasaccessmode. xlnochange, mvalue, mvalue) ;}/// <summary> // release memory /// </Summary> Public void dispose (Excel. _ worksheet cursheet, Excel. _ workbook curbook, Excel. _ application curexcel) {try {system. runtime. interopservices. marshal. releasecomobject (cursheet); cursheet = NULL; curbook. close (false, mvalue, mvalue); system. runtime. interopservices. marshal. releasecomobject (curbook); curbook = NULL; curexcel. quit (); system. runtime. interopservices. marshal. releasecomobject (curexcel); curexcel = NULL; GC. collect (); GC. waitforpendingfinalizers ();} catch (system. exception ex) {httpcontext. current. response. write ("an error occurred when releasing the Excel memory space:" + ex) ;}finally {foreach (system. diagnostics. process pro in system. diagnostics. process. getprocessesbyname ("Excel") // If (Pro. starttime <datetime. now) Pro. kill ();} system. GC. suppressfinalize (this );}}

Using system; using system. Web; using Excel = Microsoft. Office. InterOP. Excel; // <summary> /// Summary of exceloperate. Excel operation function // </Summary> public class exceloperate {private object mvalue = system. reflection. missing. value; Public exceloperate () {// todo: add the constructor logic here // <summary> // merge cells /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <Param name = "objstartcell"> Start Cell </param> // <Param name = "objendcell"> end cell </param> Public void merge (Excel. _ worksheet cursheet, object objstartcell, object objendcell) {cursheet. get_range (objstartcell, objendcell ). merge (0 );} /// <summary> /// set the font size of the continuous area /// </Summary> /// <Param name = "cursheet"> worksheet </param> // /<Param name = "strstartcell"> Start Cell </param> // <Param name = "strendcell"> end cell </param> // <Param name =" intfontsize "> font size </param> Public void setfontsize (Excel. _ worksheet cursheet, object objstartcell, object objendcell, int intfontsize) {cursheet. get_range (objstartcell, objendcell ). font. size = intfontsize. tostring ();} /// <summary> /// print horizontally /// </Summary> /// <Param name = "cursheet"> </param> Public void xllandscape (Excel. _ worksheet cursheet) {cursheet. pagesetup. orientation = excel. xlpageorientation. xllandscape ;} /// <summary> /// print vertically /// </Summary> /// <Param name = "cursheet"> </param> Public void xlportrait (Excel. _ worksheet cursheet) {cursheet. pagesetup. orientation = excel. xlpageorientation. xlportrait ;} /// <summary> /// Insert the specified value into the specified cell /// </Summary> /// <Param name = "cursheet"> worksheet </param>/ // <Param name = "cell"> cells, such as cells [] </param> /// <Param name = "objvalue"> equivalent of text and numbers </param> Public void writecell (Excel. _ worksheet cursheet, object objcell, object objvalue) {cursheet. get_range (objcell, mvalue ). value2 = objvalue ;} /// <summary> /// Insert the specified value in the specified range /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <Param name = "startcell"> Start Cell </param> /// <Param name = "endcell"> end cell </param> /// <Param name = "objvalue"> equivalent of text and numbers </param> Public void writerange (Excel. _ worksheet cursheet, object objstartcell, object objendcell, object objvalue) {cursheet. get_range (objstartcell, objendcell ). value2 = objvalue;} // <summary> // merge the cells, insert the specified value to the merged cell. /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <Param name = "objstartcell"> Start Cell </param> // <Param name = "objendcell"> end cell </param> /// <Param name = "objvalue"> text, numeric equivalent </param> Public void writeaftermerge (Excel. _ worksheet cursheet, object objstartcell, object objendcell, object objvalue) {cursheet. get_range (objstartcell, objendcell ). merge (mvalue); cursheet. get_range (objstartcell, mvalue ). value2 = objvalue ;} /// <summary> /// set the formula for the cell /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <param name = "objcell"> cell </param> // <Param name = "strformula"> formula </param> Public void setformula (Excel. _ worksheet cursheet, object objcell, string strformula) {cursheet. get_range (obthe cell, mvalue ). formula = strformula ;} /// <summary> /// automatic cell line feed /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <Param name = "objstartcell"> Start Cell </param> // <Param name = "objendcell"> end cell </param> Public void autowraptext (Excel. _ worksheet cursheet, object objstartcell, object objendcell) {cursheet. get_range (objstartcell, objendcell ). wraptext = true ;} /// <summary> /// set the font color of the entire continuous area /// </Summary> /// <Param name = "cursheet"> worksheet </param>/ // <Param name = "objstartcell"> Start Cell </param> /// <Param name = "objendcell"> end cell </param> /// <Param name = "clrcolor"> color </param> Public void setcolor (Excel. _ worksheet cursheet, object objstartcell, object objendcell, system. drawing. color clrcolor) {cursheet. get_range (objstartcell, objendcell ). font. color = system. drawing. colortranslator. toole (clrcolor );} /// <summary> /// set the cell background color of the entire continuous area /// </Summary> /// <Param name = "cursheet"> </param> // /<Param name = "objstartcell"> </param> /// <Param name = <objendcell "> </param> // <Param name =" clrcolor "> </param> Public void setbgcolor (Excel. _ worksheet cursheet, object objstartcell, object objendcell, system. drawing. color clrcolor) {cursheet. get_range (objstartcell, objendcell ). interior. color = system. drawing. colortranslator. toole (clrcolor );} /// <summary> /// set the font name of the continuous area /// </Summary> /// <Param name = "cursheet"> worksheet </param> // /<Param name = "objstartcell"> Start Cell </param> /// <Param name = "objendcell"> end cell </param> /// <Param name =" fontname "> font name, gb_ gb2312, etc. </param> Public void setfontname (Excel. _ worksheet curcursheet, object objstartcell, object objendcell, string fontname) {cursheet. get_range (objstartcell, objendcell ). font. name = fontname ;} /// <summary> /// set the font of the continuous area to // </Summary> /// <Param name = "cursheet"> worksheet </param>/ // <Param name = "objstartcell"> Start Cell </param> // <Param name = "objendcell"> end cell </param> Public void setbold (Excel. _ worksheet cursheet, object objstartcell, object objendcell) {cursheet. get_range (objstartcell, objendcell ). font. bold = true;} // <summary> // you can specify the border of a continuous area: black continuous border on the top, bottom, and left /// </Summary> /// <Param name = "cursheet"> worksheet </param> /// <Param name = "objstartcell"> cell </param> /// <Param name = "objendcell"> end cell </param> Public void setborderall (Excel. _ worksheet cursheet, object objstartcell, object objendcell) {cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xledgetop]. color = system. drawing. colortranslator. toole (system. drawing. color. lightgray); cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xledgetop]. linestyle = excel. xllinestyle. xlcontinuous; cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xledgebottom]. color = system. drawing. colortranslator. toole (system. drawing. color. lightgray); cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xledgebottom]. linestyle = excel. xllinestyle. xlcontinuous; cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xledgeleft]. color = system. drawing. colortranslator. toole (system. drawing. color. lightgray); cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xledgeleft]. linestyle = excel. xllinestyle. xlcontinuous; cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xledgeright]. color = system. drawing. colortranslator. toole (system. drawing. color. lightgray); cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xledgeright]. linestyle = excel. xllinestyle. xlcontinuous; cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xlinsidehorizontal]. color = system. drawing. colortranslator. toole (system. drawing. color. lightgray); cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xlinsidehorizontal]. linestyle = excel. xllinestyle. xlcontinuous; cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xlinsidevertical]. color = system. drawing. colortranslator. toole (system. drawing. color. lightgray); cursheet. get_range (objstartcell, objendcell ). borders [Excel. xlbordersindex. xlinsidevertical]. linestyle = excel. xllinestyle. xlcontinuous ;}

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.