使用XSSF 创建高版本的excel

使用XSSF 创建高版本的excel

标签(空格分隔): POI


使用HSSF进行excel导入和导出实现的文件后缀名为.xls,这是1997到2003版本的excel,如果使用例如2007等高版本的excel文件,则需要换用XSSF技术来实现excel导入和导出,生成的文件后缀为.xlsx。下面是使用XSSF技术来实现excel导出。

package com.excel.poi;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PoiXssfExpExcel {

public static void main(String[] args){

    String[] title={"id","name","sex"};

    //创建工作簿
    XSSFWorkbook workbook=new XSSFWorkbook();

    //创建工作表
    XSSFSheet sheet=workbook.createSheet();

    //创建第一行
    XSSFRow row=sheet.createRow(0);
    XSSFCell cell=null;

    //插入第一行
    for(int i=0;i<title.length;i++){
        cell=row.createCell(i);
        cell.setCellValue(title[i]);
    }

    for(int i=1;i<=10;i++){
        XSSFRow nextrow=sheet.createRow(i);
        XSSFCell cell2=nextrow.createCell(0);
        cell2.setCellValue("a"+i);
        cell2=nextrow.createCell(1);
        cell2.setCellValue("user"+i);
        cell2=nextrow.createCell(2);
        cell2.setCellValue("boy");
    }

    //创建excel文件
    File file=new File("e:/poi_test2.xlsx");
    try {
        file.createNewFile();
        //将excel内容存盘
        FileOutputStream stream=FileUtils.openOutputStream(file);
        workbook.write(stream);
        stream.close();
        workbook.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

猜你喜欢

转载自blog.csdn.net/u013985662/article/details/73379789