【poi第一节】用poi创建工作薄、sheet页、行row、单元格cell

x33g5p2x  于2021-12-28 转载在 其他  
字(1.1k)|赞(0)|评价(0)|浏览(501)

工作中用到了Excel导出,被迫学习了一下poi,文章都是看视频个人的记录。

poi版本:

<!--excel-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
/**
 * @ClassName 类名:ExcelDemo1
 * @Author作者: hzh
 * @Date时间:2018/12/4 8:44
 **/
public class ExcelDemo1 {

    public static void main(String[] args) throws Exception {
        //获得一个工作薄
        Workbook wb = new HSSFWorkbook();
        //第一个sheet
        Sheet sheet = wb.createSheet("第一个sheet");
        //创建第一行
        Row row = sheet.createRow(0);
        //获取第一个单元格(第一列)
        Cell cell = row.createCell(0);
        //给第一个单元格赋值
        cell.setCellValue("第一个单元格的值");

        //第二列
        cell = row.createCell(1);
        cell.setCellValue(new Date());

        CreationHelper creationHelper = wb.getCreationHelper();
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-MM-dd hh:mm:ss"));

        //第三列
        cell = row.createCell(2);
        cell.setCellValue(new Date());
        cell.setCellStyle(cellStyle);

        //第四列
        cell = row.createCell(3);
        cell.setCellValue(Calendar.getInstance());
        cell.setCellStyle(cellStyle);

        FileOutputStream fileOutputStream = new FileOutputStream("D:\\file\\工作薄.xls");
        wb.write(fileOutputStream);
        wb.close();

    }

相关文章