asp.net编程中实现导入xls表格操作的实现代码
2011-03-07 11:04
507 查看
在开发应用中xls文件导入到数据库教程是常用的,下面我们来看这款程序:
using system;
using system.configuration;
using system.data;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.io; public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{ } /// <summary>
/// 上传excel文件
/// </summary>
/// <param name="inputfile">IT泡泡堂(www.3ppt.com)</param>
/// <returns></returns>
private string uploadxls(system.web.ui.htmlcontrols.htmlinputfile inputfile)
{
string orifilename = string.empty;
string uploadfilepath = string.empty;
string modifyfilename = string.empty;
string fileext = "" ;//文件扩展名
int filesize = 0;//文件大小
try
{
if(inputfile.value != string.empty)
{
//得到文件的大小
filesize = inputfile.postedfile.contentlength;
if(filesize == 0 )
{
throw new exception("导入的excel文件大小为0,请检查是否正确!");
}
//得到扩展名
fileext = inputfile.value.substring(inputfile.value.lastindexof(".")+1);
if(fileext.tolower() != "xls")
{ throw new exception("你选择的文件格式不正确,只能导入excel文件!");
}
//路径
uploadfilepath = server.mappath("~/");
//新文件名
modifyfilename = system.guid.newguid().tostring();
modifyfilename += "."+inputfile.value.substring(inputfile.value.lastindexof(".")+1);
//判断是否有该目录
system.io.directoryinfo dir = new
system.io.directoryinfo(uploadfilepath);
if (!dir.exists)
{
dir.create();
}
orifilename = uploadfilepath+modifyfilename;
//如果存在,删除文件
if(file.exists(orifilename))
{
file.delete(orifilename);
}
// 上传文件
inputfile.postedfile.saveas(orifilename);
}
else
{
throw new exception("请选择要导入的excel文件!"); }
}
catch(exception ex)
{
throw ex;
}
return orifilename;
} protected void btnimport_click(object sender, system.eventargs e)
{ try
{
string strfilename = this.uploadxls(this.comfile);
string strconn = "provider=microsoft.jet.oledb.4.0;data
source=" + strfilename + ";extended properties='excel
8.0;hdr=no;imex=1'";
oledbconnection conn = new oledbconnection(strconn);
oledbdataadapter mycommand = new oledbdataadapter("select * from [sheet1$]", strconn);
dataset mydataset = new dataset();
mycommand.fill(mydataset);
string str = "";
}
catch (exception ex)
{
throw ex;
}
finally
{
deletefile(strfilename);
}
} /// <summary>
/// 删除文件
/// </summary>
/// <param name="filename">待删除的文件名</param>
private void deletefile(string filename)
{
if (filename != string.empty && file.exists(filename))
{
file.delete(filename);
}
}
}
前台代码
<%@ page language="c#" default.asp教程x.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"<a
href="http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd</a>">
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
请选择要导入的文件:<input id="fileexcel" style="width: 300px" type="file" size="42" name="filephoto" runat="server" />
<asp:button id="btnimport" text="导 入" css教程class="button" runat="server" onclick="btnimport_click"></asp:button><br />
<asp:label id="lblmessage" runat="server" font-bold="true" forecolor="red"></asp:label>
</form>
</body>
</html>
using system;
using system.configuration;
using system.data;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.io; public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{ } /// <summary>
/// 上传excel文件
/// </summary>
/// <param name="inputfile">IT泡泡堂(www.3ppt.com)</param>
/// <returns></returns>
private string uploadxls(system.web.ui.htmlcontrols.htmlinputfile inputfile)
{
string orifilename = string.empty;
string uploadfilepath = string.empty;
string modifyfilename = string.empty;
string fileext = "" ;//文件扩展名
int filesize = 0;//文件大小
try
{
if(inputfile.value != string.empty)
{
//得到文件的大小
filesize = inputfile.postedfile.contentlength;
if(filesize == 0 )
{
throw new exception("导入的excel文件大小为0,请检查是否正确!");
}
//得到扩展名
fileext = inputfile.value.substring(inputfile.value.lastindexof(".")+1);
if(fileext.tolower() != "xls")
{ throw new exception("你选择的文件格式不正确,只能导入excel文件!");
}
//路径
uploadfilepath = server.mappath("~/");
//新文件名
modifyfilename = system.guid.newguid().tostring();
modifyfilename += "."+inputfile.value.substring(inputfile.value.lastindexof(".")+1);
//判断是否有该目录
system.io.directoryinfo dir = new
system.io.directoryinfo(uploadfilepath);
if (!dir.exists)
{
dir.create();
}
orifilename = uploadfilepath+modifyfilename;
//如果存在,删除文件
if(file.exists(orifilename))
{
file.delete(orifilename);
}
// 上传文件
inputfile.postedfile.saveas(orifilename);
}
else
{
throw new exception("请选择要导入的excel文件!"); }
}
catch(exception ex)
{
throw ex;
}
return orifilename;
} protected void btnimport_click(object sender, system.eventargs e)
{ try
{
string strfilename = this.uploadxls(this.comfile);
string strconn = "provider=microsoft.jet.oledb.4.0;data
source=" + strfilename + ";extended properties='excel
8.0;hdr=no;imex=1'";
oledbconnection conn = new oledbconnection(strconn);
oledbdataadapter mycommand = new oledbdataadapter("select * from [sheet1$]", strconn);
dataset mydataset = new dataset();
mycommand.fill(mydataset);
string str = "";
}
catch (exception ex)
{
throw ex;
}
finally
{
deletefile(strfilename);
}
} /// <summary>
/// 删除文件
/// </summary>
/// <param name="filename">待删除的文件名</param>
private void deletefile(string filename)
{
if (filename != string.empty && file.exists(filename))
{
file.delete(filename);
}
}
}
前台代码
<%@ page language="c#" default.asp教程x.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"<a
href="http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd</a>">
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
请选择要导入的文件:<input id="fileexcel" style="width: 300px" type="file" size="42" name="filephoto" runat="server" />
<asp:button id="btnimport" text="导 入" css教程class="button" runat="server" onclick="btnimport_click"></asp:button><br />
<asp:label id="lblmessage" runat="server" font-bold="true" forecolor="red"></asp:label>
</form>
</body>
</html>
相关文章推荐
- xls表格导入数据库功能实例代码
- jquery 表格分页等操作实现代码(pagedown,pageup)
- BootStrap Fileinput插件和Bootstrap table表格插件相结合实现文件上传、预览、提交的导入Excel数据操作步骤
- poi导入xls表格老版本问题解决,写入文件,删除文件操作
- 结合bootstrap fileinput插件和Bootstrap-table表格插件,实现文件上传、预览、提交的导入Excel数据操作流程
- jquery 操作表格实现代码(多种操作打包)
- xls表格导入数据库功能实例代码
- Javascript 表格操作实现代码
- Javascript 表格操作实现代码
- xls表格导入数据库功能实例代码
- JS小功能(操作Table--动态添加删除表格及数据)实现代码
- jquery获取指定表格的指定列的值 以及 操作radio,checkbox,select表单操作实现代码
- jquery 表格分页等操作实现代码(pagedown,pageup)
- JS小功能(操作Table--动态添加删除表格及数据)实现代码
- JS小功能(操作Table--动态添加删除表格及数据)实现代码
- 用jquery代码实现表格行列的增删操作(每行编号随之修改)
- jquery 操作表格实现代码(多种操作打包)
- JQuery实现表格中相同单元格合并示例代码
- jQuery实现用户信息表格的添加、删除操作