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