使用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) {
}
}
}
}
创建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) {
}
}
}
}
相关文章推荐
- 关于使用POI导出时excel版本问题的bug解决
- 使用POI导出excel,完美兼容2003及2007以上版本,购物车原理
- java中使用poi导出Excel详解
- 使用apache的poi实现导入导出excel
- 使用POI3.8导出大数据excel方法(解决内存溢出)
- 使用poi 导出Excel文件 并解决中文名乱码
- springMVC中使用POI方式导出excel至客户端、服务器实例
- shh中导出excel(使用poi)
- 使用poi将数据导出到excel
- 使用poi导入、导出Excel内容
- Java中使用poi导入、导出Excel
- excel 导出,关于POI的使用经验
- 使用POI根据SQL自动导出Excel
- Java 导出Excel实例代码整理(POI) 可用
- 使用poi实现导出excel数据表格
- SpringMvc 使用poi导入导出Excel
- POI导出Excel使用教程
- Java中使用poi导入、导出Excel
- poi 导出Excel 使用工具类
- JAVA使用POI根据模版导出EXCEL