2
schangxiang@126.com
2024-08-16 b47c50a2a514def7374b32d7194b2c599cba5625
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using FineUIPro;
using iWareExcel.EXCEL.Entity;
using iWareExcel.Utils;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
namespace iWareWms.Code.Excel.Service
{
    /// <summary>
    /// Excel服务
    /// </summary>
    public class ExcelService
    {
        private static object Lock = new object();
 
        private ExcelService() { }
 
        private static ExcelService Instance = null;
 
        /// <summary>
        /// 获取单例的方法
        /// </summary>
        /// <returns>菜单服务的单例实体</returns>
        public static ExcelService GetInstance()
        {
            if (Instance == null)
            {
                lock (Lock)
                {
                    if (Instance == null)
                    {
                        Instance = new ExcelService();
                    }
                }
            }
            return Instance;
        }
 
        public string ToExcel(WorkBookEntity workBook, Dictionary<string, List<object>> datas, out string msg)
        {
            var path = ExcelHelper.ToExcelByEPPlus(workBook, datas, out msg);
 
            if (!string.IsNullOrEmpty(msg))
            {
                Alert.ShowInTop(msg);
                return "";
            }
            return path;
        }
 
        public string ToExcelByEPPlus(WorkBookEntity workBook, Dictionary<string, List<object>> datas, out string msg)
        {
            var path = ExcelHelper.ToExcelByEPPlus(workBook, datas, out msg);
 
            if (!string.IsNullOrEmpty(msg))
            {
                Alert.ShowInTop(msg);
                return "";
            }
            return path;
        }
 
        public void OutputClient(FileInfo file)
        {
            HttpResponse response = HttpContext.Current.Response;
            response.Buffer = true;
            response.Clear();
            response.ClearHeaders();
            response.ClearContent();
 
           
            //导出到 .xlsx 格式不能用时,可以试试这个
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
 
            response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", file.Name));
 
            response.Charset = "GB2312";
            response.ContentEncoding = Encoding.GetEncoding("GB2312");
 
            response.AddHeader("Content-Length", file.Length.ToString());
 
            response.WriteFile(file.FullName);
            response.Flush();
 
            response.Close();
            
        }
    }
}