1、该功能需要poi的jar包,链接: http://pan.baidu.com/s/1migAtNq 密码: 38fx。
2、首先新建一个实体类,用以存放单个数据
public classTest {privateString x;privateString y;privateString value;public voidsetX(String x) {this.x =x;
}public voidsetY(String y) {this.y =y;
}public voidsetValue(String value) {this.value =value;
}publicString getX() {returnx;
}publicString getY() {returny;
}publicString getValue() {returnvalue;
}
}
3、将数据合并成一个集合,例如list list;
4、开始生成excel
//1、创建workbook,对应一个excel
HSSFWorkbook wb = newHSSFWorkbook();//1.5、生成excel中可能用到的单元格样式//首先创建字体样式
HSSFFont font = wb.createFont();//创建字体样式
font.setFontName("宋体");//使用宋体
font.setFontHeightInPoints((short) 10);//字体大小
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗//然后创建单元格样式style
HSSFCellStyle style1 =wb.createCellStyle();
style1.setFont(font);//将字体注入
style1.setWrapText(true);//自动换行
style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
style1.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());//设置单元格的背景颜色
style1.set