wang-hao-jie
2022-08-25 57dcc73636bb7d8dce89c808eb8cc988a7512264
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.ruoyi.common.core.excel;
 
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
import java.io.*;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
 
/**
 * Excel超大数据写入,抽象excel2007读入器,先构建.xlsx一张模板,改写模板中的sheet.xml,
 * 使用这种方法 写入.xlsx文件,不需要太大的内存
 * @version 2014-9-2
 */
public abstract class ExcelWriter {
 
    private SpreadsheetWriter sw;
 
    /**
     * 写入电子表格的主要流程
     * 
     * @param fileName
     * @throws Exception
     */
    @SuppressWarnings("resource")
    public void process(String fileName) throws Exception {
        
        // 建立工作簿和电子表格对象
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet("sheet1");
        
        // 持有电子表格数据的xml文件名 例如 /xl/worksheets/sheet1.xml
        String sheetRef = sheet.getPackagePart().getPartName().getName();
 
        // 保存模板
        FileOutputStream os = new FileOutputStream("template.xlsx");
        wb.write(os);
        os.close();
 
        // 生成xml文件
        File tmp = File.createTempFile("sheet", ".xml");
        Writer fw = new FileWriter(tmp);
        sw = new SpreadsheetWriter(fw);
        generate();
        fw.close();
 
        // 使用产生的数据替换模板
        File templateFile = new File("template.xlsx");
        FileOutputStream out = new FileOutputStream(fileName);
        substitute(templateFile, tmp, sheetRef.substring(1), out);
        out.close();
        // 删除文件之前调用一下垃圾回收器,否则无法删除模板文件
        System.gc();
        // 删除临时模板文件
        if (templateFile.isFile() && templateFile.exists()) {
            templateFile.delete();
        }
    }
 
    /**
     * 类使用者应该使用此方法进行写操作
     * 
     * @throws Exception
     */
    public abstract void generate() throws Exception;
 
    public void beginSheet() throws IOException {
        sw.beginSheet();
    }
 
    public void insertRow(int rowNum) throws IOException {
        sw.insertRow(rowNum);
    }
 
    public void createCell(int columnIndex, String value) throws IOException {
        sw.createCell(columnIndex, value, -1);
    }
 
    public void createCell(int columnIndex, double value) throws IOException {
        sw.createCell(columnIndex, value, -1);
    }
 
    public void endRow() throws IOException {
        sw.endRow();
    }
 
    public void endSheet() throws IOException {
        sw.endSheet();
    }
 
    /**
     * 
     * @param zipfile the template file
     * @param tmpfile the XML file with the sheet data
     * @param entry the name of the sheet entry to substitute, e.g. xl/worksheets/sheet1.xml
     * @param out the stream to write the result to
     */
    private static void substitute(File zipfile, File tmpfile, String entry,
            OutputStream out) throws IOException {
            try (
                ZipFile zip = new ZipFile(zipfile);
                ZipOutputStream zos = new ZipOutputStream(out);
                InputStream is = new FileInputStream(tmpfile);
            ){
            @SuppressWarnings("unchecked")
            Enumeration<ZipEntry> en = (Enumeration<ZipEntry>) zip.entries();
            while (en.hasMoreElements()) {
                ZipEntry ze = en.nextElement();
                if (!ze.getName().equals(entry)) {
                    zos.putNextEntry(new ZipEntry(ze.getName()));
                    try (InputStream is2=zip.getInputStream(ze)){
                        copyStream(is2, zos);
                    }
                }
            }
            zos.putNextEntry(new ZipEntry(entry));
            copyStream(is, zos);
        }
    }
 
    private static void copyStream(InputStream in, OutputStream out)
            throws IOException {
        byte[] chunk = new byte[1024];
        int count;
        while ((count = in.read(chunk)) >= 0) {
            out.write(chunk, 0, count);
        }
    }
 
    /**
     * 在写入器中写入电子表格
     * 
     */
    public static class SpreadsheetWriter {
        private final Writer _out;
        private int _rownum;
        private static String LINE_SEPARATOR = System
                .getProperty("line.separator");
 
        public SpreadsheetWriter(Writer out) {
            _out = out;
        }
 
        public void beginSheet() throws IOException {
            _out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                    + "<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">");
            _out.write("<sheetData>" + LINE_SEPARATOR);
        }
 
        public void endSheet() throws IOException {
            _out.write("</sheetData>");
            _out.write("</worksheet>");
        }
 
        /**
         * 插入新行
         * 
         * @param rownum
         *            以0开始
         */
        public void insertRow(int rownum) throws IOException {
            _out.write("<row r=\"" + (rownum + 1) + "\">" + LINE_SEPARATOR);
            this._rownum = rownum;
        }
 
        /**
         * 插入行结束标志
         */
        public void endRow() throws IOException {
            _out.write("</row>" + LINE_SEPARATOR);
        }
 
        /**
         * 插入新列
         * 
         * @param columnIndex
         * @param value
         * @param styleIndex
         * @throws IOException
         */
        public void createCell(int columnIndex, String value, int styleIndex)
                throws IOException {
            String ref = new CellReference(_rownum, columnIndex)
                    .formatAsString();
            _out.write("<c r=\"" + ref + "\" t=\"inlineStr\"");
            if (styleIndex != -1) {
                _out.write(" s=\"" + styleIndex + "\"");
            }
            _out.write(">");
            _out.write("<is><t>" + encoderXML(value) + "</t></is>");
            _out.write("</c>");
        }
 
        public void createCell(int columnIndex, String value)
                throws IOException {
            createCell(columnIndex, value, -1);
        }
 
        public void createCell(int columnIndex, double value, int styleIndex)
                throws IOException {
            String ref = new CellReference(_rownum, columnIndex)
                    .formatAsString();
            _out.write("<c r=\"" + ref + "\" t=\"n\"");
            if (styleIndex != -1) {
                _out.write(" s=\"" + styleIndex + "\"");
            }
            _out.write(">");
            _out.write("<v>" + value + "</v>");
            _out.write("</c>");
        }
 
        public void createCell(int columnIndex, double value)
                throws IOException {
            createCell(columnIndex, value, -1);
        }
 
        public void createCell(int columnIndex, Calendar value, int styleIndex)
                throws IOException {
            createCell(columnIndex, DateUtil.getExcelDate(value, false),
                    styleIndex);
        }
    }
 
    // XML Encode
    private static final String[] xmlCode = new String[256];
 
    static {
        // Special characters
        xmlCode['\''] = "'";
        xmlCode['\"'] = "\""; // double quote
        xmlCode['&'] = "&"; // ampersand
        xmlCode['<'] = "<"; // lower than
        xmlCode['>'] = ">"; // greater than
    }
 
    /**
     * <p>
     * Encode the given text into xml.
     * </p>
     * 
     * @param string the text to encode
     * @return the encoded string
     */
    public static String encoderXML(String string) {
        if (string == null) {
            return "";
        }
        int n = string.length();
        char character;
        String xmlchar;
        StringBuffer buffer = new StringBuffer();
        // loop over all the characters of the String.
        for (int i = 0; i < n; i++) {
            character = string.charAt(i);
            // the xmlcode of these characters are added to a StringBuffer
            // one by one
            try {
                xmlchar = xmlCode[character];
                if (xmlchar == null) {
                    buffer.append(character);
                } else {
                    buffer.append(xmlCode[character]);
                }
            } catch (ArrayIndexOutOfBoundsException aioobe) {
                buffer.append(character);
            }
        }
        return buffer.toString();
    }
 
//    /**
//     * 测试方法
//     */
//    public static void main(String[] args) throws Exception {
//
//        String file = "E:/测试导出数据.xlsx";
//        
//        ExcelWriter writer = new ExcelWriter() {
//            @Override
//            public void generate() throws Exception {
//                
//                // 电子表格开始
//                this.beginSheet();
//                
//                for (int rownum = 0; rownum < 100; rownum++) {
//                    // 插入新行
//                    this.insertRow(rownum);
//                    
//                    // 建立新单元格,索引值从0开始,表示第一列
//                    this.createCell(0, "第 " + rownum + " 行");
//                    this.createCell(1, 34343.123456789);
//                    this.createCell(2, "23.67%");
//                    this.createCell(3, "12:12:23");
//                    this.createCell(4, "2014-10-11 12:12:23");
//                    this.createCell(5, "true");
//                    this.createCell(6, "false");
//
//                    // 结束行
//                    this.endRow();
//                }
//                
//                // 电子表格结束
//                this.endSheet();
//            }
//        };
//        writer.process(file);
//    }
        
}