在开发应用中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)
{ } ///
/// 上传excel文件
///
/// 上传的控件名
///
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);
}} ///
/// 删除文件
///
/// 待删除的文件名
private void deletefile(string filename)
{
if (filename != string.empty && file.exists(filename))
{
file.delete(filename);
}
}
}
前台代码
<%@ page language="c#" default.asp教程x.cs" inherits="_default" %>
http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">
无标题页
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔