标签:
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
注:此方法创建的为excel 2007版本
private static void CreateExcel(List<EslExcel> eslExcel, String filePath) throws Exception
{
// 创建Excel文档
HSSFWorkbook hwb = new HSSFWorkbook();
//自己创建的excel数据列的实体类
EslExcel xlsDto = null;
// sheet 对应一个工作页
HSSFSheet firstSheet = hwb.createSheet("firstSheet");
HSSFRow firstrow = firstSheet.createRow(0); // 下标为0的行开始
HSSFCell[] firstcell = new HSSFCell[10];
String[] columns = { "col1", "col2", "col3", "col4"};
for (int i = 0; i < columns.length; i++)
{
firstcell[i] = firstrow.createCell(i);
firstcell[i].setCellValue(columns[i]);
}
HSSFSheet secondSheet= hwb.createSheet("secondSheet");
firstrow = secondSheet.createRow(0); // 下标为0的行开始
firstcell = new HSSFCell[3];
columns = new String[]{ "col1", "col2", "col3" };
for (int i = 0; i < columns.length; i++)
{
firstcell[i] = firstrow.createCell(i);
firstcell[i].setCellValue(columns[i]);
}
for (int i = 0; i < eslExcel.size(); i++)
{
// 创建一行
// HSSFRow row_firstSheet = firstSheet.createRow(i + 1);
HSSFRow row_firstSheet = firstSheet.createRow((short) (firstSheet.getLastRowNum() + 1));
HSSFRow row_secondSheet = secondSheet.createRow((short) (secondSheet.getLastRowNum() + 1));
// 得到要插入的每一条记录
xlsDto = eslExcel.get(i);
// for (int colu = 0; colu <= 3; colu++) {
// 在一行内创建各列并赋值
HSSFCell col1 = row_firstSheet.createCell(0);
col1.setCellValue(xlsDto.getNo());
col1 = row_secondSheet.createCell(0);
col1.setCellValue(xlsDto.getCol1());
HSSFCell col2 = row_firstSheet.createCell(1);
col2.setCellValue(xlsDto.getCol2());
col2 = row_secondSheet.createCell(1);
col2.setCellValue(xlsDto.getCol2());
HSSFCell col3= row_firstSheet.createCell(2);
col3.setCellValue(xlsDto.getCol3());
col3 = row_secondSheet.createCell(2);
col3.setCellValue(xlsDto.getCol3());
HSSFCell col4= row_firstSheet.createCell(3);
col4.setCellValue(xlsDto.getCol4());
}
// 创建文件输出流,准备输出电子表格
OutputStream out = new FileOutputStream(filePath);
hwb.write(out);
out.close();
}
标签:
原文地址:http://www.cnblogs.com/xiaxinhaha/p/4874067.html