1
private string InputExcelFile()//上传按钮事件
2
{
3
string fileNamt = fileUpLoad.Value.Trim();
4
PlanServer PS=new PlanServer();
5
HttpPostedFile UpFile=this.fileUpLoad.PostedFile;
6
if(UpFile==null || UpFile.ContentLength<1 || System.IO.Path.GetExtension(UpFile.FileName).ToLower() != ".txt")
7
return "上传的文件无效!";
8
9
string filePath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) +"\\"+ConfigurationSettings.AppSettings["UploadFilePath"]+"\\PlanExcel";
10
if(!Directory .Exists(filePath))
11
Directory.CreateDirectory(filePath);
12
13
string fileName = Convert.ToString(HttpContext.Current.Session["UserID"]) + "_"
14
+ DateTime.Now.ToString("yyyyMMddHHmmss")
15
+ ".txt";
16
17
try
18
{
19
UpFile.SaveAs(filePath + "\\" + fileName);
20
}
21
catch
22
{
23
return "保存文件失败。";// + ex.Message);
24
}
25
26
try
27
{
28
DataTable dt=PS.readTXT(filePath,fileName);
29
30
dt = CheckDataFromExcel(dt);
31
if(dt == null)
32
return "";
33
else
34
{
35
InsertToDataBase(dt);
36
return "";
37
}
38
}
39
catch (Exception ex)
40
{
41
return ex.Message;
42
}
43
}
private string InputExcelFile()//上传按钮事件2
{3
string fileNamt = fileUpLoad.Value.Trim();4
PlanServer PS=new PlanServer();5
HttpPostedFile UpFile=this.fileUpLoad.PostedFile;6
if(UpFile==null || UpFile.ContentLength<1 || System.IO.Path.GetExtension(UpFile.FileName).ToLower() != ".txt")7
return "上传的文件无效!";8

9
string filePath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) +"\\"+ConfigurationSettings.AppSettings["UploadFilePath"]+"\\PlanExcel";10
if(!Directory .Exists(filePath))11
Directory.CreateDirectory(filePath);12

13
string fileName = Convert.ToString(HttpContext.Current.Session["UserID"]) + "_"14
+ DateTime.Now.ToString("yyyyMMddHHmmss")15
+ ".txt";16

17
try18
{19
UpFile.SaveAs(filePath + "\\" + fileName);20
}21
catch22
{23
return "保存文件失败。";// + ex.Message);24
}25

26
try27
{28
DataTable dt=PS.readTXT(filePath,fileName);29

30
dt = CheckDataFromExcel(dt);31
if(dt == null)32
return "";33
else34
{35
InsertToDataBase(dt);36
return "";37
}38
}39
catch (Exception ex)40
{41
return ex.Message;42
}43
} 1
public DataTable readTXT(string filePath,string fileName)//读取TXT文件内容并返回一个DataTable
2
{
3
DataTable DT=new DataTable();
4
string ConnectionString;
5
string SQLString;
6
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +filePath+"\\;Extended Properties=\"Text;HDR=no;FMT=Delimited\"";
7
SQLString = "select * from " + fileName;
8
System.Data.OleDb.OleDbConnection ConnectionText = new System.Data.OleDb.OleDbConnection();
9
ConnectionText.ConnectionString = ConnectionString;
10
ConnectionText.Open();
11
System.Data.OleDb.OleDbDataAdapter AdapterText = new System.Data.OleDb.OleDbDataAdapter(SQLString, ConnectionText);
12
System.Data.DataTable dt = new System.Data.DataTable();
13
14
for(int k=0;k<15;k++)
15
DT.Columns.Add("a"+k.ToString());
16
string[] arrylist={};
17
18
AdapterText.Fill(dt);
19
if(dt.Rows.Count>0)
20
{
21
22
for(int i=0;i<dt.Rows.Count;i++)
23
{
24
arrylist=dt.Rows[i][0].ToString().Split('\t');
25
DT.Rows.Add(arrylist);
26
}
27
DT.AcceptChanges();
28
}
29
ConnectionText.Close();
30
return DT;
31
}
public DataTable readTXT(string filePath,string fileName)//读取TXT文件内容并返回一个DataTable2
{3
DataTable DT=new DataTable();4
string ConnectionString; 5
string SQLString; 6
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +filePath+"\\;Extended Properties=\"Text;HDR=no;FMT=Delimited\""; 7
SQLString = "select * from " + fileName; 8
System.Data.OleDb.OleDbConnection ConnectionText = new System.Data.OleDb.OleDbConnection(); 9
ConnectionText.ConnectionString = ConnectionString; 10
ConnectionText.Open(); 11
System.Data.OleDb.OleDbDataAdapter AdapterText = new System.Data.OleDb.OleDbDataAdapter(SQLString, ConnectionText); 12
System.Data.DataTable dt = new System.Data.DataTable();13
14
for(int k=0;k<15;k++)15
DT.Columns.Add("a"+k.ToString());16
string[] arrylist={};17
18
AdapterText.Fill(dt);19
if(dt.Rows.Count>0)20
{21

22
for(int i=0;i<dt.Rows.Count;i++)23
{24
arrylist=dt.Rows[i][0].ToString().Split('\t');25
DT.Rows.Add(arrylist);26
} 27
DT.AcceptChanges();28
}29
ConnectionText.Close(); 30
return DT;31
}



浙公网安备 33010602011771号