日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

DataSet写入Excel

發(fā)布時間:2025/4/14 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DataSet写入Excel 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

??????? protected void ExportExcel_Bn_Click(object sender, EventArgs e)
??????? {
??????????? FileInfo File = new FileInfo(Server.MapPath("_resourses\\UpExcel\\StudentModel.xls"));//Excel文件模板

??????????? string sql = "select XH as 學(xué)號,XM as 姓名,XB as 性別,YXDM as 院系代碼,ZYDM as 專業(yè)代碼,BJDM as 班級代碼,XJZT as 學(xué)籍狀態(tài),CSNY as 出生年月,SYD as 生源地,JGM as 籍貫,SFZH as 身份證號,MZ as 民族編碼,ZZMM as 政治面貌碼 from T_YXSJZB_XSJBXX where BDPCBH='" + BDPCBH.SelectedValue + "'";
??????????? DataSet ds = DataHelper.GetDataSet(sql);

??????????? string NewAddStudent = Server.MapPath("_resourses\\UpExcel\\StudentOutFull.xls");//填充新文件為Excel文件的名稱

??????????? FileInfo NewFile = new FileInfo(NewAddStudent);//實(shí)例新文件Excel文件

??????????? if (NewFile.Exists)? //判斷新文件是否存在
??????????? {
??????????????? NewFile.Delete(); //刪除文件
??????????????? File.CopyTo(NewAddStudent);//復(fù)制新文件
??????????????? SavetoExcel(NewAddStudent, ds);//把數(shù)據(jù)寫入excel
??????????????? Response.Redirect("_resourses\\UpExcel\\StudentOutFull.xls");


??????????? }
??????????? else
??????????? {
??????????????? File.CopyTo(NewAddStudent);//復(fù)制新文件
??????????????? SavetoExcel(NewAddStudent, ds);//把數(shù)據(jù)寫入excel
??????????????? Response.Redirect("_resourses\\UpExcel\\StudentOutFull.xls");

??????????? }

?

?

?

??????????? //string path = Server.MapPath("_resourses\\UpExcel\\學(xué)生信息");

??????????? //if (File.Exists)
??????????? //{
??????????? //??? ExcelCtrol.DelExcel(Server.MapPath("_resourses\\UpExcel\\學(xué)生信息.xls"));
??????????? //??? ExcelCtrol.ToExcel(ds, path);
??????????? //??? Response.Redirect("_resourses\\UpExcel\\學(xué)生信息.xls");
??????????? //}
??????????? //else
??????????? //{
??????????? //??? ExcelCtrol.ToExcel(ds, path);
??????????? //??? Response.Redirect("_resourses\\UpExcel\\學(xué)生信息.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();//學(xué)號
??????????????????? string xm = ds.Tables[0].Rows[j][1].ToString();//姓名
??????????????????? string xb = ds.Tables[0].Rows[j][2].ToString();//性別
??????????????????? string xy = ds.Tables[0].Rows[j][3].ToString();//院系代碼
??????????????????? string zy = ds.Tables[0].Rows[j][4].ToString();//專業(yè)代碼
??????????????????? string bj = ds.Tables[0].Rows[j][5].ToString();//班級代碼
??????????????????? string xj = ds.Tables[0].Rows[j][6].ToString();//學(xué)籍狀態(tài)
??????????????????? string CSNY = ds.Tables[0].Rows[j][7].ToString();//出生年月
??????????????????? string syd = ds.Tables[0].Rows[j][8].ToString();//生源地
??????????????????? string jg = ds.Tables[0].Rows[j][9].ToString();//籍貫
??????????????????? string sfz = ds.Tables[0].Rows[j][10].ToString();//身份證號
??????????????????? string mz = ds.Tables[0].Rows[j][11].ToString();//民族編碼
??????????????????? string zz = ds.Tables[0].Rows[j][12].ToString();//政治面貌碼
??????????????????? cmd.CommandText = "INSERT INTO [sheet1$] (學(xué)號,姓名,性別,院系代碼,專業(yè)代碼,班級代碼,學(xué)籍狀態(tài),出生年月,生源地,籍貫,身份證號,民族編碼,政治面貌碼) 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("寫入Excel發(fā)生錯誤:" + ex.Message);
??????????? }
??????????? return false;
??????? }

?

?


?

轉(zhuǎn)載于:https://www.cnblogs.com/i799/archive/2008/08/04/1260358.html

總結(jié)

以上是生活随笔為你收集整理的DataSet写入Excel的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。