using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.IO;
using System.Data.SqlClient;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = ExcelToDataTable("C:\\zzz.xls", "sheet1");
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string strName = dt.Rows[i][0].ToString();
SqlConnection myConnection = new SqlConnection("Data Source=(local); User=sa; Pwd=1234; Initial Catalog = EFlyer");
SqlDataAdapter myDataAdapter = new SqlDataAdapter();
DataTable myDataTable = new DataTable();
string strCom = @"select emailaccount from currentstudent where name='" + strName + "'";
myDataAdapter.SelectCommand = new SqlCommand(strCom, myConnection);
SqlCommandBuilder myCB = new SqlCommandBuilder(myDataAdapter);
myDataAdapter.Fill(myDataTable);
string strEmail = "";
if (myDataTable.Rows.Count > 0)
{
if (myDataTable.Rows[0]["emailaccount"].ToString() == "" || myDataTable.Rows[0]["emailaccount"].ToString() == null)
{
strEmail = "no address";
}
else
{
strEmail = myDataTable.Rows[0]["emailaccount"].ToString();
}
Response.Write(strName + " " + "GroupZ" + " " + strEmail + "
");
}
else
{
Response.Write(strName + " " + "GroupZ" + " " + " " + "
");
}
}
}
}
public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
string strExcel = "select * from [sheet1$]";
DataSet ds = new DataSet();
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, strSheetName);
conn.Close();
return ds.Tables[strSheetName];
}
}
没有评论:
发表评论