poi 操作Excel

1. 取得模板

// 创建工作簿
HSSFWorkbook wb = null;

// 取得导出模板
wb = new HSSFWorkbook(new FileInputStream("C://template//template.xls"));

// 创建sheet页
HSSFSheet sheet = wb.getSheet("Sheet1");

2. 创建行

HSSFRow row = sheet.createRow(rowNumber);

3. 创建单元格

HSSFCell cell = row.createCell(cellNumber);

4. 给单元格赋值

cell.setCellValue(value);

5. 设置中文格式

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

6. 设置单元格样式

HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);

7. 保存对话框

HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment;filename="
+ new String("TEST.xls".getBytes("gb2312"), "ISO8859-1")); //中文设置

wb.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();


1. 取得模板

// 创建工作簿
HSSFWorkbook wb = null;

// 取得导出模板
wb = new HSSFWorkbook(new FileInputStream("C://template//template.xls"));

// 创建sheet页
HSSFSheet sheet = wb.getSheet("Sheet1");

2. 创建行

HSSFRow row = sheet.createRow(rowNumber);

3. 创建单元格

HSSFCell cell = row.createCell(cellNumber);

4. 给单元格赋值

cell.setCellValue(value);

5. 设置中文格式

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

6. 设置单元格样式

HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);

7. 保存对话框

HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment;filename="
+ new String("TEST.xls".getBytes("gb2312"), "ISO8859-1")); //中文设置

wb.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();

你可能感兴趣的:(Excel)