자바 대량 생 성 엑셀 코드 공유

3525 단어
package com.test.util;

/** 
 * @author ocq
 * 
 */
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class SimpleExcelWrite {
    public static void createExcel(OutputStream os, String[] headField, String[] values) throws WriteException,
            IOException {
        //      
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        //       
        WritableSheet sheet = workbook.createSheet("First Sheet", 0);

        //         ,       ,         ,         ,        
        for (int y = 0; y < 100 * 100 + 1; y++) {
            int index = y % 100;
            if (index == 0) {
                for (int x = 0; x < headField.length; x++) {
                    Label xuexiao = new Label(x, index, headField[x]);
                    sheet.addCell(xuexiao);
                }
            } else {
                for (int x = 0; x < values.length; x++) {
                    String string = values[x];
                    if (x == 0 || x == 3) {
                        string += y;
                    }
                    Label xuexiao = new Label(x, index, string);
                    sheet.addCell(xuexiao);
                }
            }

        }
        //
        workbook.write();
        workbook.close();
        os.close();
    }

    public static void main(String[] args) throws WriteException, IOException {
        // List<String> headField=new ArrayList<String>();
        String[] headField = new String[] { "   ", "  2", ..... };

        String[] values = new String[] { "   ", "  2", ... };

        for (int i = 1; i < 101; i++) {
            OutputStream os = new FileOutputStream("file name" + i + ".xls");
            createExcel(os, headField, values);
        }

    }
}

jar 패키지 도입 필요: www. andykhan. com / jexcelapi / download. html
복사 해서 효 과 를 볼 게 요.
참고: 자바 생 성 및 조작 엑셀 파일

좋은 웹페이지 즐겨찾기