Protected void exportexcel_bn_click (Object sender, eventargs E)
{
Fileinfo file = new fileinfo (server. mappath ("_ resourses \ upexcel \ studentmodel.xls"); // Excel file template
String SQL = "select XH as student ID, XM as name, XB as gender, yxdm as faculty code, zydm as professional code, bjdm as class code, xjzt as student status, csny as date of birth, Syd as source, jgm as nationality, sfzh as ID number, MZ as ethnic code, zzmm as political appearance code from t_yxsjzb_xsjbxx where bdpcbh = '"+ bdpcbh. selectedvalue + "'";
Dataset DS = datahelper. getdataset (SQL );
String newaddstudent = server. mappath ("_ resourses \ upexcel \ studentoutfull.xls"); // fill in the name of the new Excel File
Fileinfo newfile = new fileinfo (newaddstudent); // the Excel file of the new instance.
If (newfile. exists) // determines whether the new file exists.
{
Newfile. Delete (); // delete an object
File. copyto (newaddstudent); // copy the new file
Savetoexcel (newaddstudent, DS); // write data to excel
Response. Redirect ("_ resourses \ upexcel \ studentoutfull.xls ");
}
Else
{
File. copyto (newaddstudent); // copy the new file
Savetoexcel (newaddstudent, DS); // write data to excel
Response. Redirect ("_ resourses \ upexcel \ studentoutfull.xls ");
}
// String Path = server. mappath ("_ resourses \ upexcel \ student information ");
// If (file. exists)
//{
// Excelctrol. delexcel (server. mappath ("_ resourses \ upexcel \ student information .xls "));
// Excelctrol. toexcel (DS, PATH );
// Response. Redirect ("_ resourses \ upexcel \ student information .xls ");
//}
// Else
//{
// Excelctrol. toexcel (DS, PATH );
// Response. Redirect ("_ resourses \ upexcel \ student information .xls ");
//}
}
Public bool savetoexcel (string path, dataset DS)
{
Try
{
String strconn = "provider = Microsoft. Jet. oledb.4.0;" + "Data Source =" + path + ";" + "extended properties = Excel 8.0 ;";
Oledbconnection conn = new oledbconnection (strconn );
Conn. open ();
System. Data. oledb. oledbcommand cmd = new oledbcommand ();
Cmd. Connection = conn;
Int rowcount = Ds. Tables [0]. Rows. count;
For (Int J = 0; j <rowcount; j ++)
{
String XH = Ds. Tables [0]. Rows [J] [0]. tostring (); // student ID
String XM = Ds. Tables [0]. Rows [J] [1]. tostring (); // name
String XB = Ds. Tables [0]. Rows [J] [2]. tostring (); // gender
String xy = Ds. Tables [0]. Rows [J] [3]. tostring (); // Department Code
String ZY = Ds. Tables [0]. Rows [J] [4]. tostring (); // professional code
String bj = Ds. Tables [0]. Rows [J] [5]. tostring (); // class code
String XJ = Ds. Tables [0]. Rows [J] [6]. tostring (); // student status
String csny = Ds. Tables [0]. Rows [J] [7]. tostring (); // Date of birth
String Syd = Ds. Tables [0]. Rows [J] [8]. tostring (); // Source
String JG = Ds. Tables [0]. Rows [J] [9]. tostring (); // Nationality
String sfz = Ds. Tables [0]. Rows [J] [10]. tostring (); // ID card number
String MZ = Ds. Tables [0]. Rows [J] [11]. tostring (); // ethnic code
String ZZ = Ds. Tables [0]. Rows [J] [12]. tostring (); // political appearance code
Cmd. commandtext = "insert into [sheet1 $] (student ID, name, gender, department code, professional code, class code, student status, date of birth, source of birth, nationality, ID card number, ethnic code, political appearance code) values ('"+ XH +"', '"+ XM +"', '"+ XB +"', '"+ xy + "', '"+ ZY +"', '"+ BJ +"', '"+ XJ +"', '"+ csny +"', '"+ Syd + "', '"+ JG +"', '"+ sfz +"', '"+ MZ +"', '"+ ZZ + "')";
Cmd. executenonquery ();
}
Conn. Close ();
Cmd. Dispose ();
Return true;
}
Catch (system. Data. oledb. oledbexception ex)
{
System. Diagnostics. Debug. writeline ("an error occurred when writing data to an Excel file:" + ex. Message );
}
Return false;
}