Excel、记事本数据导入到数据库的实现方法
文件示例:
excel:
记事本:
前台代码:
<div class="tab-content detail" id="divsecond" runat="server" visible="false">
<fieldset >
<p>
<label>
选择文件</label>
<asp:fileupload id="fileupload1" runat="server" /><asp:requiredfieldvalidator
id="requiredfieldvalidator1" runat="server" errormessage="请选择要提交的excel文件" controltovalidate="fileupload1"></asp:requiredfieldvalidator>
</p>
<p>
<label>文件示例</label><img src="../images/ex_excel.jpg" />
</p>
</fieldset>
<div class="clear"></div>
<div class="clear"></div>
<div style="margin-left:200px">
<asp:button id="btnexcel" runat="server" text="提 交" cssclass="button"
onclick="btnexcel_click" />
<input class="button" type="button" value="返 回" onclick="javascript:window.location.href='cartlist.aspx'" />
</div>
</div>
<div class="tab-content detail" id="divthird" runat="server" visible="false">
<fieldset >
<p>
<label>
选择文件</label>
<asp:fileupload id="fileupload2" runat="server" /> <asp:requiredfieldvalidator
id="requiredfieldvalidator2" runat="server" errormessage="请选择要提交的ttxt文件"
controltovalidate="fileupload2"></asp:requiredfieldvalidator>
</p>
<p>
<label>文件示例</label><img src="../images/ex_txt.jpg" />
</p>
</fieldset>
<div class="clear"></div>
<div class="clear"></div>
<div style="margin-left:200px">
<asp:button id="btnnotepad" runat="server" text="提 交" cssclass="button"
onclick="btnnotepad_click" />
<input class="button" type="button" value="返 回" onclick="javascript:window.location.href='cartlist.aspx'" />
</div>
</div>
后台代码
//excel上传
protected void btnexcel_click(object sender, eventargs e)
{
string backstr= uploadfile(fileupload1, "excel",1);
if (backstr == "-1")
{
//这里是放返回消息的,改为对应放消息的方式就行了,js这个类就不上传了
js.showsuremsgbox(this.page,"请选择要提交的excel文件");
return;
}
else if (backstr == "-2")
{
js.showsuremsgbox(this.page, "请选择.xls或.xlsx类型文件");
return;
}
else
{
string url = backstr; //绝对路径
datatable dt = exceltodataset(url);
if (dt.rows.count > 0)
{
for (int i = 0; i < dt.rows.count; i++)
{
//读取每行数据
string phonenum= dt.rows[i][0].tostring();
}
}
else
{
js.showsuremsgbox(this.page, "文件内容为空");
}
file.delete(url); //删除上传的文件
}
}
//记事本上传
protected void btnnotepad_click(object sender, eventargs e)
{
string backstr = uploadfile(fileupload2, "txt",2);
if (backstr == "-1")
{
js.showsuremsgbox(this.page, "请选择要提交的txt文件");
return;
}
else if (backstr == "-2")
{
js.showsuremsgbox(this.page, "请选择.txt类型文件");
return;
}
else
{
string url = backstr; //绝对路径
datatable dt = readtxt(url);
if (dt.rows.count > 0)
{
for (int i = 0; i < dt.rows.count; i++)
{
//读取每行数据
string phonenum= dt.rows[i][0].tostring();
}
}
else
{
js.showsuremsgbox(this.page, "文件内容为空");
}
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 + "/");//图片存储文件夹路径,需要按照不同的需要进行相应的修改
if (!directory.exists(fpath))//查看存储路径的文件是否存在
{
directory.createdirectory(fpath); //创建文件夹,并上传文件
}
string time = datetime.now.tostring("yyyymmddhhmmssfff");//使用时间定义上传图片的名字
string picturename = time + fileextension;
string newfilepath = fpath + picturename; //文件保存路径
fileuploadname.saveas(newfilepath);
return newfilepath; //绝对路径
}
else
{
return "-1"; //没有文件
}
}
//读取excel数据
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的架构,包括各个sheet表的名称,类型,创建时间和修改时间等
datatable dtsheetname = conn.getoledbschematable(oledbschemaguid.tables, new object[] { null, null, null, "table" });
//包含excel中表名的字符串数组
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();
//从指定的表明查询数据,可先把所有表明列出来供用户选择
string strexcel = "select * from [" + strtablenames[0] + "]";
mycommand = new oledbdataadapter(strexcel, strcon);
mycommand.fill(dt);
conn.close();
return dt;
}
//读取记事本数据
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;
}
下一篇: java九九乘法表示例