欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Poi_02_设置单元格的值

程序员文章站 2022-03-06 23:02:33
...

Poi_02_设置单元格的值

1.导入相关依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hoki</groupId>
    <artifactId>poi-demo</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2.执行代码

package com.hoki.poi;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * ClassName:WorkSheetUtil
 * Package:com.hoki.poi
 * Description:工作簿工具类
 *
 * @author :[email protected]://hoki-lin.github.io/hoki.github.io/
 * @date :2019/1/17 4:02
 */
public class WorkSheetUtil {
    private static final String   EXCEL_XLS  = "xls";
    private static final String   EXCEL_XLSX = "xlsx";
    private              String   suffix;
    private              Workbook wb         = null;
    
    public WorkSheetUtil(String suffix) {
        this.suffix = suffix;
        if (this.suffix.equals(EXCEL_XLS)) {
            //Excel 97/2003
            wb = new HSSFWorkbook();
        } else if (this.suffix.equals(EXCEL_XLSX)) {
            // Excel 2007/2010
            wb = new XSSFWorkbook();
        }

    }

    public Workbook getWb() {
        return wb;
    }
}

3.测试代码

package com.hoki.poitest;

import com.hoki.poi.WorkSheetUtil;
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;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

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

/**
 * ClassName:WorkSheetUtilTest
 * Package:com.hoki.poitest
 * Description:工作簿工具类的测试类
 *
 * @author :[email protected]://hoki-lin.github.io/hoki.github.io/
 * @date :2019/1/17 16:26
 */
public class WorkSheetUtilTest {
    private static final String           FILEPATH = "C:\\Users\\lhq\\Desktop";
    private static final String           FILENAME = "poi-test-1";
    private static final String           SUFFIX   = "xlsx";
    private              Workbook         wb       = null;
    private              Sheet            sheet    = null;
    private              FileOutputStream out      = null;

    @Before
    public void init() {
        WorkSheetUtil workSheet = new WorkSheetUtil(SUFFIX);
        wb = workSheet.getWb();
        sheet = wb.createSheet();
    }

    /**
     * 创建空的表格
     */
    @Test
    public void createExcelTest() {
    }

    /**
     * 创建单元格并设置值
     */
    @Test
    public void createCellTest() {
        Row row = sheet.createRow(0);//the first row
        Cell cell = row.createCell(0);//create first cell in first column
        cell.setCellValue(1);

        row.createCell(1).setCellValue(1.2);
        row.createCell(2).setCellValue("字符串");
        row.createCell(3).setCellValue(false);
        row.createCell(4).setCellValue("2019/01/17");
    }


    @After
    public void end() throws IOException {
        try {
            out = new FileOutputStream(FILEPATH + "\\" + FILENAME + "." + SUFFIX);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            wb.write(out);
            out.flush();
            out.close();
        }
    }

}