zoukankan      html  css  js  c++  java
  • POI的简单使用

    一:简介

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

    二:程序代码示例

    package com.wang.test;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.Calendar;
    import java.util.Date;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFDataFormat;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    public class Test {
    	public static void main(String[] args) throws Exception{
    		//创建工作簿
    		HSSFWorkbook wb=new HSSFWorkbook();
    		//创建工作表
    		HSSFSheet sheet1=wb.createSheet("first sheet");
    		HSSFSheet sheet2=wb.createSheet("second sheet");
    		//创建row
    		HSSFRow row=sheet1.createRow(0);
    		//创建单元格
    		HSSFCell cell=row.createCell(0);
    		cell.setCellValue(false);
    		row.createCell(1).setCellValue(Calendar.getInstance());
    		row.createCell(2).setCellValue(new Date());
    		row.createCell(3).setCellValue(123456.654321);
    		String str="abcdefghi";
    		
    		//创建数据格式对象
    		HSSFDataFormat format=wb.createDataFormat();
    		//创建单元格样式
    		HSSFCellStyle style=wb.createCellStyle();
    		
    		//对日期格式化
    		style.setDataFormat(format.getFormat("yyyy-MM-dd HH:mm:ss"));
    		//应用样式给单元格
    		row.getCell(1).setCellStyle(style);
    		row.getCell(2).setCellStyle(style);
    		
    		//对double值格式化
    		style=wb.createCellStyle();
    		style.setDataFormat(format.getFormat("#.00"));
    		row.getCell(3).setCellStyle(style);
    		
    		//设置列宽,注意:列宽相对于sheet的。
    		sheet1.setColumnWidth(1, 3000);
    		//也可以自动调节列宽
    		sheet1.autoSizeColumn(2);
    		sheet1.setColumnWidth(4, 7000);
    		//自动回绕文本,把太长的字符串换行显示
    		row=sheet1.createRow(1);
    		row.createCell(0).setCellValue("左上");
    		row.createCell(1).setCellValue("中间");
    		row.createCell(2).setCellValue("右下");
    		
    		//设置行高
    		row.setHeightInPoints(50);
    		sheet1.setColumnWidth(0, 5000);
    		sheet1.setColumnWidth(1, 5000);
    		sheet1.setColumnWidth(2, 5000);
    		
    		//设置对齐方式--左上
    		style=wb.createCellStyle();
    		style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
    		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
    		row.getCell(0).setCellStyle(style);
    		
    		//设置对齐方式--中间
    		style=wb.createCellStyle();
    		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    		row.getCell(1).setCellStyle(style);
    		
    		//设置对齐方式--右下
    		style=wb.createCellStyle();
    		style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
    		style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);
    		row.getCell(2).setCellStyle(style);
    		
    		//重点:计算列
    		row=sheet1.createRow(3);
    		row.createCell(0).setCellValue(13);
    		row.createCell(1).setCellValue(45);
    		row.createCell(2).setCellValue(25);
    		row.createCell(3).setCellFormula("sum(A4:C4)");
    		//导出到磁盘
    		wb.write(new FileOutputStream(new File("f:/poi.xls")));
    	}
    }
    效果:

    1

  • 相关阅读:
    Rust v1.39发布
    Theory of Storage
    What is Market Intelligence and how is it Used?
    敏感性分析与风险分析
    四大交易所重磅齐发布!涉及股指期权上市、市场体系创新、优化期权做市商机制等等
    库存+基差+跨期套利的交易策略
    大宗商品贸易模式及风险识别——基于贸易融资方式、真实贸易资金需求等视角
    CME Futures & Options Order Book
    什么是期货跟单?什么是期货跟单系统?
    ScrollReveal-元素随页面滚动产生动画的js插件
  • 原文地址:https://www.cnblogs.com/liuruowang/p/4525975.html
Copyright © 2011-2022 走看看