欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JXL导出EXCEL合并单元格

程序员文章站 2022-06-14 09:26:58
...

图片
JXL导出EXCEL合并单元格

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import jxl.CellView;
import jxl.Workbook;
import jxl.biff.DisplayFormat;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public final static void buildExcel4(OutputStream outStream,
            String mainTitle, String[] titles, List<String> contents) {
        int beginRow = 0;
        try {

            /** **********创建工作簿************ */
            WritableWorkbook workbook = Workbook.createWorkbook(outStream);

            /** **********创建工作表************ */
            WritableSheet sheet = workbook.createSheet("sheet", 0);


            /** **********设置纵横打印(默认为纵打)、打印纸***************** */
            SheetSettings sheetset = sheet.getSettings();
            sheetset.setProtected(false);

            /** ************设置单元格字体************** */
            WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
            WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10,
                    WritableFont.BOLD);

            /** ************以下设置三种单元格样式************ */
            // 用于标题居中
            WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);
            wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
            wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
            wcf_center.setAlignment(Alignment.CENTRE); // 文字水平对齐
            wcf_center.setWrap(false); // 文字是否换行

            // 用于正文居左
            WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
            wcf_left.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条
            wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐
            wcf_left.setAlignment(Alignment.CENTRE); // 文字水平对齐
            wcf_left.setWrap(false); // 文字是否换行

            // excel大标题
            if (mainTitle != null && !mainTitle.equals("")) {
                sheet.mergeCells(0, 0, titles.length - 1, 0);
                sheet.addCell(new Label(0, 0, mainTitle, wcf_center));
                beginRow = beginRow + 1;
            }

            /** ***************以下是EXCEL第一行列标题********************* */
            if (titles != null && titles.length > 1) {
                for (int i = 0; i < titles.length; i++) {
                    sheet.addCell(new Label(i, 1, titles[i], wcf_center));
                }
                beginRow = beginRow + 1;
            }
            /** ***************以下是EXCEL正文数据********************* */
            sheet.addCell(new Label(0, 2,  "1", wcf_left));
            sheet.addCell(new Label(1, 2, "创建红包数", wcf_left));
            sheet.addCell(new Label(2, 2, "创建红包数", wcf_left));
            sheet.addCell(new Label(3, 2, "10", wcf_left));
            sheet.addCell(new Label(4, 2, "100", wcf_left));

            sheet.addCell(new Label(0, 3, "2", wcf_left));
            sheet.addCell(new Label(1, 3, "收入", wcf_left));
            sheet.addCell(new Label(2, 3, "发放的红包总额", wcf_left));
            sheet.addCell(new Label(3, 3, "10", wcf_left));
            sheet.addCell(new Label(4, 3, "100", wcf_left));



            sheet.addCell(new Label(0, 4, "3", wcf_left));
            sheet.addCell(new Label(1, 4, "收入", wcf_left));
            sheet.addCell(new Label(2, 4, "小计", wcf_left));
            sheet.addCell(new Label(3, 4, "10", wcf_left));
            sheet.addCell(new Label(4, 4, "100", wcf_left));

            //合并单元格               
            sheet.mergeCells(1, 3, 1, 4);                 

            sheet.addCell(new Label(0, 5, "4", wcf_left));
            sheet.addCell(new Label(1, 5, "支出", wcf_left));
            sheet.addCell(new Label(2, 5, "失效红包总额", wcf_left));
            sheet.addCell(new Label(3, 5, "1", wcf_left));
            sheet.addCell(new Label(4, 5, "10", wcf_left));

            sheet.addCell(new Label(0, 6, "5", wcf_left));
            sheet.addCell(new Label(1, 6, "支出", wcf_left));
            sheet.addCell(new Label(2, 6, "使用红包总额", wcf_left));
            sheet.addCell(new Label(3, 6, "3", wcf_left));
            sheet.addCell(new Label(4, 6, "30", wcf_left));

            sheet.addCell(new Label(0, 7, "6", wcf_left));
            sheet.addCell(new Label(1, 7, "支出", wcf_left));
            sheet.addCell(new Label(2, 7, "小计", wcf_left));
            sheet.addCell(new Label(3, 7, "4", wcf_left));
            sheet.addCell(new Label(4, 7, "40", wcf_left));

            //合并单元格                   
            sheet.mergeCells(1, 5, 1, 7);   

            sheet.addCell(new Label(0, 8, "7", wcf_left));
            sheet.addCell(new Label(1, 8, "剩余红包数", wcf_left));
            sheet.addCell(new Label(2, 8, "未使用红包总额", wcf_left));
            sheet.addCell(new Label(3, 8, "7", wcf_left));
            sheet.addCell(new Label(4, 8, "70", wcf_left));

            sheet.setColumnView(1, 20);
            sheet.setColumnView(2, 20);
            sheet.setColumnView(3, 20);
            sheet.setColumnView(4, 20);
            sheet.setColumnView(5, 20);


    }
            workbook.write();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }