Excel数据导入___你hold住么(一)

        最近小编跟着团队一起开发ITOO3.0高校云平台项目,其中的收获是不言而喻滴,在项目中有个导入功能:导入学生信息;导入班级信息:导入教学楼信息等,在不知多少次的尝试之下,成功实现功能。

框架分析

将保存在Excel表格的信息导入到SQL数据表中

        详细解释一下

        -第一步:在MVC框架的Client端新建文件夹
Excel模板

        - 第二步:通过NPOI文件流(具体的专业名称不知道是叫啥,姑且称文件流)将保存在Client的Excel文件流传到WCF框架的Server端

        - 第三步:在Server端中,新建文件夹保存Excel模板的xml文件
这里写图片描述
        Server端中的XML文件起到解析的作用,从而将Excel 中的数据插入Insert到数据表中


代码设计

        上传Excel到Server
* Controller

private readonly IUploadFile upLoadService1 = ServiceFactory.GetUploadFileService();
#region ImportFlowBatch()+批量导入流程-徐露-2015年7月8日10:39:02
///
/// 批量导入流程
///
///
public ActionResult ImportFlowBatch()
{
#region 文件验证以及上传到指定文件夹 Client端
HttpPostedFileBase file = Request.Files[“files”];
string strFileName;
string strSavePath;
string ClientPath = AppDomain.CurrentDomain.BaseDirectory + “File\UpFile\”;
string strPaperId = “1”;
//这个是Client端的文件保存路径

        if (file == null || file.ContentLength <= 0)
        {
            ViewBag.error = "文件不能为空";
            return View();
        }
        else
        {
            string strFilename = Path.GetFileName(file.FileName);
            int intFilesize = file.ContentLength;//获取上传文件的大小单位为字节byte
            string fileEx = System.IO.Path.GetExtension(strFilename);//获取上传文件的扩展名
            string strNoFileName = System.IO.Path.GetFileNameWithoutExtension(strFilename);//获取无扩展名的文件名
            int Maxsize = 4000 * 1024;//定义上传文件的最大空间大小为4M
            string FileType = ".xls,.xlsx";//定义上传文件的类型字符串

            strFileName = strNoFileName + DateTime.Now.ToString("yyyyMMddhhmmss") + fileEx;
            if (!FileType.Contains(fileEx))
            {
                ViewBag.error = "文件类型不对,只能导入xls和xlsx格式的文件";
                //return View();
            }
            if (intFilesize >= Maxsize)
            {
                ViewBag.error = "上传文件超过4M,不能上传";
                //return View();
            }
            strSavePath = Path.Combine(ClientPath, strFileName);
            file.SaveAs(strSavePath);
        }
        #endregion

        #region 将Client端上传的文件  上传到Server端
        FileUploadMessage myFileMessage = new FileUploadMessage();
        string strDataFileName = file.FileName;
        myFileMessage.FileName = strDataFileName;//文件名
        string CientPathName = ClientPath + strFileName;
        using (FileStream fs = System.IO.File.OpenRead(CientPathName))
        {
            myFileMessage.FileData = fs;
            try
            {
                upLoadService1.UploadFileMethod(myFileMessage);
            }
            catch { }
            //关闭流
            fs.Close();
        }
        #endregion
        string[] HeadName = { "流程ID", "流程名称", "优先级", "是否启用(启用1,未启用0)", "流程Url", "时间戳", "是否删除" };

        //调用执行 写数据库
        if(upLoadService1.ServiceReadFile (strDataFileName ,strPaperId )==null) {

         return RedirectToAction("Index","ImportStudent");
        }
        else 
        {
            DataTable table = upLoadService1 .ServiceReadFile (strDataFileName ,strPaperId )[0];
            return File (Export .ExportManager .ExportExcel (table,HeadName ),"application/vnd.ms-excel", "流程导入错误列表" + ".xls");

        }           
    }
    #endregion

         *在Service中读取Excel文件,写入数据库
需要加的三个:IUploadFile(接口)、UploadFile(实现类)、以及ServiceFactory(工厂)
        篇幅限制,具体代码不再粘贴,小编会具体上传Demo,另行下载

        *XML配置
        针对每一个导入的Excel都需要一个相应的XML配置文件,以便底层方法能够解析获取
        XML应统一放置在某一路径下,具体路径配置如下:

<appConfig>下添加节点
        <add key="ExcelImportXMLPath" value="Models/ImportConfigXML"/>
即将配置文件存放路径放在了Service层的Models/ImportConfigXML文件夹下。
命名:各系统可根据具体情况具体命名,调用方法时需要传入XML文件名称

        具体XML配置可参考:

<?xml version="1.0" encoding="utf-8" ?>
<Excel name="导入流程模板">
  <Sheet name="流程" table="FreshFlowEntity" primaryKey="FlowID" pkType="guid">

    <Column name="流程名称" field="Name">
      <DataType>string</DataType>
      <ForeignKey isExist="false"></ForeignKey>
    </Column>
    <Column name="优先级" field="Sort" isNecessary="true">
      <DataType>int</DataType>
      <ForeignKey isExist="false"></ForeignKey>
    </Column>
    <Column name="是否启用(启用1,未启用0)" field="IsUse" isVerifyRepeat="false"  >
      <DataType>int</DataType>
      <ForeignKey isExist="false"></ForeignKey>
    </Column>
    <Column name="流程Url" field="Url" isVerifyRepeat="false">
      <DataType>string</DataType>
      <ForeignKey isExist="false"></ForeignKey>
    </Column>
    <Column name="时间戳" field="TimeSpan" isNecessary="false" isVerifyRepeat="false" >
      <DataType>DateTime</DataType>
      <ForeignKey isExist="false"></ForeignKey>
    </Column>
    <Column name="是否删除" field="IsDelete" isVerifyRepeat="false">
      <DataType>int</DataType>
      <ForeignKey isExist="false"></ForeignKey>
    </Column>
  </Sheet>
</Excel>

        以上就是做导入的基本流程,那是不是完好敲出代码就能正常运行呢,请见小编下文分析 link text

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值