poi操作excel的例子

     这个是poi操作excel的例子,只是对poi的简单的运用。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelUtils {
    /** Excel文件的存放位置。注意是正斜线 */
    public static String filePath = "D:/test.xls";

    public static void main(String argv[]) {
        creadExcel();
        // readExcel();
        // updateExcel();
    }

    public static void creadExcel() {
        try {

            // 创建新的Excel 工作簿

            HSSFWorkbook workbook = new HSSFWorkbook();

            // 在Excel工作簿中建一工作表,其名为缺省值

            HSSFSheet sheet = workbook.createSheet();

            // 在索引0的位置创建行(最顶端的行)

            HSSFRow row = sheet.createRow(0);

            // 在索引0的位置创建单元格(左上端)
            HSSFCell cell1 = row.createCell(0);
            // 定义单元格为字符串类型
            cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
            // 在单元格中输入一些内容
            cell1.setCellValue("增加值1");
            ExcelCellStyleUtils excelCellStyleUtils = new ExcelCellStyleUtils(workbook);
            cell1.setCellStyle(excelCellStyleUtils.nameStyle);

            // 新建一输出文件流
            FileOutputStream fOut = new FileOutputStream(filePath);
            // 把相应的Excel 工作簿存盘
            workbook.write(fOut);
            fOut.flush();
            // 操作结束,关闭文件
            fOut.close();
            System.out.println("文件生成...");

        } catch (Exception e) {
            System.out.println("已运行 xlCreate() : " + e);
        }
    }

    public static void readExcel() {
        try {
            // 创建对Excel工作簿文件的引用
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));
            // 创建对工作表的引用。
            // 本例是按名引用(让我们假定那张表有着缺省名"Sheet0")
            HSSFSheet sheet = workbook.getSheet("Sheet0");
            // 也可用getSheetAt(int index)按索引引用,
            HSSFRow row = sheet.getRow(0);
            HSSFCell cell = row.getCell(0);
            // 输出单元内容,cell.getStringCellValue()就是取所在单元的值
            System.out.println("左上端单元是: " + cell.getStringCellValue());
        } catch (Exception e) {
            System.out.println("已运行xlRead() : " + e);
        }
    }

    public static void updateExcel() {
        try {
            // 创建对Excel工作簿文件的引用
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));
            // 创建对工作表的引用。
            // 本例是按名引用(让我们假定那张表有着缺省名"Sheet0")
            HSSFSheet sheet = workbook.getSheet("Sheet0");
            HSSFRow row = sheet.createRow(4);
            HSSFCell cell = row.createCell(1);
            cell.setCellValue("这是又加上的");

            // 新建一输出文件流
            FileOutputStream fOut = new FileOutputStream(filePath);
            // 把相应的Excel 工作簿存盘
            workbook.write(fOut);
            fOut.flush();
            // 操作结束,关闭文件
            fOut.close();
        } catch (Exception e) {
            System.out.println("已运行xlRead() : " + e);
        }
    }
}


一个格式辅助类
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class ExcelCellStyleUtils {

    // 标题样式
    public static HSSFCellStyle titleStyle;
    // 时间样式
    public static HSSFCellStyle dataStyle;
    // 单元格样式
    public static HSSFCellStyle nameStyle;
    // 超链接样式
    public static HSSFCellStyle linkStyle;
    public static HSSFFont font;

    public ExcelCellStyleUtils(HSSFWorkbook workbook) {
        titleStyle = linkStyle(workbook);
        dataStyle = dataStyle(workbook);
        nameStyle = nameStyle(workbook);
        linkStyle = titleStyle(workbook);
    }

    /**
     * 超链接样式
     * 
     * @return HSSFCellStyle
     */
    private static HSSFCellStyle linkStyle(HSSFWorkbook work) {
        HSSFCellStyle linkStyle = work.createCellStyle();
        linkStyle.setBorderBottom((short) 1);
        linkStyle.setBorderLeft((short) 1);
        linkStyle.setBorderRight((short) 1);
        linkStyle.setBorderTop((short) 1);
        linkStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
        linkStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        HSSFFont font = work.createFont();
        font.setFontName(HSSFFont.FONT_ARIAL);
        font.setUnderline((byte) 1);
        font.setColor(HSSFColor.BLUE.index);
        linkStyle.setFont(font);
        return linkStyle;
    }

    /**
     * s 单元格样式
     * 
     * @return HSSFCellStyle
     */
    private static HSSFCellStyle nameStyle(HSSFWorkbook work) {
        HSSFCellStyle nameStyle = work.createCellStyle();
        nameStyle.setBorderBottom((short) 1);
        nameStyle.setBorderLeft((short) 1);
        nameStyle.setBorderRight((short) 1);
        nameStyle.setBorderTop((short) 1);
        nameStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
        nameStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        return nameStyle;
    }

    /**
     * 时间样式
     * 
     * @return HSSFCellStyle
     */
    private static HSSFCellStyle dataStyle(HSSFWorkbook work) {
        HSSFCellStyle dataStyle = work.createCellStyle();
        dataStyle.setBorderBottom((short) 1);
        dataStyle.setBorderLeft((short) 1);
        dataStyle.setBorderRight((short) 1);
        dataStyle.setBorderTop((short) 1);
        dataStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
        dataStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        dataStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        return dataStyle;
    }

    /**
     * 标题样式
     * 
     * @return HSSFCellStyle
     */
    private static HSSFCellStyle titleStyle(HSSFWorkbook work) {
        HSSFCellStyle titleStyle = work.createCellStyle();
        font = work.createFont();
        font.setItalic(true);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setColor(HSSFColor.BLUE.index);
        titleStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);
        titleStyle.setBorderLeft((short) 1);
        titleStyle.setBorderRight((short) 1);
        titleStyle.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
        titleStyle.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);
        titleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        return titleStyle;
    }
}

你可能感兴趣的:(apache,工作,Excel)