POI导出Excel格式的报表简单易用
如例:
//创建标题行
public static void createTitle(HSSFWorkbook wb,HSSFSheet sheet,
String title,short margeCount){
HSSFRow titleRow = sheet.createRow(0); //行和列索引都是从0开始
HSSFCell titleCell = titleRow.createCell((short)0);
//设置样式
HSSFCellStyle style = wb.createCellStyle();
HSSFFont f = wb.createFont();
f.setFontName("黑体"); //字体
f.setFontHeight((short)300); //字高
style.setFont(f);
titleCell.setCellStyle(style); //关联样式
titleCell.setEncoding(HSSFCell.ENCODING_UTF_16); //字符集
titleCell.setCellValue(title); //列内容
//合并单元格
sheet.addMergedRegion(new Region(0,(short)0,0,margeCount));
}
//创建列
public static void createTitleCell(HSSFWorkbook wb,HSSFRow row,
short column,String cellValue){
HSSFCell cell = row.createCell(column);
HSSFCellStyle style = wb.createCellStyle();
HSSFFont f = wb.createFont();
if(row.getRowNum()==1){
f.setFontName("宋体");
f.setFontHeight((short)260);
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置粗体
}
else{
f.setFontName("楷体");
}
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置居中
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(cellValue);
}
//创建数字列
public static void createMathCell(HSSFWorkbook wb,HSSFRow row,
short column,String cellValue){
HSSFCell cell = row.createCell(column);
HSSFCellStyle style = wb.createCellStyle();
HSSFFont f = wb.createFont();
f.setFontName("楷体");
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(Double.parseDouble(cellValue));
}
//创建正文
public void saveExcelByPOI() throws IOException {
users = introService.retrieveUsers();
ActionContext ctx = ActionContext.getContext();
HttpServletResponse response = (HttpServletResponse)
ctx.get(ServletActionContext.HTTP_RESPONSE);
response.addHeader("Content-Disposition",
"p_w_upload;filename="+new String ("POI测试样例.xls".getBytes(),"ISO8859-1"));
OutputStream out = response.getOutputStream();
HSSFWorkbook wb = new HSSFWorkbook(); //创建工作簿
HSSFSheet sheet = wb.createSheet(); //创建工作表
sheet.setDefaultColumnWidth((short)20); //设置每列默认宽度
String title = "POI测试样例";
createTitle(wb, sheet, title, (short)4); //创建标题
HSSFRow row = sheet.createRow(1);
createTitleCell(wb, row, (short)0, "姓名");
createTitleCell(wb, row, (short)1, "年龄");
createTitleCell(wb, row, (short)2, "性别");
createTitleCell(wb, row, (short)3, "住址");
createTitleCell(wb, row, (short)4, "邮箱");
int j = 2;
for(int i=0;i<users.size();i++){
row = sheet.createRow(j);
info = (MyInfo)users.get(i);
createTitleCell(wb, row, (short)0, info.getName());
createMathCell(wb, row, (short)1, String.valueOf(info.getAge()));
createTitleCell(wb, row, (short)2, info.getSex());
createTitleCell(wb, row, (short)3, info.getAddr());
createTitleCell(wb, row, (short)4, info.getEmail());
j++;
}
wb.write(out);
out.close();
}
如例:
//创建标题行
public static void createTitle(HSSFWorkbook wb,HSSFSheet sheet,
String title,short margeCount){
HSSFRow titleRow = sheet.createRow(0); //行和列索引都是从0开始
HSSFCell titleCell = titleRow.createCell((short)0);
//设置样式
HSSFCellStyle style = wb.createCellStyle();
HSSFFont f = wb.createFont();
f.setFontName("黑体"); //字体
f.setFontHeight((short)300); //字高
style.setFont(f);
titleCell.setCellStyle(style); //关联样式
titleCell.setEncoding(HSSFCell.ENCODING_UTF_16); //字符集
titleCell.setCellValue(title); //列内容
//合并单元格
sheet.addMergedRegion(new Region(0,(short)0,0,margeCount));
}
//创建列
public static void createTitleCell(HSSFWorkbook wb,HSSFRow row,
short column,String cellValue){
HSSFCell cell = row.createCell(column);
HSSFCellStyle style = wb.createCellStyle();
HSSFFont f = wb.createFont();
if(row.getRowNum()==1){
f.setFontName("宋体");
f.setFontHeight((short)260);
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置粗体
}
else{
f.setFontName("楷体");
}
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置居中
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(cellValue);
}
//创建数字列
public static void createMathCell(HSSFWorkbook wb,HSSFRow row,
short column,String cellValue){
HSSFCell cell = row.createCell(column);
HSSFCellStyle style = wb.createCellStyle();
HSSFFont f = wb.createFont();
f.setFontName("楷体");
style.setFont(f);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(Double.parseDouble(cellValue));
}
//创建正文
public void saveExcelByPOI() throws IOException {
users = introService.retrieveUsers();
ActionContext ctx = ActionContext.getContext();
HttpServletResponse response = (HttpServletResponse)
ctx.get(ServletActionContext.HTTP_RESPONSE);
response.addHeader("Content-Disposition",
"p_w_upload;filename="+new String ("POI测试样例.xls".getBytes(),"ISO8859-1"));
OutputStream out = response.getOutputStream();
HSSFWorkbook wb = new HSSFWorkbook(); //创建工作簿
HSSFSheet sheet = wb.createSheet(); //创建工作表
sheet.setDefaultColumnWidth((short)20); //设置每列默认宽度
String title = "POI测试样例";
createTitle(wb, sheet, title, (short)4); //创建标题
HSSFRow row = sheet.createRow(1);
createTitleCell(wb, row, (short)0, "姓名");
createTitleCell(wb, row, (short)1, "年龄");
createTitleCell(wb, row, (short)2, "性别");
createTitleCell(wb, row, (short)3, "住址");
createTitleCell(wb, row, (short)4, "邮箱");
int j = 2;
for(int i=0;i<users.size();i++){
row = sheet.createRow(j);
info = (MyInfo)users.get(i);
createTitleCell(wb, row, (short)0, info.getName());
createMathCell(wb, row, (short)1, String.valueOf(info.getAge()));
createTitleCell(wb, row, (short)2, info.getSex());
createTitleCell(wb, row, (short)3, info.getAddr());
createTitleCell(wb, row, (short)4, info.getEmail());
j++;
}
wb.write(out);
out.close();
}
转载于:https://blog.51cto.com/xiasxjy/128357