//Excel上傳
protected void btnExcel_Click(object sender, EventArgs e)
{
string backStr= UploadFile(FileUpload1, "Excel",1);
if (backStr == "-1")
{
//這里是放返回消息的,改為對(duì)應(yīng)放消息的方式就行了,Js這個(gè)類就不上傳了
Js.ShowSureMsgBox(this.Page,"請(qǐng)選擇要提交的Excel文件");
return;
}
else if (backStr == "-2")
{
Js.ShowSureMsgBox(this.Page, "請(qǐng)選擇.xls或.xlsx類型文件");
return;
}
else
{
string url = backStr; //絕對(duì)路徑
DataTable dt = ExcelToDataSet(url);
if (dt.Rows.Count > 0)
{
for (int i = 0; i dt.Rows.Count; i++)
{
//讀取每行數(shù)據(jù)
string phoneNum= dt.Rows[i][0].ToString();
}
}
else
{
Js.ShowSureMsgBox(this.Page, "文件內(nèi)容為空");
}
File.Delete(url); //刪除上傳的文件
}
}
//記事本上傳
protected void btnNotepad_Click(object sender, EventArgs e)
{
string backStr = UploadFile(FileUpload2, "TXT",2);
if (backStr == "-1")
{
Js.ShowSureMsgBox(this.Page, "請(qǐng)選擇要提交的TXT文件");
return;
}
else if (backStr == "-2")
{
Js.ShowSureMsgBox(this.Page, "請(qǐng)選擇.txt類型文件");
return;
}
else
{
string url = backStr; //絕對(duì)路徑
DataTable dt = ReadTXT(url);
if (dt.Rows.Count > 0)
{
for (int i = 0; i dt.Rows.Count; i++)
{
//讀取每行數(shù)據(jù)
string phoneNum= dt.Rows[i][0].ToString();
}
}
else
{
Js.ShowSureMsgBox(this.Page, "文件內(nèi)容為空");
}
File.Delete(url); //刪除上傳的文件
}
}
//上傳文件
public string UploadFile(FileUpload FileUploadName, string varfilename,int type)
{
if (FileUploadName.HasFile)//判斷是否有上傳文件
{
string fileExtension = System.IO.Path.GetExtension(FileUploadName.FileName).ToLower();//獲取文件的后綴名
if (type == 1)
{
if (fileExtension != ".xls" fileExtension != ".xlsx")
{
return "-2";
}
}
if (type == 2)
{
if (fileExtension != ".txt")
{
return "-2";
}
}
string fpath = System.Web.HttpContext.Current.Server.MapPath("/Manager/Uploadfiles/" + varfilename + "/");//圖片存儲(chǔ)文件夾路徑,需要按照不同的需要進(jìn)行相應(yīng)的修改
if (!Directory.Exists(fpath))//查看存儲(chǔ)路徑的文件是否存在
{
Directory.CreateDirectory(fpath); //創(chuàng)建文件夾,并上傳文件
}
string time = DateTime.Now.ToString("yyyyMMddhhmmssfff");//使用時(shí)間定義上傳圖片的名字
string picturename = time + fileExtension;
string newFilePath = fpath + picturename; //文件保存路徑
FileUploadName.SaveAs(newFilePath);
return newFilePath; //絕對(duì)路徑
}
else
{
return "-1"; //沒(méi)有文件
}
}
//讀取Excel數(shù)據(jù)
public DataTable ExcelToDataSet(string filename)
{
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filename + ";Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection(strCon);
conn.Open();
//返回Excel的架構(gòu),包括各個(gè)sheet表的名稱,類型,創(chuàng)建時(shí)間和修改時(shí)間等
DataTable dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
//包含excel中表名的字符串?dāng)?shù)組
string[] strTableNames = new string[dtSheetName.Rows.Count];
for (int k = 0; k dtSheetName.Rows.Count; k++)
{
strTableNames[k] = dtSheetName.Rows[k]["TABLE_NAME"].ToString();
}
OleDbDataAdapter myCommand = null;
DataTable dt = new DataTable();
//從指定的表明查詢數(shù)據(jù),可先把所有表明列出來(lái)供用戶選擇
string strExcel = "select * from [" + strTableNames[0] + "]";
myCommand = new OleDbDataAdapter(strExcel, strCon);
myCommand.Fill(dt);
conn.Close();
return dt;
}
//讀取記事本數(shù)據(jù)
public DataTable ReadTXT(string dirTXT)
{
StreamReader objReader = new StreamReader(dirTXT);
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("DN", System.Type.GetType("System.String"));
string sLine = "";
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null !sLine.Equals(""))
{
DataRow dr = dt.NewRow();
dr[0] = sLine;
dt.Rows.Add(dr);
}
}
objReader.Close();
return dt;
}