POI创建生成excel及设置相关属性

简单的读写到excel中:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


public class CreateEx {
    /**
     * 
     * 层次结构就是workbook-->Sheet-->Row-->Cell
     * 只要按照这种层次结构操作就不会有什么大的问题
     * @author Administrator
     * @throws IOException 
     *
     */
    public static void main(String[] args) throws IOException {
        //HSSFWorkbook对应的是2003
        //XSSFWorkbook对应的是2007
        //对于03和07它们的操作都是差不多的,只是在需要用07的时候把相应的HSSF前缀改成XSSF前缀就可以了
        //第一步建一个工作簿,即workbook
        Workbook workbook = new HSSFWorkbook();
        //第二步建一个工作表单,急sheet
        Sheet sheet = workbook.createSheet("mysheet1");
        for (int i=0;i<5;i++) {
            //有了表单以后就是行Row了,
            Row row = sheet.createRow(i);
            for (int j=0;j<5;j++) {
                //有了row以后就是row上的一个个小的单元格了
                Cell cell = row.createCell(j);
                //给单元格添加内容
                cell.setCellValue("row"+(i+1)+",column"+(j+1));
            }
        }
        //建一个用于存放新建的excel的文件输出流
        OutputStream os = new FileOutputStream("f://2.xls");    
        //把形成的workbook写到一个输出流里面
        workbook.write(os);
        os.close();
    }
}

合并单元格:

public static void main(String args[]) throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        HSSFRow row = sheet.createRow(1);
        HSSFCell cell = row.createCell(1);
        cell.setCellValue("a test of merge!");
        //执行合并操作的语句
        sheet.addMergedRegion(new CellRangeAddress(
                1,// 开始行
                1,// 结束行
                1,// 开始列
                3// 结束列
        ));
        OutputStream os = new FileOutputStream("f://3.xls");
        wb.write(os);
        os.close();
    }

换行:

public static void main(String args[]) throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        HSSFRow row = sheet.createRow(6);
        sheet.autoSizeColumn(2);
        for (int i=0;i<5;i++) {
            HSSFCell cell = row.createCell(i+2);
            HSSFCellStyle style = wb.createCellStyle();
            //to set cell newLine should set its wrap true
            style.setWrapText(true);
            //利用
来实现换行操作,只有在Cell设置为setWrapText(true)的时候才能实现人为的换行
            cell.setCellValue("just use 
 to wrap in a cell!");
            cell.setCellStyle(style);
        }
        OutputStream os = new FileOutputStream("f://4.xls");
        wb.write(os);
        os.close();        
    }

画图:

public static void main(String[] args) throws IOException {
        //drawing shapes
        /*
         * To create a shape you have to go through the following steps: 

            1.Create the patriarch.
            2.Create an anchor to position the shape on the sheet.
            3.Ask the patriarch to create the shape.
            4.Set the shape type (line, oval, rectangle etc...)
            5.Set any other style details converning the shape. (eg: line thickness, etc...)

         */
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        HSSFPatriarch partriarch = (HSSFPatriarch) sheet.createDrawingPatriarch();
        HSSFSimpleShape shape = partriarch.createSimpleShape(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,5));
        shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
        //shape可以设置很多的属性
        shape.setFillColor(255,200,200);
        shape.setLineStyle(HSSFSimpleShape.LINESTYLE_DASHGEL);
        //Text boxes are created using a different call: 
        OutputStream os = new FileOutputStream("f://5.xls");
        wb.write(os);
        os.close();    

    }

原文地址:https://www.cnblogs.com/estellez/p/4091254.html