C #. Net:excel npoi Import and Export Operation Tutorial database table information data export to an Excel file and write to disk sample sharing

Source: Internet
Author: User

 String sql = @ "SELECT * from T_excel";

———————————————— DataTable Star ————————————————
DataTable dt = sqlhelper.executedatatable (sql);
if (dt. Rows.Count > 0)
        {
Create a workbook
Iworkbook workbook = new Hssfworkbook ();
Create a table
isheet sheet = workbook. Createsheet ("Dbtoexcel");
IRow row0 = sheet. CreateRow (0);
row0. Createcell (0). Setcellvalue ("ID information");
row0. Createcell (1). Setcellvalue ("name Information");
row0. Createcell (2). Setcellvalue ("note information");
for (int r = 0; r < dt. Rows.Count; r++)
            {
Create rows to accept DataTable rows of data
IRow row = sheet. CreateRow (r + 1);
row. Createcell (0). Setcellvalue ((int) dt. rows[r]["Id"]);
row. Createcell (1). Setcellvalue (dt. rows[r]["Name"]. ToString ());
row. Createcell (2). Setcellvalue (dt. rows[r]["Remarks"]. ToString ());

}

———————————————— DataTable END ————————————————

———————————————— SqlDataReader ————————————————
//SqlDataReader reader = sqlhelper.executereader (sql);
//if (reader. HasRows)
//{
Create a workbook
//Iworkbook workbook = new Hssfworkbook ();
Create a worksheet
//isheet sheet = workbook. Createsheet ("Dbtoexcel");
//int rowIndex = 0;
//While (reader. Read ())
//{
A row is created by reading a single piece of data
//IRow row = sheet. CreateRow (rowIndex);
Record number of Read data rows
//rowindex++;
//row. Createcell (0). Setcellvalue (reader. GetInt32 (0));
//row. Createcell (1). Setcellvalue (reader. GetString (1));
//row. Createcell (2). Setcellvalue (reader. GetString (2));
//    }

———————————————— SqlDataReader ————————————————
using (FileStream Saveurl = File.openwrite (@ "C:\Users\Administrator\Desktop\112.xls"))
            {
workbook. Write (Saveurl);
Title = "Data has been exported!";
            }


}
Else
        {
Title = "No data exported!" ";
        }

String sql = @ "SELECT * from T_excel";
DataTable dt = sqlhelper.executedatatable (SQL);
if (dt. Rows.Count > 0)
{
Create a workbook
Iworkbook workbook = new Hssfworkbook ();
Create a table
Isheet sheet = workbook. Createsheet ("Dbtoexcel");
IRow row0 = sheet. CreateRow (0);
Row0. Createcell (0). Setcellvalue ("ID information");
Row0. Createcell (1). Setcellvalue ("name Information");
Row0. Createcell (2). Setcellvalue ("note information");
for (int r = 0; r < dt. Rows.Count; r++)
{
Create rows to accept DataTable rows of data
IRow row = sheet. CreateRow (r + 1);
Row. Createcell (0). Setcellvalue ((int) dt. rows[r]["Id"]);
Row. Createcell (1). Setcellvalue (dt. rows[r]["Name"]. ToString ());
Row. Createcell (2). Setcellvalue (dt. rows[r]["Remarks"]. ToString ());

}


SqlDataReader reader = sqlhelper.executereader (SQL);
if (reader. HasRows)
//{
Create a workbook
Iworkbook workbook = new Hssfworkbook ();
Create a worksheet
Isheet sheet = workbook. Createsheet ("Dbtoexcel");
int rowIndex = 0;
while (reader. Read ())
//    {
A row is created by reading a single piece of data
IRow row = sheet. CreateRow (RowIndex);
Record number of Read data rows
rowindex++;
Row. Createcell (0). Setcellvalue (reader. GetInt32 (0));
Row. Createcell (1). Setcellvalue (reader. GetString (1));
Row. Createcell (2). Setcellvalue (reader. GetString (2));
//    }
using (FileStream Saveurl = File.openwrite (@ "C:\Users\Administrator\Desktop\112.xls"))
{
Workbook. Write (Saveurl);
Title = "Data has been exported!";
}


}
Else
{
Title = "No data exported!" ";
}

C #. Net:excel npoi Import and Export Operation Tutorial database table information data export to an Excel file and write to disk sample sharing

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.