第一步:所需要的jar包
第二步:上代码
package cn.lystudio.poi.excel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
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.xssf.usermodel.XSSFWorkbook;
/**
* 生成excel文件
* @author Administrator
*
*/
public class PoiCExcel2 {
public static void main(String[] args) {
//创建一个数组,存放数据
String[] title = {"id","name","sex"};
//创建excel工作蒲
XSSFWorkbook workbook = new XSSFWorkbook();
//创建一个工作表sheet
Sheet sheet = workbook.createSheet();
//创建第一行
Row row = sheet.createRow(0);
//定义cell
Cell cell = null;
//插入第一行数据,id,name,sex
for ( int i =0; i<title.length;i++) {
cell = row.createCell(i);
cell.setCellValue(title[i]);
}
//追加数据
for ( int i =1; i<10;i++) {
Row nextrow =sheet.createRow(i);
Cell cell2 = nextrow.createCell(0);
cell2.setCellValue("a"+i);
cell2 = nextrow.createCell(1);
cell2.setCellValue("user"+i);
cell2 = nextrow.createCell(2);
cell2.setCellValue("女生");
}
//创建一个文件
File file = new File("D:/poi_test.xlsx");
try {
file.createNewFile();
//将excel内容存盘
FileOutputStream stream = FileUtils.openOutputStream(file);
workbook.write(stream);
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}