How to export database table data to Excel

Source: Internet
Author: User

1. First, there is a need for a npoi

2. The next code

1 Private voidButton1_Click (Objectsender, EventArgs e)2 {3 //1. Read data via ADO4 stringsql ="SELECT * from Asrs_f1";5 using(SqlDataReader reader =sqlhelper.executereader (SQL, CommandType.Text))6 {7 //If you read the data8 if(reader. HasRows)9 {TenIworkbook wk =NewHssfworkbook (); OneIsheet sheet = wk. Createsheet ("asrs_f1"); A  - #regionCreate first row, set column name - //-------------------------------------------------- the //Create first row, first row indicates column name -IRow Rowhead = sheet. CreateRow (0); - //each column of the loop query -  for(intCol =0; Col < reader. FieldCount; col++) + { - Rowhead.createcell (COL). Setcellvalue (reader. GetName (col)); + } A //-------------------------------------------------- at #endregion -  - intRindex =1; - //The following is the creation of data rows -  while(reader. Read ()) - { in //ID, Position, AName, Acode, Astate, Abatch, Adatetime, Bname, Bcode, Bstate, Bbatch, Bdatetime, ismoving , Istype -IRow CurrentRow =sheet. CreateRow (rindex); torindex++; + intId = reader. GetInt32 (0); - stringPosition = reader. GetString (1); the stringAName = reader. GetString (2); * stringAcode = reader. GetString (3); $ stringAstate = reader. GetString (4);Panax Notoginseng stringAbatch =reader. IsDBNull (5)?NULL: Reader. GetString (5); -Datetime? Lockdate = reader. IsDBNull (6) ?NULL: (DateTime?) Reader. GetDateTime (6); the stringbname = reader. GetString (7); + stringBcode = reader. GetString (8); A stringBstate = reader. GetString (9); the stringBbatch = reader. IsDBNull (Ten)?NULL: Reader. GetString (Ten); +Datetime? Lockdates = reader. IsDBNull ( One) ?NULL: (DateTime?) Reader. GetDateTime ( One); - stringismoving = reader. GetString ( A); $ stringIstype = reader. GetString ( -); $  -Currentrow.createcell (0). Setcellvalue (Id); -Currentrow.createcell (1). Setcellvalue (Position); theCurrentrow.createcell (2). Setcellvalue (AName); -Currentrow.createcell (3). Setcellvalue (Acode);WuyiCurrentrow.createcell (4). Setcellvalue (astate); theCurrentrow.createcell (5). Setcellvalue (Abatch); - if(Lockdate = =NULL) Wu { - //if it is a null value, then just like Excel writes a cell, the type of the cell is blank AboutCurrentrow.createcell (6). Setcelltype (Celltype.blank); $ } - Else - { -  A //Create a cell +Icell celllockdate = Currentrow.createcell (6); the  - //Create a cell style $Icellstyle CellStyle =wk. Createcellstyle (); theCellstyle.dataformat = Hssfdataformat.getbuiltinformat ("m/d/yy h:mm"); the //Set Current cell apply CellStyle style theCelllockdate.cellstyle =CellStyle; the  -  in Celllockdate.setcellvalue (DateTime) lockdate); the } theCurrentrow.createcell (7). Setcellvalue (bname); AboutCurrentrow.createcell (8). Setcellvalue (Bcode); theCurrentrow.createcell (9). Setcellvalue (bstate); theCurrentrow.createcell (Ten). Setcellvalue (Bbatch); the  + if(Lockdates = =NULL) - { the //if it is a null value, then just like Excel writes a cell, the type of the cell is blankBayiCurrentrow.createcell ( One). Setcelltype (Celltype.blank); the } the Else - { -  the //Create a cell theIcell celllockdate = Currentrow.createcell ( One); the  the //Create a cell style -Icellstyle CellStyle =wk. Createcellstyle (); theCellstyle.dataformat = Hssfdataformat.getbuiltinformat ("m/d/yy h:mm"); the //Set Current cell apply CellStyle style theCelllockdate.cellstyle =CellStyle;94  the  the Celllockdate.setcellvalue (DateTime) lockdates); the }98Currentrow.createcell ( A). Setcellvalue (ismoving); AboutCurrentrow.createcell ( -). Setcellvalue (istype); - }101 102 //Write103 using(FileStream fswrite = File.openwrite ("semi-Finished inventory table. xls"))104 { the wk. Write (fswrite);106 }107Label1. Text ="Write Success! "+ DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss");108 }109 Else the {111Label1. Text ="no data was queried"; the }113 } the  the}

How to export database table data to Excel

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.