日期:2014-05-17  浏览次数:20658 次

求解asp.net mvc 中上传excel 如何导入到数据库中!!!
如何在mvc中实现excel的导入,并将导入的数据,先存到临时表,然后根据选择性的数据,插入到数据库。。。求各位MVC高手指教!!!最好附加个例子!!源码更好,借鉴研究一下! O(∩_∩)O谢谢各位喽!

------解决方案--------------------
读取excel模板,导出excel的例子:

C# code

        public ActionResult ExportQualifiedExaminees(int serviceid, int funcid, string condition)
        {

            RegistrationBLL bll = new RegistrationBLL();
            IList<QualifiedExamineeEn> list = bll.GetQualifiedExaminees(serviceid, condition);
            if (list == null || list.Count == 0)
            {
                return Alert("没有准考证信息", "~/Views/ExamService/SaveSuccess.aspx", new { controller = "Registration", action = "GetExamineeByPage", serviceid = serviceid, funcid = funcid });
            }

            using (MemoryStream m = bll.ExportQualifiedExaminees(Server.MapPath("~/Resources/考生签到表导出模版.xls"), list1[0].fServiceName, list, Server.MapPath("~/Common/Images/toeic_log.PNG")))
            {
                ExcelExportHandler.ExportFile(m, "application/ms-excel", "UTF-8", "GB2312", "考生签到表.xls");
            }

            return new EmptyResult();
        }

------解决方案--------------------
ExcelExportHandler类的定义:
C# code

public class ExcelExportHandler
    {
        public static void ExportFile(string content, string contentType, string charSet, string encodingName, string outPutFileName)
        {
            byte[] htmlBy = System.Text.Encoding.GetEncoding("GB2312").GetBytes(content);
            MemoryStream stream = new MemoryStream(htmlBy);
            ExportFile(stream, contentType, charSet, encodingName, outPutFileName);
        }

        public static void ExportFile(MemoryStream stream, string contentType, string charSet, string encodingName, string outPutFileName)
        {
           
           
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = contentType;
            HttpContext.Current.Response.Charset = charSet;
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(encodingName);
            String userAgent;
            userAgent = HttpContext.Current.Request.UserAgent;
            if (userAgent != null && userAgent.ToUpper().IndexOf("MSIE") > -1)
            {
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(outPutFileName, System.Text.Encoding.UTF8).Replace("+", "%20"));
                // The browser is Microsoft Internet Explorer Version 6.0.
            }
            else if (userAgent != null && userAgent.ToUpper().IndexOf("MOZILLA") > -1)
            {
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(outPutFileName, System.Text.Encoding.UTF8).Replace("+", "%20"));

            }

            HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.GetEncoding(encodingName);
            stream.WriteTo(HttpContext.Current.Response.OutputStream);
            //HttpContext.Current.Response.End();
        }
    }