java 的Excel操作封装

import java.io.FileOutputStream;

import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;


public class ExcelUtil {
    /**
     * @throws Exception
     * @功能:手工构建一个简单格式的Excel
     */

  

public void toExcel(List<Map<String, String>> list, String[] fieldNames, String[] beanFieldNames, String title, int start, int length)
        throws Exception {
    // 第一步,创建一个webbook,对应一个Excel文件
    HSSFWorkbook wb = new HSSFWorkbook();
    // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
    HSSFSheet sheet = wb.createSheet("sheet1");
    // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
    // 设置字体
    HSSFFont headfont = wb.createFont();
    headfont.setFontName("黑体");
    headfont.setFontHeightInPoints((short) 14);// 字体大小
    headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
    // 另一个样式
    HSSFCellStyle headstyle = wb.createCellStyle();
    headstyle.setFont(headfont);
    headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
    headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
    headstyle.setLocked(true);
    headstyle.setWrapText(true);// 自动换行
    headstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
    headstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
    // 另一个字体样式
    HSSFFont columnHeadFont = wb.createFont();
    columnHeadFont.setFontName("宋体");
    columnHeadFont.setFontHeightInPoints((short) 11);
    columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    // 列头的样式
    HSSFCellStyle columnHeadStyle = wb.createCellStyle();
    columnHeadStyle.setFont(columnHeadFont);
    columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
    columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
    columnHeadStyle.setLocked(true);
    columnHeadStyle.setWrapText(true);
    columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
    columnHeadStyle.setBorderLeft((short) 1);// 边框的大小
    columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
    columnHeadStyle.setBorderRight((short) 1);// 边框的大小
    columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
    columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色
    // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
    columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);

    HSSFFont font = wb.createFont();
    font.setFontName("宋体");
    font.setFontHeightInPoints((short) 11);
    // 普通单元格样式
    HSSFCellStyle style = wb.createCellStyle();
    style.setFont(font);
    style.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);// 上下居中
    style.setLeftBorderColor(HSSFColor.BLACK.index);
    style.setBorderLeft((short) 1);
    style.setRightBorderColor(HSSFColor.BLACK.index);
    style.setBorderRight((short) 1);
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
    style.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
    // style.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
    // 另一个样式
    HSSFCellStyle centerstyle = wb.createCellStyle();
    centerstyle.setFont(font);
    centerstyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);// 左右居中
    centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
    centerstyle.setWrapText(true);
    centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);
    centerstyle.setBorderLeft((short) 1);
    centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
    centerstyle.setBorderRight((short) 1);
    centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
    centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.

    // 创建第一行
    HSSFRow row0 = sheet.createRow(0);
    // 设置行高
    row0.setHeight((short) 900);
    // 创建第一列
    HSSFCell cell0 = row0.createCell(0);
    cell0.setCellValue(new HSSFRichTextString(title));

    /**
     * 合并单元格 第一个参数:第一个单元格的行数(从0开始) 第二个参数:第二个单元格的行数(从0开始)
     * 第三个参数:第一个单元格的列数(从0开始) 第四个参数:第二个单元格的列数(从0开始)
     */
    CellRangeAddress range = new CellRangeAddress(0, 0, 0, fieldNames.length - 1);
    sheet.addMergedRegion(range);
    cell0.setCellStyle(headstyle);

    // 创建第二行
    HSSFRow row1 = sheet.createRow(1);
    // 设置行高
    row1.setHeight((short) 900);
    // 创建第一列
    HSSFCell cell1 = row1.createCell(0);
    cell1.setCellValue("日期:" + System.currentTimeMillis());
    cell1.setCellStyle(headstyle);
    /**
     * 合并单元格 第一个参数:第一个单元格的行数(从0开始) 第二个参数:第二个单元格的行数(从0开始)
     * 第三个参数:第一个单元格的列数(从0开始) 第四个参数:第二个单元格的列数(从0开始)
     */
    CellRangeAddress range1 = new CellRangeAddress(1, 1, 0, fieldNames.length - 1);
    sheet.addMergedRegion(range1);

    HSSFRow row2 = sheet.createRow(2);
    // 第四步,创建单元格,并设置值表头 设置表头居中

    for (int i = 0; i < fieldNames.length; i++) {

        HSSFCell cell2 = row2.createCell(i);
        cell2.setCellValue(fieldNames[i]);
        cell2.setCellStyle(columnHeadStyle);
    }

    // 第五步,写入实体数据 实际应用中这些数据从数据库得到,


    double lastlist[] = new double[length];
    if (length != 0) {
        for (int i = 0; i < lastlist.length; i++) {
            lastlist[i] = 0.0;
        }
    }
    for (int i = 0; i < list.size(); i++) {
        HSSFRow row = sheet.createRow((int) i + 3);
        Map<String, String> map = list.get(i);
        // 第四步,创建单元格,并设置值
        int j = 0;
        HSSFCell cellcirs = row.createCell(j++);
        cellcirs.setCellValue(i + 1);
        cellcirs.setCellStyle(style);
        for (String key : beanFieldNames) {
            cellcirs = row.createCell(j++);

            String str = String.valueOf(map.get(key));
            if (str.matches("^[+-]?\\d*[.]?\\d*$") && str != null && !str.equals("")) {
                Double strnum = Double.parseDouble(str);
                cellcirs.setCellValue(strnum);
                if (length != 0) {
                    if (j >= start) {

                        lastlist[j - start] = lastlist[j - start] + strnum;

                    }
                }
            } else {
                cellcirs.setCellValue(str);
            }

            cellcirs.setCellStyle(style);

        }

    }

        if (length != 0) {
            int footRownumber = sheet.getLastRowNum();
            HSSFRow footRow = sheet.createRow(footRownumber + 1);
            HSSFCell footRowcell = footRow.createCell(0);
            footRowcell.setCellValue("合计");
            footRowcell.setCellStyle(centerstyle);
            range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0, start - 2);
            sheet.addMergedRegion(range);
            for (int i = 0; i < length; i++) {
                HSSFCell cell = footRow.createCell(start + i - 1);
                cell.setCellValue((double)Math.round(lastlist[i]*100)/100);
                cell.setCellStyle(style);

            }

            HSSFRow lastRow = sheet.createRow(footRownumber + 2);
            HSSFCell lastcell = lastRow.createCell(0);
//            lastcell.setCellValue(ConvertNum.NumToChinese(lastlist[index - sumindex]));
//            lastcell.setCellStyle(centerstyle);
            range = new CellRangeAddress(footRownumber + 2, footRownumber + 2, 0, title.length - 1);
            sheet.addMergedRegion(range);

        }
        // String chargestatistics="";
        //
        // for (Map.Entry<String, Object> entry : statistics.entrySet()) {
        //
        // chargestatistics= chargestatistics +entry.getKey() + ": " +
        // entry.getValue()+" ";
        //
        // }
        // int footRownumber = sheet.getLastRowNum();
        // HSSFRow footRow = sheet.createRow(footRownumber + 1);
        // HSSFCell footRowcell = footRow.createCell(0);
        // footRowcell.setCellValue(new HSSFRichTextString(chargestatistics));
        // footRowcell.setCellStyle(centerstyle);
        // range = new CellRangeAddress(footRownumber + 1, footRownumber + 1, 0,
        // title.length-1);
        // sheet.addMergedRegion(range);

        // 第六步,将文件存到指定位置

        FileOutputStream fout = new FileOutputStream(filepath);
        wb.write(fout);
        fout.close();

    }
    
    
}

猜你喜欢

转载自blog.csdn.net/july_young/article/details/82802742
今日推荐