优雅的使用Java操作Excel-优雅的使用java操作excel

88 阅读1分钟

具体实现

写操作:

/**
 * @Author i8023tp
 **/
public class ExcelWirteTest {
    public static void main(String[] args) throws IOException {

        final String PATH="D:\\workspace\\idea's workspace\\Java_study";

        Workbook workbook=new HSSFWorkbook();

        Sheet sheet=workbook.createSheet("zzm-1");

        Row row=sheet.createRow(0);

        Cell cell=row.createCell(0);

        cell.setCellValue("asdasdasdas");

        FileOutputStream outputStream=new FileOutputStream(PATH + "xxxx.xls");

        workbook.write(outputStream);

        outputStream.close();

        System.out.println("生成完毕");
    }
}

测试结果:

这里生成的是03版本的文件,至于03 和 07 有什么区别,其实就是03只能满足65536行的数据,而07的数据量则是无限

读操作:

    public void readTest() throws IOException {

        final String PATH="D:\\workspace\\idea's workspace\\Java_study";

        FileInputStream inputStream = new FileInputStream(PATH + "xxx.xls");

        Workbook workbook =new HSSFWorkbook(inputStream);

        Sheet sheetAt = workbook.getSheetAt(0);

        Row row = sheetAt.getRow(0);

        Cell cell = row.getCell(0);

        String stringCellValue = cell.getStringCellValue();

        System.out.println(stringCellValue);


    }

但是要注意的就是读取的数据类型,一定要细心,要不然会报错。