poi导出excel,不使用模板的

 

poi导出excel,基于模板的比较简单,这个列是动态的,所已选择不基于模板的,相对复杂些,要设置样式。包括:设置列宽、设置字体、设置边框

 

package com.urt.module;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Vector;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 导出excel
 * @author happyqing
 */
public class ExportExcel {
    
    /**
     * @param vector 数据,可以是别的类型的
     * @param title 标题
     * @param filePath  文件路径
     * @throws Exception 另一个程序正在使用此文件,进程无法访问
     */
    public static void export(Vector vector, String[] title, String filePath) throws Exception{
        //工作簿
        Workbook wb;
        
        File file = new File(filePath);

        // 创建模板工作表
        if (filePath.indexOf(".xlsx") > -1) {
                //templatewb = new XSSFWorkbook(new FileInputStream(filePath));
                wb = new XSSFWorkbook();
		//wb = new SXSSFWorkbook(1000); //大于1000行时会把之前的行写入硬盘,解决内存溢出
        } else {
                wb = new HSSFWorkbook();
        }
        
        //字体
        Font font = wb.createFont();   
        //font.setFontHeightInPoints((short)14);   
        //font.setColor(IndexedColors.DARK_BLUE.getIndex());   
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        
        //样式
        CellStyle styleBOLD = createBorderedStyle(wb);
        styleBOLD.setFont(font);
        styleBOLD.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        styleBOLD.setWrapText(true); //自动换行
        
        //样式
        CellStyle styleWrap = createBorderedStyle(wb);
        styleWrap.setWrapText(true);
        styleWrap.setVerticalAlignment(CellStyle.VERTICAL_TOP);
        
        //表
        Sheet sheet = wb.createSheet("对比结果");
        //列宽
        //sheet.autoSizeColumn(( short ) 0 ); // 调整第一列宽度
        //sheet.SetColumnWidth(1, 50 * 256);  //设置列宽,50个字符宽度。宽度参数为1/256,故乘以256,中文的要再乘以2
        sheet.setColumnWidth(0, 1500);
        sheet.setColumnWidth(1, 14000);
        sheet.setColumnWidth(2, 1500);
        sheet.setColumnWidth(3, 1200);
        sheet.setColumnWidth(4, 1200);
        sheet.setColumnWidth(5, 1200);
        sheet.setColumnWidth(6, 1200);
        sheet.setColumnWidth(7, 1200);
        sheet.setColumnWidth(8, 1200);
       
        //行
        Row row = sheet.createRow(0);
        //单元格
        Cell cell;
        //写入标题行
        for(int i=0; i<title.length; i++){
            cell = row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(styleBOLD);
        }

        //写入数据
        Vector dataRow;
        for(int i=0; i<vector.size(); i++){
            dataRow = (Vector)vector.get(i);
            row = sheet.createRow(i+1);
            for(int j=0; j<dataRow.size(); j++){
                cell = row.createCell(j);
                if(j==1){
                    cell.setCellValue(dataRow.get(j).toString());
                    //cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                    cell.setCellStyle(styleWrap);
                } else {
                    cell.setCellValue(Integer.parseInt(dataRow.get(j).toString()));
                    //cell.setCellType(Cell.CELL_TYPE_STRING);
                    cell.setCellStyle(styleWrap);
                }
            }
        }

        //写入文件
        FileOutputStream fOut = new FileOutputStream(filePath);
        wb.write(fOut);
    }
    
    private static CellStyle createBorderedStyle(Workbook wb){   
        CellStyle style = wb.createCellStyle();   
        style.setBorderRight(CellStyle.BORDER_THIN);   
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());   
        style.setBorderBottom(CellStyle.BORDER_THIN);   
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());   
        style.setBorderLeft(CellStyle.BORDER_THIN);   
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());   
        style.setBorderTop(CellStyle.BORDER_THIN);   
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());   
        return style;   
    }
}

 

参考:

http://www.iteye.com/problems/65838

 

你可能感兴趣的:(poi,列宽,Excel,字体,边框)