jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using HH.WMS.Entitys;
 
namespace HH.WMS.Common
{
    public class ExcelHelper
    {
        /// <summary>
        /// 生成Excel
        /// </summary>
        /// <param name="title">表头名</param>
        /// <param name="cellTexts">每列单元格的名称</param>
        /// <param name="cellWidths">每列单元格的宽度</param>
        /// <param name="cellValues">对应DataTable中的列名</param>
        /// <param name="table"></param>
        /// <returns></returns>
        public static MemoryStream DataTableToExcel(string title, string[] cellTexts, int[] cellWidths, string[] cellValues, DataTable table)
        {
            MemoryStream ms = new MemoryStream();
            using (table)
            {
                //初始化
                HSSFWorkbook _book = new HSSFWorkbook();
                DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
                dsi.Company = "汉和";
                _book.DocumentSummaryInformation = dsi;
 
                SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
                si.Subject = title;
                _book.SummaryInformation = si;
 
                //创建表格
                ISheet _sheet = _book.CreateSheet(title);
 
                //第一行:表头
                IRow _row = _sheet.CreateRow(0);
                _row.HeightInPoints = 30;
 
                ICell _cell = _row.CreateCell(0);
                _cell.SetCellValue(title);
 
                ICellStyle style = _book.CreateCellStyle();
                style.Alignment = HorizontalAlignment.Center;
                IFont font = _book.CreateFont();
                font.FontHeight = 20 * 20;
                font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold; //粗体
                style.SetFont(font);
                _cell.CellStyle = style;
 
                _sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, cellTexts.Length - 1));
 
                //第二行:字段名称
                ICellStyle _style = _book.CreateCellStyle();
                _style.Alignment = HorizontalAlignment.Center;
                IFont _font = _book.CreateFont();
                _font.FontHeight = 15 * 15;
                _font.FontName = "微软雅黑";
                _style.SetFont(_font);
 
                _row = _sheet.CreateRow(1);
                _row.HeightInPoints = 20;
 
                for (int cellIndex = 0; cellIndex < cellTexts.Length; cellIndex++)
                {
                    _cell = _row.CreateCell(cellIndex);
                    _cell.SetCellValue(cellTexts[cellIndex]);
                    _cell.CellStyle = _style;
                    _sheet.SetColumnWidth(cellIndex, cellWidths[cellIndex]);
                }
 
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    _row = _sheet.CreateRow(i + 2);
                    _row.HeightInPoints = 20;
 
                    for (int cellIndex = 0; cellIndex < cellTexts.Length; cellIndex++)
                    {
                        _cell = _row.CreateCell(cellIndex);
                        _cell.SetCellValue(table.Rows[i][cellValues[cellIndex]].ToString());
                    }
                }
                _book.Write(ms);
                ms.Flush();
            }
 
            return ms;
        }
 
        public static MemoryStream ListToExcel<T>(string title, string[] cellTexts, int[] cellWidths, string[] cellValues, List<T> lists)
        {
            MemoryStream ms = new MemoryStream();
            //初始化
            HSSFWorkbook _book = new HSSFWorkbook();
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "汉和";
            _book.DocumentSummaryInformation = dsi;
 
            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Subject = title;
            _book.SummaryInformation = si;
 
            //创建表格
            ISheet _sheet = _book.CreateSheet(title);
 
            //第一行:表头
            IRow _row = _sheet.CreateRow(0);
            _row.HeightInPoints = 30;
 
            ICell _cell = _row.CreateCell(0);
            _cell.SetCellValue(title);
 
            ICellStyle style = _book.CreateCellStyle();
            style.Alignment = HorizontalAlignment.Center;
            IFont font = _book.CreateFont();
            font.FontHeight = 20 * 20;
            font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold; //粗体
            style.SetFont(font);
            _cell.CellStyle = style;
 
            _sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, cellTexts.Length - 1));
 
            //第二行:字段名称
            ICellStyle _style = _book.CreateCellStyle();
            _style.Alignment = HorizontalAlignment.Center;
            IFont _font = _book.CreateFont();
            _font.FontHeight = 15 * 15;
            _font.FontName = "微软雅黑";
            _style.SetFont(_font);
 
            _row = _sheet.CreateRow(1);
            _row.HeightInPoints = 20;
 
            for (int cellIndex = 0; cellIndex < cellTexts.Length; cellIndex++)
            {
                _cell = _row.CreateCell(cellIndex);
                _cell.SetCellValue(cellTexts[cellIndex]);
                _cell.CellStyle = _style;
                _sheet.SetColumnWidth(cellIndex, cellWidths[cellIndex]);
            }
 
            for (int i = 0; i < lists.Count; i++)
            {
                _row = _sheet.CreateRow(i + 2);
                _row.HeightInPoints = 20;
 
                foreach (var list in lists)
                {
                    for (int cellIndex = 0; cellIndex < cellTexts.Length; cellIndex++)
                    {
                        _cell = _row.CreateCell(cellIndex);
                        _cell.SetCellValue(GetValue<T>(list, cellValues[cellIndex]));
                        _cell.CellStyle = _style;
                    }
 
                }
 
            }
            _book.Write(ms);
            ms.Flush();
 
            return ms;
        }
 
        private static string GetValue<T>(T t, string name)
        {
            var p = typeof(T).GetProperty(name);
            object obj = null;
            if (p != null)
            {
                obj = p.GetValue(t, null);
                return obj != null ? obj.ToString() : "";
            }
            else
                return "";
        }
 
        public static OperateResult SaveTemp(dynamic columns,string title,DataTable dt,string fileName)
        {
            try
            {
                string[] cellTexts = new string[columns.Count];
                string[] cellValues = new string[columns.Count];
                int[] cellWidths = new int[columns.Count];
 
                for (int i = 0; i < columns.Count; i++)
                {
                    cellTexts[i] = columns[i].n;
                    cellValues[i] = columns[i].f;
                    cellWidths[i] = Convert.ToInt16((columns[i].w == null ? 100 : columns[i].w.Value)) * 50;
                }
 
                MemoryStream ms = ExcelHelper.DataTableToExcel(title,
                                             cellTexts,
                                             cellWidths,
                                             cellValues,
                                             dt
                     );
                byte[] buffer;
                buffer = ms.ToArray();
                ms.Dispose();
                string tempPath = Path.GetTempPath() + fileName;
                using (FileStream fs = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    fs.Write(buffer, 0, buffer.Length);
                    fs.Flush();
                    buffer = null;
                }
                return OperateResult.Succeed();
            }
            catch (Exception ex)
            {
                return OperateResult.Error(ex.Message);
            }
        }
    }
}