批量导出

        #region 批量导出 
        /// <summary>
        /// 批量导出
        /// </summary>
        /// <param name="IDs">IDs集合</param>
        /// <param name="type">导出类型(支持多个)</param>
        /// <returns></returns>
        public ActionResult BatchExport(string IDs, int type)
        {
            string FileName = string.Empty;
            if (type == 2)
                FileName = "xxx.html";
            else if (type == 3)
                FileName = "xxx.html";
            else
                FileName = "xxx.html";

            //时间戳
            string timestamp = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
            //项目路径
            string RootDir = HttpRuntime.AppDomainAppPath;
            //模板文件路径
            string FileDir = RootDir + "Template\\";
            //临时存放路径
            string TempDir = RootDir + "Download\\" + timestamp + "\\";
            //Zip存放路径
            string ZipDir = RootDir + "Download\\" + timestamp + ".zip";
            //创建临时文件夹
            if (!Directory.Exists(TempDir))
            {
                Directory.CreateDirectory(TempDir);
            }
            //内存流
            MemoryStream ms = new MemoryStream();
            //获取模板内容
            string templateContent = System.IO.File.ReadAllText(FileDir + FileName, System.Text.Encoding.Default);

            foreach (var item in IDs.Split(new char[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries))
            {
                //导出的DOC文件名
                string exportFileName = string.Empty;
                //填充变量键值对
                IDictionary<string, string> dic = new Dictionary<string, string>();
                if (type == 2)
                    dic = SetDic(item, ref exportFileName);
                else if (type == 3)
                   dic=...    
		else
                    dic = ...

                if (dic == null)
                {
                    continue;
                }
                //填充后的文本
                string content = CommonMethod.ReplaceContent(templateContent, dic);
                //HTML文件保存在临时文件夹内
                System.IO.File.WriteAllText(TempDir + exportFileName + ".doc", content, System.Text.Encoding.Default);
            }
            //压缩文件夹(方法见Zip压缩)
            ZipDir(TempDir, ZipDir);
            //文件推送下载
            return File(System.IO.File.ReadAllBytes(ZipDir), "application/octet-stream", timestamp + ".zip");
        }

        /// <summary>
        ///  赋值
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        private IDictionary<string, string> SetDic(string Id, ref string fileName)
        {
            IDictionary<string, string> dic = new Dictionary<string, string>();
            //根据ID获取实体
	    Entity Info = new Entity();
            if (info== null)
            {
                return null;
            }

            fileName = "xxxxx(" +info.xxxx + ")的内容";

            dic.Add("属性1", info.属性);
            ......
            return dic;
        }

        #endregion

标签替换

	#region 替换标签

        /// <summary>
        /// 替换内容
        /// </summary>
        /// <param name="sourceContent"></param>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static string ReplaceContent(string source, IDictionary<string, string> dic)
        {
            while (source.IndexOf("{#") >= 0 && source.IndexOf("#}") > source.IndexOf("{#"))
            {
                string key = source.Substring(source.IndexOf("{#") + 2, source.IndexOf("#}") - source.IndexOf("{#") - 2);
                if (dic.ContainsKey(key))
                {
                    source = source.Replace("{#" + key + "#}", dic[key]);
                }
                else
                {
                    source = source.Replace("{#" + key + "#}", "");
                }
            }
            return source;
        }
        #endregion

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaEasyExcel是一款基于Java的Excel操作工具,可以方便地实现Excel的读取、写入和导出等功能。下面分别介绍JavaEasyExcel如何实现Excel批量导入数据库和批量导出。 ## Excel批量导入数据库 ### 1. 添加依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.6</version> </dependency> ``` ### 2. 创建实体类 创建一个实体类,用于保存Excel中的数据。例如,我们要导入一个学生信息表,可以创建一个Student类,包含以下字段: ```java public class Student { private String name; private Integer age; private String gender; private String phone; } ``` ### 3. 创建监听器 创建一个监听器,继承AnalysisEventListener类,并重写invoke方法,用于处理Excel中的每一行数据。例如: ```java public class StudentListener extends AnalysisEventListener<Student> { private List<Student> dataList = new ArrayList<>(); @Override public void invoke(Student student, AnalysisContext analysisContext) { dataList.add(student); // 每隔5条存储一次数据库,实际项目中可以适当调整 if (dataList.size() >= 5) { saveData(); dataList.clear(); } } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { // 所有数据解析完成 saveData(); } private void saveData() { // 将 dataList 存储到数据库中 // ... } } ``` ### 4. 执行导入 调用EasyExcel的read方法,即可实现Excel批量导入数据库。例如: ```java String filePath = "student.xlsx"; ExcelReader excelReader = EasyExcel.read(filePath, Student.class, new StudentListener()).build(); excelReader.read(); excelReader.finish(); ``` ## Excel批量导出 ### 1. 添加依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.6</version> </dependency> ``` ### 2. 创建数据源 创建一个存储Excel数据的数据源。例如,我们要导出一个学生信息表,可以创建一个List<Student>,包含若干个Student对象。 ### 3. 创建表头 创建一个List<List<String>>,用于存储Excel的表头信息。例如: ```java List<List<String>> headList = new ArrayList<>(); List<String> head0 = new ArrayList<>(); head0.add("姓名"); List<String> head1 = new ArrayList<>(); head1.add("年龄"); List<String> head2 = new ArrayList<>(); head2.add("性别"); List<String> head3 = new ArrayList<>(); head3.add("电话"); headList.add(head0); headList.add(head1); headList.add(head2); headList.add(head3); ``` ### 4. 执行导出 调用EasyExcel的write方法,即可实现Excel批量导出。例如: ```java String filePath = "student.xlsx"; ExcelWriter excelWriter = EasyExcel.write(filePath).head(headList).build(); WriteSheet writeSheet = EasyExcel.writerSheet("学生信息").build(); excelWriter.write(dataList, writeSheet); excelWriter.finish(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值