poi将数据库数据导出excel文件
poi将数据库数据导出excel文件
excel文档sheetrowcell(单元格)
hssworkbookhssfsheethssfrowhssfcell
所有版本poi-3.8
下载地址:http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.8-
20120326.zip
导入所需jar包,根目录和lib目录下
示例代码如下:
/**
*writinganewfile
*
*@time1:50:24PM
*@authorretacnyue
*/
publicclassApp{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//declareaworkbookobjectreference
HSSFWorkbookworkbook;
//declareafosobjreference
FileOutputStreamfos=null;
try{
//cerateanewfiledemo.xls
fos=newFileOutputStream("c:/demo.xls");
//createanewworkbook
workbook=newHSSFWorkbook();
//ifitisexist
//FileInputStreamfis=newFileInputStream("c:/demo.xls");
//workbook=newHSSFWorkbook(fis);
//createanewsheet
HSSFSheetsheet=workbook.createSheet("demo1");
//declarearowobjectreference
Rowrow=null;
//declareacellobjectreference
Cellcell=null;
//createthreecellstyle
CellStylecellStyle1=workbook.createCellStyle();
CellStylecellStyle2=workbook.createCellStyle();
CellStylecellStyle3=workbook.createCellStyle();
DataFormatdf=workbook.createDataFormat();
//createtwofontsobject
Fontfont1=workbook.createFont();
Fontfont2=workbook.createFont();
//setfont1to12pointtype
font1.setFontHeightInPoints((short)12);
//makeitblue
font1.setColor((short)0xc);
//makeitbold
font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
//setfont1to10pointtype
font2.setFontHeightInPoints((short)10);
//makeitred
font2.setColor(Font.COLOR_RED);
//makeitbold
font2.setBoldweight(Font.BOLDWEIGHT_BOLD);
font2.setStrikeout(true);
//setcellstyle
cellStyle1.setFont(font1);
//setcellformat
cellStyle1.setDataFormat(df.getFormat("#,##0.0"));
//setathinborder
cellStyle2.setBorderBottom(cellStyle2.BORDER_THIN);
//fillwfgfillcolor
cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
//setthesellformattotext
cellStyle2.setDataFormat(HSSFDataFormat.getBuiltinFormat
("text"));
cellStyle2.setFont(font2);
//setsheetName
//workbook.setSheetName(0,"sheet1");
//createasheetwith40rows(0-39)
intrownum;
for(rownum=(short)0;rownum<40;rownum++){
//createarow
row=sheet.createRow(rownum);
//oneveryotherrow
if((rownum%2)==0){
row.setHeight((short)0x294);
}
}
rownum++;
sheet.createRow(rownum);
cellStyle3.setBorderBottom(cellStyle3.BORDER_THICK);
//create50cells
for(shortcellnum=(short)0;cellnum<50;cellnum++){
cell=row.createCell(cellnum);
//setittothickblackborderstyle
cell.setCellStyle(cellStyle3);
}
//createasheet,setitstitlethendeleteit
//sheet=workbook.createSheet();
//workbook.setSheetName(1,"deleteSheet");
//workbook.removeName(1);
workbook.write(fos);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(fos!=null){
try{
fos.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}