public void writeExcel2003(List<String> list)
{
//当集合为空,则认为不需要生成EXCEL文件
if (null == list || list.isEmpty())
{
return;
}
FileOutputStream fos = null;
try
{
String filePath = System.getProperty("user.dir")
+ File.separator + "config" + File.separator
+ "2003.xls";
fos = new FileOutputStream(filePath);
HSSFWorkbook workbook = new HSSFWorkbook();
//文件头部的样式
HSSFCellStyle headStyle = workbook.createCellStyle();
headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headStyle.setFillForegroundColor(HSSFColor.ROYAL_BLUE.index);
headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
headStyle.setWrapText(true);
HSSFSheet sheet = workbook.createSheet("sheet0");
HSSFRow headRow = sheet.createRow(0); //创建第一行,第一行为标题部分
HSSFCell headCell = headRow.createCell(0); //创建头部第一列
headCell.setCellValue("head"); //往第一行输入值
headCell.setCellStyle(headStyle); //设置第一行第一列的样式
sheet.setColumnWidth(0, 4500); //设置第一列的列宽
HSSFRow row = null;
HSSFCell cell = null;
//由于需要单数行和双数行的颜色样式设置为不一样,所以下面有两种列的样式
//设置LEMON_CHIFFON颜色
HSSFCellStyle lemonChiffonStyle = workbook.createCellStyle(); //通过workbook创建样式
lemonChiffonStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
lemonChiffonStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
lemonChiffonStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
lemonChiffonStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
lemonChiffonStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
lemonChiffonStyle.setFillForegroundColor(HSSFColor.LEMON_CHIFFON.index);
// functionStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
lemonChiffonStyle.setWrapText(true); //设置可以自动换行
//设置CORNFLOWER_BLUE颜色
HSSFCellStyle cornlowerBlueStyle = workbook.createCellStyle();
cornlowerBlueStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cornlowerBlueStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
cornlowerBlueStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cornlowerBlueStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cornlowerBlueStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
cornlowerBlueStyle.setFillForegroundColor(HSSFColor.CORNFLOWER_BLUE.index);
cornlowerBlueStyle.setWrapText(true);
HSSFCellStyle cellStyle = null;
String value = "";
for (int i = 0; i < list.size(); i++)
{
value = list.get(i);
if (null == value || value.isEmpty())
{
//当value值为空,则进入到下一轮循环
continue;
}
if(i%2==0)
{
//双数列设置lemonChiffonStyle样式
cellStyle = lemonChiffonStyle;
}
else
{
//单数列设置cornlowerBlueStyle样式
cellStyle = cornlowerBlueStyle;
}
row = sheet.createRow(i + 1); // 因为第一行为标题,所以行数要+1
cell = row.createCell(0); // 创建第一列
cell.setCellValue(value);
cell.setCellStyle(cellStyle); //设置列的样式
}
//写出EXCEL文件
workbook.write(fos);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (null != fos)
{
try
{
//关闭FileOutputStream流
fos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}