schangxiang@126.com
2024-09-06 2a19504209e763a7c0e957e4ee265dd419486ef1
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#nullable enable
using Furion.FriendlyException;
using Furion;
using Microsoft.AspNetCore.Http;
using OfficeOpenXml.Style;
using OfficeOpenXml;
using Microsoft.Extensions.Configuration;
 
namespace iWare.Wms.Core.Util
{
    public static class ExcelUtil
    {
        /// <summary>
        /// 在文件中导出标准格式的数据
        /// </summary>
        /// <param name="headers"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ToExcel(List<string> headers, List<List<object>> data, string sheetName, string target, string fileName)
        {
            if (!target.EndsWith("\\"))
            {
                target += "\\";
            }
            target += DateTime.Now.Ticks + "\\";
            if (!Directory.Exists(target))
            {
                Directory.CreateDirectory(target);
            }
            fileName = fileName + "[" + DateTime.Now.ToString("yyyy-MM-dd") + "]" + ".xlsx";
            var path = target + fileName;
            using (var package = new ExcelPackage(new FileInfo(path)))
            {
                var worksheet = package.Workbook.Worksheets.Add(sheetName);
 
                for (int j = 0; j < headers.Count; j++)
                {
                    worksheet.Cells[1, j + 1].Value = headers[j];
                    worksheet.Cells[1, j + 1].Style.Font.Bold = true;//加粗
                    worksheet.Cells[1, j + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;//水平居中 
                    worksheet.Cells[1, j + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;//垂直居中 
                    worksheet.Cells[1, j + 1].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;//边框常规粗细
                    worksheet.Cells[1, j + 1].Style.Border.Left.Style = ExcelBorderStyle.Thin;//边框常规粗细
                    worksheet.Cells[1, j + 1].Style.Border.Right.Style = ExcelBorderStyle.Thin;//边框常规粗细
                    worksheet.Cells[1, j + 1].Style.Border.Top.Style = ExcelBorderStyle.Thin;//边框常规粗细
                }
 
                for (int i = 0; i < data.Count; i++)
                {
                    for (int j = 0; j < data[i].Count; j++)
                    {
                        worksheet.Cells[2 + i, j + 1].Value = data[i][j];
                    }
                }
 
                package.Save();
            }
            return path;
        }
 
        /// <summary>
        /// 在response中导出标准格式的数据
        /// </summary>
        /// <param name="headers"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static void ToExcel(List<string> headers, List<List<object>> data, string sheetName, Stream newStream)
        {
 
            using (var package = new ExcelPackage(newStream))
            {
                var worksheet = package.Workbook.Worksheets.Add(sheetName);
 
                for (int j = 0; j < headers.Count; j++)
                {
                    worksheet.Cells[1, j + 1].Value = headers[j];
                    worksheet.Cells[1, j + 1].Style.Font.Bold = true;//加粗
                    worksheet.Cells[1, j + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;//水平居中 
                    worksheet.Cells[1, j + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;//垂直居中 
                    worksheet.Cells[1, j + 1].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;//边框常规粗细
                    worksheet.Cells[1, j + 1].Style.Border.Left.Style = ExcelBorderStyle.Thin;//边框常规粗细
                    worksheet.Cells[1, j + 1].Style.Border.Right.Style = ExcelBorderStyle.Thin;//边框常规粗细
                    worksheet.Cells[1, j + 1].Style.Border.Top.Style = ExcelBorderStyle.Thin;//边框常规粗细
                }
 
                for (int i = 0; i < data.Count; i++)
                {
                    for (int j = 0; j < data[i].Count; j++)
                    {
                        worksheet.Cells[2 + i, j + 1].Value = data[i][j];
                    }
                }
                package.Save();
            }
        }
 
        /// <summary>
        /// 导入标准格式的数据文件
        /// </summary>
        /// <param name="file">上传的文件</param>
        /// <returns></returns>
        public static void FromExcel(IFormFile file, out List<string> headers, out List<List<object?>> data, out string sheetName)
        {
            if (file == null) throw Oops.Oh(ErrorCode.D8000);
            string key = "UploadFile:Excel";
            var allowContentTypes = App.Configuration.GetSection($"{key}:contentType").Get<IEnumerable<string>>();
            if (!allowContentTypes.Contains(file.ContentType)) throw Oops.Oh(ErrorCode.D8001);
            sheetName = "";
            headers = new();    
            data = new();
 
            using (var package = new ExcelPackage(file.OpenReadStream()))
            {
                var worksheet = package.Workbook.Worksheets.Count > 0 ? package.Workbook.Worksheets[0] : null;
 
                sheetName = package.Workbook.Worksheets.Count > 0 ? package.Workbook.Worksheets[0].Name : "";
                var cellsCount = worksheet?.Cells?.Count() ?? 0;
 
                int col = 1;
                string? header = worksheet?.Cells[1, col++].Value?.ToString();
 
                while (!string.IsNullOrEmpty(header))
                {
                    headers.Add(header);
                    header = worksheet?.Cells[1, col++]?.Value?.ToString();
                }
 
                var headersCount = headers.Count;
 
                if (headersCount == 0) return;
 
                var rowsCount = (int)Math.Ceiling(((double)cellsCount) / headersCount) - 1;
 
                for (var i = 0; i < rowsCount; i++)
                {
                    List<object?> row = new();
                    for (var j = 0; j < headersCount; j++)
                    {
                        row.Add(worksheet?.Cells[i + 2, j + 1]?.Value);
                    }
                    data.Add(row);
                }
            }
        }
    }
}