您的位置:首页 > 其它

使用poi低版本(poi-3.0.1)导出Excel整理

2011-04-06 11:01 387 查看
最近使用POI低版本(poi-3.0.1-FINAL-20070705.jar)导出数据到Excel,有很多新版本的函数都不可用,整理如下。

创建work book:

HSSFWorkbook wb = new HSSFWorkbook();

创建sheet页:
HSSFSheet sheet = wb.createSheet("sheet页标题");

设置打印预览:

// 设置水平居中

sheet.setHorizontallyCenter(true);

//设置边距 (1=2.5cm)
sheet.setMargin(HSSFSheet.TopMargin, (double) 1);

sheet.setMargin(HSSFSheet.LeftMargin, (double) 1);
sheet.setMargin(HSSFSheet.BottomMargin, (double) 1);
sheet.setMargin(HSSFSheet.RightMargin, (double) 1);

HSSFPrintSetup ps = sheet.getPrintSetup();
ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);

// 设置打印朝向
ps.setLandscape(true);// 设置横向打印
ps.setLandscape(false);// 设置纵向打印

ps.setScale((short) 80); // 设置打印缩放比例
ps.setHeaderMargin((double) 0.5);// 设置页眉打印范围
ps.setFooterMargin((double) 0.5);// 设置页脚打印范围

//插入图片:
String imagePath = DOCSettingConst.REPORT_REPORT_IMAGE_PATH;
imagePath = DOCSettingConst.REPORT_TEMPLATE_PATH_ABSOLUTE + imagePath;

ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedImage bufferImg = ImageIO.read(new File(imagePath));
ImageIO.write(bufferImg, "jpg", byteArrayOut);

HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 850, 0, (short) 0, 0, (short) 3, 7);
HSSFPicture pic = patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(),
HSSFWorkbook.PICTURE_TYPE_JPEG));

//创建cell

HSSFRow row = sheet.getRow(3);
HSSFCell cell36 = row.getCell((short) 6);
if (cell36 == null) {
cell36 = row.createCell((short) 6);
}

if (arrivalUsedTugs != null) {

//设置数字cell:
cell36 .setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell36 .setCellValue(arrivalUsedTugs);
} else {

//设置字符串cell:
cell36 .setCellType(HSSFCell.CELL_TYPE_STRING);
cell36 .setCellValue(new HSSFRichTextString(""));
}

//合并单元格:
Region mergeRegion3638 = new Region(3, (short) 6, 3, (short) 8);
sheet.addMergedRegion(mergeRegion3638);

//设置显示样式:

HSSFCellStyle style = wb.createCellStyle();

style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);

style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
style.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);

HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 24);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

style.setFont(font);

//导出到Excel:

String excelPath = "....";

String fileName = "....";

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String currentDate = sdf.format(new Date());
StringBuffer sb = new StringBuffer();
sb.append(excelPath);
sb.append(fileName);
sb.append(currentDate);
sb.append(".xls");
pathName = sb.toString();
export(pathName, wb);

public static void export(String fileName, HSSFWorkbook wb) throws Exception {
FileOutputStream fileOut = null;
try {
File excelFile = new File(fileName);
if (excelFile.exists()) {
excelFile.delete();
}

excelFile.createNewFile();

fileOut = new FileOutputStream(excelFile);
wb.write(fileOut);
} catch (Exception e) {
throw e;
} finally {
if (fileOut != null) {
try {
fileOut.close();
} catch (Exception e) {

}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: