欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET下将Excel表格中的数据规则的导入数据库思路分析及实现

程序员文章站 2024-03-05 18:36:07
今天接到新的需求,要求将excel表格中的数据显示在页面上。 我个人分析,首先要将excel中的数据存到数据库中,再进行页面显示,本人菜鸟级别,以前没有做过读取excel数...
今天接到新的需求,要求将excel表格中的数据显示在页面上。
我个人分析,首先要将excel中的数据存到数据库中,再进行页面显示,本人菜鸟级别,以前没有做过读取excel数据,研究了一下(主要是看别人的资料),写一下实现过程,我想写几篇关于excel的,首先是规则的excel数据导入,再有就是不规则的excel数据导入,还有就是根据数据生成excel。

下面开始:将规则的excel导入数据库
首先看一下excel结构,如图:
ASP.NET下将Excel表格中的数据规则的导入数据库思路分析及实现 
这是一个简单的、规整的excel格式,将它导入到数据库中
复制代码 代码如下:

view code
protected void btnimport_click(object sender, eventargs e)
{
if (fileupload1.hasfile == false)//hasfile用来检查fileupload是否有指定文件
{
response.write("<script>alert('请您选择excel文件')</script> ");
return;//当无文件时,返回
}
string isxls = system.io.path.getextension(fileupload1.filename).tostring().tolower();//system.io.path.getextension获得文件的扩展名
if (isxls != ".xls")
{
if(isxls!=".xlsx")
{
response.write("<script>alert('只可以选择excel文件')</script>");
return;//当选择的不是excel文件时,返回
}
}
string filename = fileupload1.filename; //获取execle文件名 datetime日期函数
string savepath = server.mappath(("upfiles\\") + filename);//server.mappath 获得虚拟服务器相对路径
fileupload1.saveas(savepath); //saveas 将上传的文件内容保存在服务器上
dataset ds = excelsqlconnection(savepath, filename,isxls); //调用自定义方法
datarow[] dr = ds.tables[0].select(); //定义一个datarow数组
int rowsnum = ds.tables[0].rows.count;
if (rowsnum == 0)
{
response.write("<script>alert('excel表为空表,无数据!')</script>"); //当excel表为空时,对用户进行提示
}
else
{
for (int i = 0; i < dr.length; i++)
{
//前面除了你需要在建立一个“upfiles”的文件夹外,其他的都不用管了,你只需要通过下面的方式获取excel的值,然后再将这些值用你的方式去插入到数据库里面
string title = dr[i]["标题"].tostring();
string linkurl = dr[i]["链接地址"].tostring();
string categoryname = dr[i]["分类"].tostring();
//response.write("<script>alert('导入内容:" + ex.message + "')</script>");
}
response.write("<script>alert('excle表导入成功!');</script>");
}
}
#region 连接excel 读取excel数据 并返回dataset数据集合
/// <summary>
/// 连接excel 读取excel数据 并返回dataset数据集合
/// </summary>
/// <param name="filepath">excel服务器路径</param>
/// <param name="tablename">excel表名称</param>
/// <returns></returns>
public static system.data.dataset excelsqlconnection(string filepath, string tablename,string isxls)
{
string strcon = "";
if(isxls==".xls")
{
strcon = "provider=microsoft.jet.oledb.4.0;data source=" + filepath + ";extended properties='excel 8.0;hdr=yes;imex=1'";
}
else
{
strcon = "provider=microsoft.ace.oledb.12.0;data source=" + filepath + ";extended properties='excel 12.0;hdr=yes;imex=1'";
}
oledbconnection excelconn = new oledbconnection(strcon);
try
{
string strcom = string.format("select * from [sheet1$]");
excelconn.open();
oledbdataadapter mycommand = new oledbdataadapter(strcom, excelconn);
dataset ds = new dataset();
mycommand.fill(ds, "[" + tablename + "$]");
excelconn.close();
return ds;
}
catch
{
excelconn.close();
return null;
}
}
#endregio

这段代码是在别人的代码的基础上改的,以前只能导入xls格式的,不支持xlsx格式,这两种格式的主要区别在于
xls格式
复制代码 代码如下:

strconn = "provider=microsoft.ace.oledb.12.0;data source='" + serverfilename + "';extended properties='excel 12.0;hdr=yes'";

xlsx格式
复制代码 代码如下:

strconn = "provider=microsoft.jet.oledb.4.0;data source='" + serverfilename + "';extended properties='excel 8.0;hdr=yes;'";

当然了,导入数据库还需要连接数据库,创建结构相同表。