免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1258 | 回复: 0
打印 上一主题 下一主题

POI的简单使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-05-25 10:52 |只看该作者 |倒序浏览
一:简介

利用POI工具可以导出word,excel,ppt等office文件

二:程序代码示例
  1. package com.wang.test;

  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.util.Calendar;
  5. import java.util.Date;

  6. import org.apache.poi.hssf.usermodel.HSSFCell;
  7. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  8. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  9. import org.apache.poi.hssf.usermodel.HSSFRow;
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  12. public class Test {
  13.     public static void main(String[] args) throws Exception{
  14.         //创建工作簿
  15.         HSSFWorkbook wb=new HSSFWorkbook();
  16.         //创建工作表
  17.         HSSFSheet sheet1=wb.createSheet("first sheet");
  18.         HSSFSheet sheet2=wb.createSheet("second sheet");
  19.         //创建row
  20.         HSSFRow row=sheet1.createRow(0);
  21.         //创建单元格
  22.         HSSFCell cell=row.createCell(0);
  23.         cell.setCellValue(false);
  24.         row.createCell(1).setCellValue(Calendar.getInstance());
  25.         row.createCell(2).setCellValue(new Date());
  26.         row.createCell(3).setCellValue(123456.654321);
  27.         String str="abcdefghi";
  28.          
  29.         //创建数据格式对象
  30.         HSSFDataFormat format=wb.createDataFormat();
  31.         //创建单元格样式
  32.         HSSFCellStyle style=wb.createCellStyle();
  33.          
  34.         //对日期格式化
  35.         style.setDataFormat(format.getFormat("yyyy-MM-dd HH:mm:ss"));
  36.         //应用样式给单元格
  37.         row.getCell(1).setCellStyle(style);
  38.         row.getCell(2).setCellStyle(style);
  39.          
  40.         //对double值格式化
  41.         style=wb.createCellStyle();
  42.         style.setDataFormat(format.getFormat("#.00"));
  43.         row.getCell(3).setCellStyle(style);
  44.          
  45.         //设置列宽,注意:列宽相对于sheet的。
  46.         sheet1.setColumnWidth(1, 3000);
  47.         //也可以自动调节列宽
  48.         sheet1.autoSizeColumn(2);
  49.         sheet1.setColumnWidth(4, 7000);
  50.         //自动回绕文本,把太长的字符串换行显示
  51.         row=sheet1.createRow(1);
  52.         row.createCell(0).setCellValue("左上");
  53.         row.createCell(1).setCellValue("中间");
  54.         row.createCell(2).setCellValue("右下");
  55.          
  56.         //设置行高
  57.         row.setHeightInPoints(50);
  58.         sheet1.setColumnWidth(0, 5000);
  59.         sheet1.setColumnWidth(1, 5000);
  60.         sheet1.setColumnWidth(2, 5000);
  61.          
  62.         //设置对齐方式--左上
  63.         style=wb.createCellStyle();
  64.         style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
  65.         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
  66.         row.getCell(0).setCellStyle(style);
  67.          
  68.         //设置对齐方式--中间
  69.         style=wb.createCellStyle();
  70.         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  71.         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  72.         row.getCell(1).setCellStyle(style);
  73.          
  74.         //设置对齐方式--右下
  75.         style=wb.createCellStyle();
  76.         style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
  77.         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);
  78.         row.getCell(2).setCellStyle(style);
  79.          
  80.         //重点:计算列
  81.         row=sheet1.createRow(3);
  82.         row.createCell(0).setCellValue(13);
  83.         row.createCell(1).setCellValue(45);
  84.         row.createCell(2).setCellValue(25);
  85.         row.createCell(3).setCellFormula("sum(A4:C4)");
  86.         //导出到磁盘
  87.         wb.write(new FileOutputStream(new File("f:/poi.xls")));
  88.     }
  89. }
复制代码
效果:
                           
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP