导出信息到Excel实践
在项目中经常会用到将列表的信息导出到Excel或者Word中,每次用到时都记不住调用方法的顺序,都得去网上搜一下或者看看官方文档,对于一些不是经常用到的技术很容易忘记,所以说总结非常重要,平常偶尔翻一翻,加深自己的印象,需要的时候直接看看笔记就直接用上了,提高自己的工作效率。
利用POI导出列表到Excel中
一.
首先创建web工程,添加POI包,使用的是3.9的版本
二.在web项目中导出Excel步骤
1.获得输出流
2.创建HSSFWorkbook
3.创建HSSFSheet 设置名字
4.设置大标题和样式
5.设置小标题和样式
6.生成内容和样式
7.在内存中生成工作簿并输出
下面是主要的代码
private static final String[] ExportField= new String[]{"姓名" ,"电话" ,"住址" ,"性别" };
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
//1获得输出流
OutputStream os=response.getOutputStream();
response.setHeader( "Content-disposition" , "attachment;filename=" + new String( "用户信息导出数据.xls" .getBytes("GBK" ),"ISO-8859-1" ));//文件头,导出的文件名
response.setContentType( "application/msexcel" );//类型
List<User> list= new ArrayList<User>();
User user= new User();
user.setName( "hello" );
user.setSex( "男" );
user.setTelphone( "10010" );
user.setAddress( "北京昌平" );
list.add(user);
User user1= new User();
user1.setName( "Word" );
user1.setSex( "男" );
user1.setTelphone( "10086" );
user1.setAddress( "上海浦东" );
list.add(user1);
HSSFWorkbook workbook =createWriteBook(list);
//7在内存中生成工作簿并输出
os.flush();
workbook .write(os);
}
public HSSFWorkbook createWriteBook(List<User> list){
//2创建HSSFWorkbook
HSSFWorkbook workbook= new HSSFWorkbook();
//3创建HSSFSheet 设置名字
HSSFSheet sheet=workbook.createSheet();
workbook.setSheetName(0, "用户信息表" );
//4设置标题和样式
HSSFRow row=sheet.createRow(0);
HSSFCell cell=row.createCell(0);
HSSFCellStyle titleCS=workbook.createCellStyle();
titleCS.setAlignment(HSSFCellStyle.ALIGN_CENTER );//设置水平位置
titleCS.setVerticalAlignment(HSSFCellStyle. ALIGN_CENTER_SELECTION); //设置上下位置
HSSFFont titleFont=workbook.createFont(); //设置字体
titleFont.setFontHeightInPoints(( short )16);
titleFont.setFontName( "黑体" );
titleFont.setBoldweight(HSSFFont. BOLDWEIGHT_BOLD );
titleCS.setFont(titleFont);
cell.setCellStyle(titleCS);
cell.setCellValue( new HSSFRichTextString("用户信息表" ));//设置标题内容
sheet.addMergedRegion( new CellRangeAddress(0,0,0,3));//合并单元格 从0行0列开始
//5设置小标题和样式
row=sheet.createRow(1);
row.setHeight(( short )500);
HSSFCellStyle columnNameCS=workbook.createCellStyle();
HSSFFont columnNameFont=workbook.createFont();
columnNameFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD );
columnNameCS.setVerticalAlignment(HSSFCellStyle. ALIGN_CENTER_SELECTION);
columnNameCS.setFont(columnNameFont);
for( int i=0;i< ExportField .length ;i++){
cell=row.createCell(i);
cell.setCellStyle(columnNameCS);
cell.setCellValue( ExportField [i]);
sheet.setColumnWidth(i, 2800);
}
//6生成内容和样式
HSSFCellStyle cs=workbook.createCellStyle();
cs.setWrapText( true );
cs.setAlignment(HSSFCellStyle. ALIGN_CENTER );
int rowIndex=1;
for(User user:list){
rowIndex++;
HSSFRow infoRow=sheet.createRow(rowIndex);
cell=infoRow.createCell(0);
cell.setCellType(HSSFCell. CELL_TYPE_STRING );
cell.setCellValue(user.getName());
cell=infoRow.createCell(1);
cell.setCellType(HSSFCell. CELL_TYPE_STRING );
cell.setCellValue(user.getTelphone());
cell=infoRow.createCell(2);
cell.setCellType(HSSFCell. CELL_TYPE_STRING );
cell.setCellValue(user.getAddress());
cell=infoRow.createCell(3);
cell.setCellType(HSSFCell. CELL_TYPE_STRING );
cell.setCellValue(user.getSex());
}
return workbook;
}
}
三.访问项目地址后出现下载提示
四.打开excel文件显示的用户信息
下篇主要是记录使用freemarker导出word的实践。