apache poi 生成excel

Posted 旋转的梦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apache poi 生成excel相关的知识,希望对你有一定的参考价值。

package com.coracle.yk.xmanager.util.poi;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;

import java.io.*;

/**
 * Created by huangbaidong on 2016/3/17.
 */
public class PoiTest {

    public static void main(String args[]) throws IOException {
        String path = "d:\\statistic.xls";
        //POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:/test.xls"));
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet1 = wb.createSheet("导购1");
        HSSFSheet sheet2 = wb.createSheet("导购2");
        HSSFSheet sheet3 = wb.createSheet("导购3");
        sheet1.setDefaultColumnWidth(25);//设置默认列宽
        sheet2.setDefaultColumnWidth(25);
        sheet3.setDefaultColumnWidth(25);
        sheet1.setDefaultRowHeight((short) 300);//设置默认行高
        sheet2.setDefaultRowHeight((short)300);
        sheet3.setDefaultRowHeight((short)300);
        Row row;
        Cell cell;
        HSSFCellStyle style = wb.createCellStyle();
        for(int i=0; i< 10; i++) {
            row = sheet1.createRow(i+1);
            cell = row.createCell(0);
            cell.setCellValue("name0"+i);

            cell = row.createCell(1);
            cell.setCellValue("age1"+i);

            cell = row.createCell(2);
            cell.setCellValue("sex2"+i);

            row = sheet2.createRow(i+1);
            cell = row.createCell(0);
            cell.setCellValue("name1"+i);

            cell = row.createCell(1);
            cell.setCellValue("age2"+i);

            cell = row.createCell(2);
            cell.setCellValue("sex3"+i);

            row = sheet3.createRow(i+1);
            cell = row.createCell(0);
            cell.setCellValue("name"+i);

            cell = row.createCell(1);
            cell.setCellValue("age"+i);

            cell = row.createCell(2);
            cell.setCellValue("sex"+i);
        }

        //false表示不追加, 每次都重写
        FileOutputStream fileOut = new FileOutputStream(path, false);
        wb.write(fileOut);
    }

}

Maven加入poi依赖

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>

 

以上是关于apache poi 生成excel的主要内容,如果未能解决你的问题,请参考以下文章

apache poi 生成excel

poi excel如何用java代码生成

POI生成excel如何实现自动调整行高

poi生成excel文件

JAVA POI 应用系列--生成Excel

POI 生成excel(大数据量) SXSSF