poi将数据库数据导出excel文件

poi将数据库数据导出excel文件


excel文档 sheet row cell(单元格)
hssworkbook hssfsheet hssfrow hssfcell


所有版本poi-3.8
下载地址:http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.8-


20120326.zip

导入所需jar包,根目录和lib目录下

示例代码如下:


/**
* writing a new file
*
* @time 1:50:24 PM
* @author retacn yue
* @Email [email protected]
*/
public class App {


/**
* @param args
*/
public static void main(String[] args) {
// declare a workbook object reference
HSSFWorkbook workbook;
// declare a fos obj reference
FileOutputStream fos = null;
try {
// cerate a new file demo.xls
fos = new FileOutputStream("c:/demo.xls");
// create a new workbook
workbook = new HSSFWorkbook();
//if it is exist
//FileInputStream fis = new FileInputStream("c:/demo.xls");
//workbook = new HSSFWorkbook(fis);

// create a new sheet
HSSFSheet sheet = workbook.createSheet("demo1");


// declare a row object reference
Row row = null;
// declare a cell object reference
Cell cell = null;
// create three cell style
CellStyle cellStyle1 = workbook.createCellStyle();
CellStyle cellStyle2 = workbook.createCellStyle();
CellStyle cellStyle3 = workbook.createCellStyle();
DataFormat df = workbook.createDataFormat();


// create two fonts object
Font font1 = workbook.createFont();
Font font2 = workbook.createFont();


// set font 1 to 12 point type
font1.setFontHeightInPoints((short) 12);
// make it blue
font1.setColor((short) 0xc);
// make it bold
font1.setBoldweight(Font.BOLDWEIGHT_BOLD);


// set font 1 to 10 point type
font2.setFontHeightInPoints((short) 10);
// make it red
font2.setColor(Font.COLOR_RED);
// make it bold
font2.setBoldweight(Font.BOLDWEIGHT_BOLD);


font2.setStrikeout(true);


// set cell style
cellStyle1.setFont(font1);
// set cell format
cellStyle1.setDataFormat(df.getFormat("#,##0.0"));


// set a thin border
cellStyle2.setBorderBottom(cellStyle2.BORDER_THIN);
// fill w fg fill color
cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
// set the sell format to text
cellStyle2.setDataFormat(HSSFDataFormat.getBuiltinFormat


("text"));
cellStyle2.setFont(font2);


// set sheetName
// workbook.setSheetName(0, "sheet1");


// create a sheet with 40 rows (0-39)
int rownum;
for (rownum = (short) 0; rownum < 40; rownum++) {
// create a row
row = sheet.createRow(rownum);
// on every other row
if ((rownum % 2) == 0) {
row.setHeight((short) 0x294);
}
}
rownum++;
sheet.createRow(rownum);


cellStyle3.setBorderBottom(cellStyle3.BORDER_THICK);
// create 50 cells
for (short cellnum = (short) 0; cellnum < 50; cellnum++) {
cell = row.createCell(cellnum);
// set it to thick black border style
cell.setCellStyle(cellStyle3);
}


// create a sheet,set its title then delete it
// sheet = workbook.createSheet();
// workbook.setSheetName(1, "deleteSheet");
// workbook.removeName(1);


workbook.write(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

你可能感兴趣的:(java,Excel)