asp.net use OWC to manipulate the instance code of Excel

Source: Internet
Author: User
Tags datetime tostring
This article introduced the ASP.net with OWC operation Excel's instance code, has the need friend to be possible to refer to, hoped has the help to you to be helpful copies the code code as follows:




string connstr = system.configuration.configurationmanager.connectionstrings["dqpihrconnectionstring"]. ToString ();


SqlConnection conn = new SqlConnection (CONNSTR);


SqlDataAdapter SDA = new SqlDataAdapter (SQL1. TEXT, conn);


DataSet ds = new DataSet ();


Conn. Open ();


SDA. Fill (DS);


Conn. Close ();


OWC10. Spreadsheetclass Xlsheet;


xlsheet= New OWC10. Spreadsheetclass ();


DataRow Dr;


int i = 0;


for (int ii=0;ii<ds. Tables[0]. rows.count;ii++)


        {


dr = ds. Tables[0]. ROWS[II];


//Merge cell


Xlsheet.get_range (xlsheet. Cells[i+1, 1], xlsheet. Cells[i+1, 8]). Set_mergecells (True);


Xlsheet.get_range (xlsheet. Cells[i + 5, 1], xlsheet. Cells[i + 5, 3]). Set_mergecells (True);


Xlsheet.get_range (Xlsheet. Cells[i + 5, 4], xlsheet. Cells[i + 5, 6]). Set_mergecells (True);


Xlsheet.get_range (xlsheet. Cells[i + 5, 7], xlsheet. Cells[i + 5, 8]). Set_mergecells (True);


Xlsheet. Activesheet.cells[i + 1, 1] = dr["Name"]. ToString () + "natural condition";


//FONT bold


Xlsheet.get_range (xlsheet. Cells[i + 1, 1], xlsheet. Cells[i + 1, 14]). Font.set_bold (TRUE);


//Cell text horizontally centered


Xlsheet.get_range (Xlsheet. Cells[i + 1, 1], xlsheet. Cells[i + 1]). Set_horizontalalignment (OWC10. Xlhalign.xlhaligncenter);


//Set Font size


Xlsheet.get_range (xlsheet. Cells[i + 1, 1], xlsheet. Cells[i + 1, 14]). Font.set_size (14);


//Set column width


Xlsheet.get_range (xlsheet. Cells[i + 1, 8], xlsheet. Cells[i + 1, 8]). Set_columnwidth (20);


//Draw Border line


Xlsheet.get_range (Xlsheet. Cells[i + 1, 1], xlsheet. Cells[i+5, 8]). Borders.set_linestyle (OWC10. xllinestyle.xlcontinuous);


//write data (generated here by DS)


Xlsheet. Activesheet.cells[i + 2, 1] = "name";


Xlsheet. Activesheet.cells[i + 2, 2] = dr["Name"]. ToString ();


Xlsheet. Activesheet.cells[i + 2, 3] = "used name";


Xlsheet. Activesheet.cells[i + 2, 4] = dr["used name"]. ToString ();


Xlsheet. Activesheet.cells[i + 2, 5] = "Date of birth";


Xlsheet. Activesheet.cells[i + 2, 6] = DateTime.Parse (dr["date of birth"). ToString ()). Year.tostring () + "-" + datetime.parse (dr["Birth date"). ToString ()). Month.tostring ();


Xlsheet. Activesheet.cells[i + 2, 7] = "Participation in working hours";


Xlsheet. Activesheet.cells[i + 2, 8] = DateTime.Parse (dr["Working hours"). ToString ()). Year.tostring () + "-" + datetime.parse (dr["Working hours"). ToString ()). Month.tostring ();


Xlsheet. Activesheet.cells[i + 3, 1] = "gender";


Xlsheet. Activesheet.cells[i + 3, 2] = dr["Sex"]. ToString ();


Xlsheet. Activesheet.cells[i + 3, 3] = "Nation";


Xlsheet. Activesheet.cells[i + 3, 4] = dr["Nation"]. ToString ();


Xlsheet. Activesheet.cells[i + 3, 5] = "political visage";


Xlsheet. Activesheet.cells[i + 3, 6] = dr["political visage"]. ToString ();


Xlsheet. Activesheet.cells[i + 3, 7] = "title";


Xlsheet. Activesheet.cells[i + 3, 8] = dr["title"]. ToString ();


Xlsheet. Activesheet.cells[i + 4, 1] = "education";


Xlsheet. Activesheet.cells[i + 4, 2] = dr["Education"]. ToString ();


Xlsheet. Activesheet.cells[i + 4, 3] = "degree";


Xlsheet. Activesheet.cells[i + 4, 4] = dr["degree"]. ToString ();


Xlsheet. Activesheet.cells[i + 4, 5] = "position";


Xlsheet. Activesheet.cells[i + 4, 6] = dr["title"]. ToString ();


Xlsheet. Activesheet.cells[i + 4, 7] = "File number";


//excel does not support 0 start input, plus the first letter of surname is exactly the number name


Xlsheet. Activesheet.cells[i + 4, 8] = dr["surname first letter"]. ToString () + dr["file Number"]. ToString ();


Xlsheet. Activesheet.cells[i + 5, 1] = "Now engaged in professional:" + dr["is engaged in professional"]. ToString ();


Xlsheet. Activesheet.cells[i + 5, 4] = "Work unit:" + dr["work unit"]. ToString ();


Xlsheet. Activesheet.cells[i + 5, 7] = "ID card:" + dr["identity card number"]. ToString ();


i = 6;


        }


Try


        {


string D = DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + DateTime.Now.Day.ToString () +


DateTime.Now.Hour.ToString () + DateTime.Now.Minute.ToString () + DateTime.Now.Second.ToString () +


DateTime.Now.Millisecond.ToString ();


Xlsheet. Export (Server.MapPath ("./") + "" +d+ ". xls", OWC10. Sheetexportactionenum.ssexportactionnone, OWC10. Sheetexportformat.ssexportxmlspreadsheet);


Response.Write ("<script>window.open" (' +d+ ". xls ') </script>");


        }


Catch


        {


        }


    }

Related Article

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.