码迷,mamicode.com
首页 > Web开发 > 详细

apache poi 生成excel

时间:2016-03-17 21:41:16      阅读:386      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/cocoat/p/5289224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!