博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
项目中用到的一个导入Excel的方法
阅读量:4049 次
发布时间:2019-05-25

本文共 3165 字,大约阅读时间需要 10 分钟。

#region 导入试题    ///     /// 导入按钮事件    ///     ///     ///     protected void btnImport_Click(object sender, EventArgs e)    {        try        {            //获取题型            string strQuestionType = ddlQuestionType.SelectedItem.Text.Trim();            //获取表名            string strTableName = GetTableNameBLL.GetTableNameByCourseAndQuestionType(ddlCourse.CourseText.Trim(), strQuestionType, "1");            //检查文件是否存在            if (FileContainer.HasFile == false)//HasFile用来检查FileUpload是否有指定文件            {                Alert("请选择Excel文件!");                return;            }            //获得文件的扩展名            string strFileType = System.IO.Path.GetExtension(FileContainer.FileName).ToString().ToLower();            if (strFileType != ".xls")            {                Alert("文件类型有误,请选择Excel文件!");                return;//当选择的不是Excel文件时,返回            }            string strFileName = DateTime.Now.ToString("yyyymmddhhMMss") + FileContainer.FileName;            //获取Execle文件名  DateTime日期函数            string savePath = Server.MapPath(("~\\Pages\\Excels\\") + strFileName);//Server.MapPath 获得虚拟服务器相对路径            //清理Excel文件夹            ClearFile(Server.MapPath("~\\Pages\\Excels"));            //将上传的文件保存在服务器上            FileContainer.SaveAs(savePath);            //将路径下的Excel文件转换为DataTable类型的数据源            DataTable dt = createDataSource(savePath);            bool flag = BllImportExcel.Import(dt, strQuestionType, strTableName);            //关掉Excel进程,否则导一次试题,内存占用量就越大            Process[] processes = System.Diagnostics.Process.GetProcesses();            Process process;            for (int i = 0; i < processes.Length; i++)            {                process = processes[i];                if (process.ProcessName == "EXCEL")                {                    process.Kill();                }            }            if (flag)            {                Alert("导入成功!");            }            else            {                Alert("导入失败!");            }        }        catch        {            Alert("导入失败");        }    }#endregion     #region 将路径下的Excel文件转换为DataTable类型的数据源    ///     /// 将路径下的Excel文件转换为DataTable类型的数据源    ///     /// 路径    /// 
private DataTable createDataSource(string strPath) { /** * 1、新建连接 * 2、绑定数据源 * 3、导出到Datatable * */ string strCon; strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties=Excel 8.0"; OleDbConnection con = new OleDbConnection(strCon); OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]", con); DataTable dt = new DataTable(); da.Fill(dt); return dt; } #endregion #region 清理Excel文件夹 /// /// 当文件超过5个的时候,清理文件夹 /// /// private void ClearFile(string FilePath) { String[] files = System.IO.Directory.GetFiles(FilePath); if (files.Length > 5) { for (int i = 0; i < 5; i++) { try { System.IO.File.Delete(files[i]); } catch { } } } } #endregion

 

转载地址:http://mhdci.baihongyu.com/

你可能感兴趣的文章
CImg库编译使用.
查看>>
openstack虚拟机创建流程
查看>>
openstack网络总结
查看>>
excel 查找一个表的数据在另一个表中是否存在
查看>>
centos 7 上配置dnsmasq 同时支持ipv4和ipv6的DHCP服务
查看>>
AsyncTask、View.post(Runnable)、ViewTreeObserver三种方式总结frame animation自动启动
查看>>
Android中AsyncTask的简单用法
查看>>
java反编译命令
查看>>
activemq依赖包获取
查看>>
概念区别
查看>>
final 的作用
查看>>
在Idea中使用Eclipse编译器
查看>>
Idea下安装Lombok插件
查看>>
zookeeper
查看>>
Idea导入的工程看不到src等代码
查看>>
技术栈
查看>>
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>