使用XSSF 创建高版本的excel

来源:互联网 发布:kontakt mac 音源 编辑:程序博客网 时间:2024/05/21 22:43

使用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();    }}

}

阅读全文
0 0
原创粉丝点击